Main Site Documentation

SerialPort in custom emulator


#1

Hi,

I’m trying to port develop a custom emulator device to be able to run my application there for demo purpose. I’m using .NET MF 4.3 to this.
This application uses UART for communication with external devices and this behaviour needs to be emulated in order to demo my application before sb decides to buy real hardware.
The problem I have is that from inside of my application I always receive ArgumentException from SerialPort::InternalOpen() method:

[quote]#### Exception System.ArgumentException - 0xfd000000 (1) ####

Message:

System.IO.Ports.SerialPort::InternalOpen [IP: 0000]

System.IO.Ports.SerialPort::Open [IP: 0018]

Gadgeteer.SocketInterfaces.NativeSerial::Open [IP: 0010]

Gadgeteer.Program::Run [IP: 0016]

A first chance exception of type ‘System.ArgumentException’ occurred in Microsoft.SPOT.Hardware.SerialPort.dll
[/quote]

This is my application PostInit method of my mainboard:

public override void PostInit()
{
    var list = new ArrayList();
    foreach (var socket in sockets)
    {
        if (socket.SerialPortName != null && socket.SerialPortName.Length > 0 && socket.SerialPortName == "COM1")
        {
            // Create and open serial port
            var serial = SerialFactory.Create(
                socket,
                9600,
                SerialParity.None,
                SerialStopBits.One,
                8,
                HardwareFlowControl.NotRequired,
                null);
            serial.Open();
            list.Add(serial);
        }
    }
    serialPorts = new Serial[list.Count];
    list.CopyTo(serialPorts, 0);
}

And this is how I declare “COM1” port in my custom emulator code:

public override void SetupComponent()
{
    base.SetupComponent();
 
    var serial = new SerialPortModule()
    {
        Baudrate = 9600,
        ComPortHandle = new Microsoft.SPOT.Emulator.Com.ComPortHandle(Microsoft.SPOT.Emulator.Com.TransportType.Usart, 1),
        Handshake = System.IO.Ports.Handshake.None,
        PhysicalPortName = "COM10",
        SupportNonStandardBaudRate = true
    };
    this.RegisterComponent(serial);
}

“COM10” is a virtual port registered on my laptop and it’s available from i.e. Hyper-Terminal so seems like emulator has this part defined properly.
Oh and SerialPortModule class:

public class SerialPortModule : ComPortToStream
{
    private System.IO.Ports.SerialPort serialPort;
    private string physicalPortName = "COM10";
    private int baudrate = 9600;
    private int readTimeout = 1000;
    private Handshake handshake = Handshake.None;
 
    protected override void InitializeProtected()
    {
        base.InitializeProtected();
        if (this.Stream == null)
        {
            this.serialPort = new System.IO.Ports.SerialPort(this.physicalPortName, this.baudrate);
            this.serialPort.ReadTimeout = this.readTimeout;
            this.serialPort.Handshake = this.handshake;
            this.serialPort.Open();
            this.Stream = this.serialPort.BaseStream;
        }
    }
 
    protected override void UninitializeProtected()
    {
        base.UninitializeProtected();
        if (this.Stream != null)
        {
            this.serialPort.Close(); //also closes the underlying stream
            this.serialPort = null;
            this.Stream = null;
        }
    }
 
    public string PhysicalPortName
    {
        get
        { 
            return this.physicalPortName; 
        }
        set 
        { 
            this.physicalPortName = value; 
        }
    }
 
    public int Baudrate
    {
        get
        {
            return this.baudrate; 
        }
        set
        { 
            this.baudrate = value; 
        }
    }
 
    public int ReadTimeout
    {
        get 
        { 
            return this.readTimeout; 
        }
        set 
        { 
            this.readTimeout = value; 
        }
    }
 
    public Handshake Handshake
    {
        get 
        { 
            return this.handshake; 
        }
        set 
        {
            this.handshake = value; 
        }
    }
}

Probably it is something obvious for someone who created custom emulator before but I’m new to this. I would be really grateful if you could help me to solve that out. The problem is that my application code doesn’t work on emulator only, and I do not have any problems on real FEZ hardware.

Best regards,
Lukasz


#2

Welcome to the forum.

You need to add the comport to the emulator configuration.
You can find it for 4.3 sample emulater there:
“C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.3\Tools\Microsoft.SPOT.Emulator.Sample.SampleEmulator.exe.emulatorconfig”

in there is a xml element like this:

<PhysicalSerialPort id="COM1">
  <ComPortHandle>Usart1</ComPortHandle>
  <PhysicalPortName>COM1</PhysicalPortName>
  <BaudRate>38400</BaudRate>
</PhysicalSerialPort>

Copy it and rename it like this:

<PhysicalSerialPort id="COM10">
  <ComPortHandle>Usart10</ComPortHandle>
  <PhysicalPortName>COM10</PhysicalPortName>
  <BaudRate>38400</BaudRate>
</PhysicalSerialPort>

I’m not sure if you need to define the BaudRate here. I did it and it works fine for me.

Be sure to use the correct config file for your simulator.


#3

Thanks!

I was pretty sure that I tried that yesterday evening but it didn’t work. I checked that now once again and it worked! Thank you so much for helpful and quick answer! :slight_smile:


#4

You’re welcome :smiley: