I’d like to try some new C# techniques I’ve been reading about. Unfortunately, I don’t have any hardware here at home. According to what I’ve read, I should be able to use the emulator built into the .net micro framework. I’ve read a bunch of posts from the web and this forum, but still can’t make it work. I’ve got my Project->Properites->.NET Micro Framework Transport set to Emulator and the"Sample Emulator" window pops up when I run my program but nothing shows up on it when I execute the .Write command in my program. The emulator window does have an “Emulator Serial Ports” menu item that has a “Write to Usart2 (COM2)” choice. When I type stuff into the window that pops up I don’t seem to be able to read it in my .net program. I’ve tried COM1 and COM2 with similar results. I’m sure I’d doing something dumb or don’t have something set up correctly but I haven’t been able to figure out what.
Thanks for the help.
Here’s the simplest possible program I’m starting with.
using System;
using System.Collections;
using System.Threading;
using System.IO.Ports;
using System.Text;
using Microsoft.SPOT;
namespace realSerialIO
{
public class Program
{
public static void Main()
{
SerialPort UART = new SerialPort("COM2", 9600);
byte[] rxData = new byte[10];
byte[] txData = new byte[10];
int readCount = 0;
Debug.Print("\r\nStarting Program\r\n");
UART.Open();
txData = Encoding.UTF8.GetBytes("Test 1");
UART.Write(txData, 0, txData.Length);
readCount = UART.Read(rxData, 0, 1);
}
}
}
Hmm, doesn’t seem to be working for me. Does this seem right just as a quick test?
using System;
using System.Collections;
using System.Threading;
using System.IO.Ports;
using System.Text;
using Microsoft.SPOT;
namespace realSerialIO
{
public class Program
{
static SerialPort UART = new SerialPort("COM2", 57600);
static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] buffer = new byte[10];
UART.Read(buffer, 0, buffer.Length);
UART.DiscardInBuffer();
UART.Flush();
Debug.Print("Got a character" );
}
public static void Main()
{
Debug.Print("\r\nStarting Program\r\n");
UART.Open();
UART.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
//write something just for the heck of it
byte[] buffer = new byte[1];
buffer[0] = (byte)'4';
UART.Write(buffer, 0, 1);
buffer = null;
while (true)
{
Debug.Print("sleep for 10ms");
Thread.Sleep(10);
}
}
}
}
I am not sure if that bug is fixed or not, but the order is/was important and you have to do it the way you are doing it now. First Open and then subscribe.
@ Gene - Your program is not acting as you expect since you wrote one byte and when the DataReceived event occurs you are reading ten bytes and blocking.
There is a a BytesToRead property which tells you how many bytes are available to be read in the internal receive buffer. You should use this value to do your read in the event handler.
Still no joy. I’ve tried it with the UART.open ahead of and after attaching the event. I have a chunk in my code ahead of the event driven part that makes sure polled IO works. It is currently commented out but I’ve tried it and it works fine. The sequence of events is 1) start debugging 2) The emulator pops up and I hit the “Emulator Serial Ports” drop down 3) Then I hit the write to Usart2 (COM2) pick (it’s the only one) 4) type in some characters and hit the send button. When the polled IO code isn’t commented out (as it is below), it gets the characters I type in just fine. However, regardless of the position of the UART.Open and whether the polled IO chunk is commented out or not, the Debug.Print statement in the event handler never prints nor do I hit the break point I have set on that line.
Is there anything else I have to include in my program to get event driven IO working except the “using” stuff below?
Any project settings I’m missing?
Can anyone else get the emulator to work using event driven IO?
I have the following references in my project:
Microsoft.SPOT.Hardware
Microsoft.SPOT.Hardware.SerialPort
Microsoft.SPOT.Native
mscorlib
Here’s my code as it stands now:
using System;
using System.Collections;
using System.Threading;
using System.IO.Ports;
using System.Text;
using Microsoft.SPOT;
namespace realSerialIO
{
public class Program
{
static SerialPort UART = new SerialPort("COM2", 57600);
static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] buffer = new byte[10];
Debug.Print("Got a character");
UART.Read(buffer, 0, 1);
UART.DiscardInBuffer();
UART.Flush();
}
public static void Main()
{
Debug.Print("\r\nStarting Program\r\n");
/* Uncomment this section to see if polled IO works
*
* UART.Open();
//write something just to see if it shows up on the emulator
string txString = "Hopefully this will show up at the emulator \r\n";
byte[] txBytes = Encoding.UTF8.GetBytes(txString);
UART.Write(txBytes, 0, txBytes.Length);
//First try reading serial port using polled IO
char rxByte;
String rxString = "";
int readCount = 0;
Debug.Print("Waiting for at least 10 characters");
while (readCount < 10)
{
while (UART.BytesToRead > 0)
{
rxByte = (char)UART.ReadByte();
rxString = rxString + rxByte.ToString();
readCount++;
}
Thread.Sleep(10);
}
Debug.Print("Got the string: " + rxString);
UART.Close();
*/
UART.Open();
UART.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
//UART.Open();
Debug.Print("Waiting for characters to arrive at serial event handler");
Thread.Sleep(Timeout.Infinite);
}
}
}
Well it is disappointing to find out the emulator isn’t all I hoped it would be. On the other hand, it was nice to find out you got the same results as I did. Thanks again for all the help.