using System; using Microsoft.SPOT; using System.Threading; using System.IO.Ports; using System.Text; using Microsoft.SPOT.Hardware; namespace CPF { class Optode { public static SerialPort optodePort = new SerialPort("COM6", 9600, Parity.None, 8, StopBits.One); private static byte[] txBytes = new byte[64]; private static int messageIndex = 0; private static int numPortBytes = 0; private static object optodeLock = new object(); private static StructQueue.qStruct qS = new StructQueue.qStruct(); //MEASUREMENT 4330 1532 // O2Concentration[uM] 2.644894E+02 AirSaturation[%] 9.722411E+01 // Temperature[Deg.C] 2.217678E+01 CalPhase[Deg] 2.919595E+01 // TCPhase[Deg] 2.919595E+01 C1RPh[Deg] 3.770208E+01 // C2RPh[Deg] 8.506126E+00 C1Amp[mV] 8.645895E+02 // C2Amp[mV] 9.500998E+02 RawTemp[mV] 1.244538E+02 public enum OptodeSampleFields : int { O2Conc = 0, AirSat, TOpt, CalPhase, TCPhase, C1RPh, C2RPh, C1Amp, C2Amp, RawTemp } public static void init() { qS.byteArray = new byte[StructQueue.qRecordWidth]; optodePort.ReadTimeout = 5000; optodePort.Open(); optodePort.DiscardInBuffer(); optodePort.DiscardOutBuffer(); optodePort.DataReceived += new SerialDataReceivedEventHandler(optodeDataReceived); wakeUp(); Thread.Sleep(2000); } private static StringBuilder wakeUpCmd = new StringBuilder("\r"); public static void wakeUp() { //Debug.Print("Sending Wake Up"); Array.Clear(txBytes, 0, txBytes.Length); Array.Copy(Encoding.UTF8.GetBytes(wakeUpCmd.ToString()), txBytes, wakeUpCmd.Length); optodePort.Write(txBytes, 0, wakeUpCmd.Length); } private const string doSampleCmd = "Do Sample\r\n"; private const string doSampleHeader = "Sending Optode Do Sample"; public static void doSample() { //Debug.Print("Sending Optode wakeup and Do Sample"); Array.Clear(qS.byteArray, 0, qS.byteArray.Length); Array.Copy(UTF8Encoding.UTF8.GetBytes(doSampleHeader), qS.byteArray, doSampleHeader.Length); qS.state = Program.CPFState; qS.qRecordType = StructQueue.QRecordType.Optode; qS.timeStamp = DateTime.Now; lock (optodeLock) { Program.sQueue.Enqueue(qS); } wakeUp(); Thread.Sleep(500); Array.Clear(txBytes, 0, txBytes.Length); Array.Copy(Encoding.UTF8.GetBytes(doSampleCmd.ToString()), txBytes, doSampleCmd.Length); optodePort.Write(txBytes, 0, doSampleCmd.Length); } private const string getAllCmd = "get all\r\n"; public static void getAll() { //Debug.Print("Sending Get All"); Array.Clear(txBytes, 0, txBytes.Length); Array.Copy(Encoding.UTF8.GetBytes(getAllCmd), txBytes, getAllCmd.Length); optodePort.Write(txBytes, 0, getAllCmd.Length); } private static bool endGetAll = false; private static int lineNum = 0; private static StringBuilder sbTemp = new StringBuilder(4096); private static byte[] getAllPortBytes = new byte[4096]; private static byte[] getAllMsgBytes = new byte[4096]; public static void getAllWithRead() { optodePort.DiscardInBuffer(); optodePort.DiscardOutBuffer(); //Debug.Print("Sending Get All"); Array.Clear(txBytes, 0, txBytes.Length); Array.Copy(Encoding.UTF8.GetBytes(getAllCmd.ToString()), txBytes, getAllCmd.Length); optodePort.Write(txBytes, 0, getAllCmd.Length); Thread.Sleep(1000); sbTemp.Clear(); Array.Clear(getAllMsgBytes, 0, messageBytes.Length); Array.Clear(getAllPortBytes, 0, portBytes.Length); messageIndex = 0; endGetAll = false; while (!endGetAll) { numPortBytes = optodePort.BytesToRead; try { optodePort.Read(getAllPortBytes, 0, numPortBytes); } catch { Debug.Print("Optode Port Get All Read Exception"); //Probably should do something smarter here } for (int i = 0; i < numPortBytes; i++) { if (getAllPortBytes[i] != LF) { if ((messageIndex == 0) && (getAllPortBytes[i] == '#')) { endGetAll = true; } else { getAllMsgBytes[messageIndex] = getAllPortBytes[i]; messageIndex = messageIndex + 1; } } else { sbTemp.Append(UTF8Encoding.UTF8.GetChars(getAllMsgBytes, 0, (messageIndex - 1))); lock (optodeLock) { EngrLogger.writeToColumns(EngrLogger.ColumnNums.comment, EngrLogger.getPrefix(sbTemp)); } sbTemp.Clear(); messageIndex = 0; lineNum = lineNum + 1; } } } optodePort.DiscardInBuffer(); optodePort.DiscardOutBuffer(); } private const int ehBufferSize = 2048; private static byte[] portBytes = new byte[ehBufferSize]; private static byte[] messageBytes = new byte[ehBufferSize]; private static byte[] errorHeader = new byte[5]{(byte)69, (byte)114, (byte)114, (byte)111, (byte)114}; private static byte LF = 10; //End of text = LF private static void optodeDataReceived(object sender, SerialDataReceivedEventArgs e) { numPortBytes = optodePort.BytesToRead; if ( (numPortBytes + messageIndex + 50) < ehBufferSize) { try { optodePort.Read(portBytes, 0, numPortBytes); } catch { Debug.Print("Optode Port Read Exception"); //Probably should do something smarter here } for (int i = 0; i < numPortBytes; i++) { if (portBytes[i] != LF) { messageBytes[messageIndex] = portBytes[i]; messageIndex = messageIndex + 1; } else { Array.Clear(qS.byteArray, 0, qS.byteArray.Length); Array.Copy(messageBytes, qS.byteArray, (messageIndex - 1)); qS.timeStamp = DateTime.Now; qS.state = Program.CPFState; qS.qRecordType = StructQueue.QRecordType.Optode; lock (optodeLock) { Program.sQueue.Enqueue(qS); } messageIndex = 0; } } } else { ErrorHandler.incrOptodeEHErrors(); Array.Clear(qS.byteArray, 0, qS.byteArray.Length); Array.Copy(errorHeader, qS.byteArray, errorHeader.Length); Array.Copy(messageBytes, qS.byteArray, (messageIndex - 1)); qS.timeStamp = DateTime.Now; qS.state = Program.CPFState; qS.qRecordType = StructQueue.QRecordType.Optode; lock (optodeLock) { Program.sQueue.Enqueue(qS); } } } } }