How do I use the serial port emulator

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);
        }
    }
}

When I run your app against the emulator, it appears to work.

The return from the UART.Read() returns how many characters it read; it returned one, which is how many you asked it to read.

As far as I know there’s no way to map the serial port output back to a port on the PC in the default emulator, to really show the data you SEND to the serial port. There’s more info in “Option 2 - Extend the Emulator” in http://blogs.msdn.com/b/netmfteam/archive/2010/06/21/bicycle-computer-7-working-with-the-emulator-when-you-have-new-peripherals.aspx

You’re right, the read part works fine. I was stuck at trying to get the write part to work and never stepped past that line. I did find a blog on the internet that talks about hooking up hyperterm as a serial port emulator http://bloggingabout.net/blogs/jens/archive/2007/09/08/connecting-emulator-com-ports-to-hyper-terminal.aspx . It’s just more work than I want to start on a Sunday afternoon.

Thanks again

One more question: Does the emulator allow event driven serial reads?

Jens’ info really emulates a TCP/IP socket connected serial port, same result though…

As far as events go, I am sure it would; it’s jus running netmf !

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);
            }
        }
    }
}

@ Gene

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.

It is all about timing. It might work in one instance but will fail in another. Usually worked under debugger , but fails on its own.

@ 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.

Interesting question (first open, or first subscribe). Is there only one correct way or can both sequences be used?

The problem has always occurred if event registration occurred before the open, as Architect mentioned.

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);
        }
    }
}

Thanks for all the help

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.

Cheers - Gene