using System; using System.Threading; using System.IO.Ports; using System.Text; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; namespace MFConsoleApplication1 { public static class Console { private static char ConsoleTerminatingChar = '\n'; private static string ConsoleRxString = ""; public const String BeginCmd = "BG", UploadCmd = "UP", ExitCmd = "EX", Error = "ERROR = "; private static SerialPort ConsolePort = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One); private static AutoResetEvent ConsoleReadEvent = new AutoResetEvent(false); public static int initSerialPort(String PortName) { ConsolePort.ReadTimeout = 1000; ConsolePort.Open(); ConsolePort.DiscardInBuffer(); ConsolePort.DiscardOutBuffer(); ConsolePort.DataReceived += new SerialDataReceivedEventHandler(ConsolePort_DataReceived); ConsolePort.ErrorReceived += new SerialErrorReceivedEventHandler(ConsolePort_ErrorReceived); return (0); } public static int initConsole(String PortName) { initSerialPort(PortName); return (0); //Eventually deal with errors } public static String readCommand() { byte[] ConsoleTxBytes = { }; string consoleCmd = ""; string consolePrompt = "BG to begin, UP to upload, EX to exit\r\n"; int errorCode = 0; ConsoleTxBytes = Encoding.UTF8.GetBytes(consolePrompt); ConsolePort.Write(ConsoleTxBytes, 0, ConsoleTxBytes.Length); EngrLog.writeEvent(Program.DateTimeNowMilliSec() + " ,Waiting for Console command"); if (ConsoleReadEvent.WaitOne(600000, false)) //waiting for notification, 10 minute timeout { consoleCmd = ConsoleRxString; } else { consoleCmd = Error + errorCode.ToString(); EngrLog.writeEvent(Program.DateTimeNowMilliSec() + " ,Console RD timed out"); } return (consoleCmd); } static public void InvalidCommandReceived(String lineToSend) { byte[] ConsoleTxBytes = { }; ConsoleTxBytes = Encoding.UTF8.GetBytes(lineToSend); ConsolePort.Write(ConsoleTxBytes, 0, ConsoleTxBytes.Length); } static public void sendLine(String lineToSend) { byte[] ConsoleTxBytes = { }; ConsoleTxBytes = Encoding.UTF8.GetBytes(lineToSend); ConsolePort.Write(ConsoleTxBytes, 0, ConsoleTxBytes.Length); } static void ConsolePort_DataReceived(object sender, SerialDataReceivedEventArgs e) { char rxChar; while (ConsolePort.BytesToRead > 0) { rxChar = (char)ConsolePort.ReadByte(); Debug.Print(rxChar.ToString()); ConsoleRxString = ConsoleRxString + rxChar.ToString(); if (rxChar == ConsoleTerminatingChar) ConsoleReadEvent.Set(); } } static void ConsolePort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e) { //Eventually } } } /* using System; using Microsoft.SPOT; namespace MFConsoleApplication1 { static class Console { public enum Commands : int { Error, Begin, Upload, Exit, Null, } static public Commands WaitForCommand() { bool error = false; if (!error) return(Commands.Begin); else return(Commands.Error); } } } */