Fun with Emulator-COM-Ports - Part II
In my last post I described how you can route data between a .NET Micro Framework Emulator and Sockets to debug a .NET Micro Framework App with Hyper-Terminal. It is possible with the extensible emulator to route data to all kind of streams!
No you will learn how to let an emulated Microsoft .NET Micro Framework application communicate with physical COM-Ports on your host PC (also virtual Ports).Therefore I wrote the emulator component ComPortToPhysicalPcPort.This allows you to debug your app with feeding and
verifying serial test data with the socket component on Hyper-Teminal first.And then by just exchanging the socket component with the physical
component (only a few XML lines) allows you to connect a real device to a PC COM Port and debug without having a MF development board but your real serial device. At last if you have your real hardware platform and real serial device connect to it your code should run like a charm.
The emulator component:
using System;
using System.IO.Ports;
using Microsoft.SPOT.Emulator.Com;
namespace Kuehner.SPOT.Emulator
{
public class ComPortToPhysicalPcSerialPort : ComPortToStream
{
private SerialPort serialPort;
private string physicalPortName = "COM1";
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 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;
}
}
#region properties
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; }
}
#endregion
}
}
The emulator configuration:
<?xml version="1.0" encoding="utf-8" ?>
<Emulator>
<Types>
<PcCom>Kuehner.SPOT.Emulator.ComPortToPhysicalPcSerialPort, ComPortEmulatorComponents</PcCom>
</Types>
<EmulatorComponents>
<PcCom>
<ComPortHandle>Usart1</ComPortHandle>
<!--optional-->
<PhysicalPortName>COM1</PhysicalPortName>
<Baudrate>9600</Baudrate>
<ReadTimeout>1000</ReadTimeout>
</PcCom>
</EmulatorComponents>
</Emulator>