using System; using Microsoft.SPOT; using System.Text; using System.Threading; using System.IO.Ports; using Microsoft.SPOT.Hardware; using Microsoft.SPOT.IO; using GHI.Hardware.EMX; namespace miniCPF { class SBE41 { private static OutputPort DR = new OutputPort(Pin.IO15, false); private static OutputPort SPM = new OutputPort(Pin.IO46, true); private static OutputPort serialTX; private static double pressure = Double.NaN; private static byte[] pressureBytes = new byte[16]; private static int pressureBytesIndex = 0; private static int portBytesToRead; private static byte[] portReadBytes = new byte[1024]; private static byte[] portInBytes = new byte[240]; private static int portInBytesIndex = -1; private static int messageNum; private static SerialPort SBE41Port = new SerialPort("com4", 9600, Parity.None, 8, StopBits.One); private static Timer SBE41Timer = null; private static int timerState = 0; private static StringBuilder sbTemp = new StringBuilder(256); private static Object SBE41Lock = new Object(); private static int parserI = 0; private static byte token = 32; //token (separator) = space private static byte CR = 13; private static byte LF = 10; private static byte comma = 44; private static int inArrayLength = 0; private static byte[] header = UTF8Encoding.UTF8.GetBytes("CTD "); private static byte[] message = new byte[256]; public static void Init() { SBE41Port.DataReceived += new SerialDataReceivedEventHandler(portDataReceivedHandler); //Initialize(); SBE41Port.ReadTimeout = 5000; SBE41Port.WriteTimeout = 20; SBE41Port.Open(); //ReleaseWakeUp(); DR.Write(false); SPM.Write(true); SBE41Timer = new Timer(new TimerCallback(SBE41TimerCallback), null, 500, 2000); //Stop the timer by set SBE41Timer = null Array.Copy(header, message, header.Length); } private static void SBE41TimerCallback(object state) { switch (timerState) { case 0: SBE41Port.Close(); serialTX = new OutputPort(Pin.IO6, true); DR.Write(true); SPM.Write(false); // Debug.Print(DateTime.Now + "." + DateTime.Now.TimeOfDay.Milliseconds.ToString() + " SBE41 State 0 DR = true SM = false TX = true"); timerState = 1; SBE41Timer.Change(0, 50); break; case 1: DR.Write(false); // Debug.Print(DateTime.Now + "." + DateTime.Now.TimeOfDay.Milliseconds.ToString() + " SBE41 State 1 DR = false SM = false TX = true"); timerState = 2; SBE41Timer.Change(0, 150); break; case 2: serialTX.Write(false); // Debug.Print(DateTime.Now + "." + DateTime.Now.TimeOfDay.Milliseconds.ToString() + " SBE41 State 2 DR = false SM = false TX = false"); timerState = 3; SBE41Timer.Change(0, 150); break; case 3: SPM.Write(true); serialTX.Write(true); //Debug.Print(DateTime.Now + "." + DateTime.Now.TimeOfDay.Milliseconds.ToString() + " SBE41 State 3 DR = false SM = true TX = true"); timerState = 4; SBE41Timer.Change(0, 50); break; case 4: serialTX.Dispose(); SBE41Port.Open(); //Debug.Print(DateTime.Now + "." + DateTime.Now.TimeOfDay.Milliseconds.ToString() + " SBE41 State 3 DR = false SM = true TX = true"); timerState = 0; SBE41Timer.Change(0, 1600); break; } } private static void portDataReceivedHandler(object sender, SerialDataReceivedEventArgs e) { portBytesToRead = SBE41Port.BytesToRead; try { SBE41Port.Read(portReadBytes, 0, portBytesToRead); } catch { Debug.Print("SBE41 Read Exception"); //TODO do something smarter here } for (int i = 0; i < portBytesToRead; i++) { if (portInBytesIndex == -1) //Check to make sure first character is = LF { if (portReadBytes[i] == LF) //only move on to byte index 1 after we get a LF { portInBytesIndex = portInBytesIndex + 1; } } else { portInBytes[portInBytesIndex] = portReadBytes[i]; if (portInBytes[portInBytesIndex] == CR) { messageNum = messageNum + 1; Array.Copy(portInBytes, 1, message, header.Length, portInBytesIndex); lock (SBE41Lock) { Program.MessageQueue.Enqueue(message); } Array.Clear(portReadBytes, 0, portReadBytes.Length); Array.Clear(portInBytes, 0, portInBytes.Length); portInBytesIndex = -1; } else { portInBytesIndex = portInBytesIndex + 1; } } } } public static double parse(byte[] inArray) { inArrayLength = Array.IndexOf(inArray, 0); parserI = Array.IndexOf(inArray, token); while ((inArray[parserI] != CR) && (inArray[parserI] != comma) && (parserI < inArrayLength)) { pressureBytes[pressureBytesIndex] = inArray[parserI]; pressureBytesIndex = pressureBytesIndex + 1; parserI = parserI + 1; } sbTemp.Clear(); sbTemp.Append(UTF8Encoding.UTF8.GetChars(pressureBytes)); pressure = double.Parse(sbTemp.ToString()); Array.Clear(pressureBytes, 0, pressureBytes.Length); pressureBytesIndex = 0; return (pressure); } } }