using System; using System.Threading; using System.IO.Ports; using System.Text; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; namespace simpleTerm { public class Program { //COM1 = Console (19200) COM2 = Elmo (19200) COM3 = DGH (38400) public static SerialPort terminalPort = new SerialPort("COM3", 38400, Parity.None, 8, StopBits.One); public static string rxString = ""; private static AutoResetEvent ev = new AutoResetEvent(false); public static void Main() { Debug.Print("\r\nStarting Program\r\n"); byte[] txBytes; string txString = ""; terminalPort.ReadTimeout = 2000; terminalPort.Open(); terminalPort.DiscardInBuffer(); terminalPort.DiscardOutBuffer(); terminalPort.DataReceived += new SerialDataReceivedEventHandler(terminalPort_DataReceived); txString = "$1RD\r"; txBytes = Encoding.UTF8.GetBytes(txString); terminalPort.Write(txBytes, 0, txBytes.Length); Debug.Print("Waiting for Signal ..."); if (ev.WaitOne(1000, false)) //waiting for notification, 1000 mSec timeout Debug.Print("RX String = " + rxString); else Debug.Print("WaitOne timed out"); //Do other error handling stuff terminalPort.Close(); } static void terminalPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { char rxChar; if (terminalPort.BytesToRead > 0) { rxChar = (char)terminalPort.ReadByte(); Debug.Print(rxChar.ToString()); rxString = rxString + rxChar.ToString(); Thread.Sleep(10); if (rxChar == '\r') ev.Set(); } } } }