using System; using Microsoft.SPOT; using System.Threading; using System.IO.Ports; using System.Text; namespace CPF { class IModem { public enum ReturnCodes : int { none = 0, CSQ, SBDWT, SBDIX, } public static int returnCode; public static SerialPort iModemPort = new SerialPort("COM2", 19200, Parity.None, 8, StopBits.One); private static object iModemLock = new object(); public static void init() { Array.Clear(messageBytes, 0, messageBytes.Length); Array.Clear(bytesRead, 0, bytesRead.Length); qS.byteArray = new byte[StructQueue.byteArrayWidth]; iModemPort.ReadTimeout = 5000; iModemPort.Open(); iModemPort.DiscardInBuffer(); iModemPort.DiscardOutBuffer(); iModemPort.DataReceived += new SerialDataReceivedEventHandler(iModemDataReceived); sendD0(); Thread.Sleep(1000); sendK0(); Thread.Sleep(1000); sendSBDD2(); Thread.Sleep(1000); } private static byte[] txBytes = new byte[1024]; private const string a3laCSQ = "AT+CSQ\r"; public static void sendCSQ() { //Debug.Print(DateTime.Now + " Sending CSQ"); Array.Clear(txBytes, 0, txBytes.Length); Array.Copy(UTF8Encoding.UTF8.GetBytes(a3laCSQ), txBytes, a3laCSQ.Length); iModemPort.Write(txBytes, 0, a3laCSQ.Length); } private const string a3laD0 = "AT&D0\r"; public static void sendD0() { //Debug.Print(DateTime.Now + " Sending D0"); Array.Clear(txBytes, 0, txBytes.Length); Array.Copy(UTF8Encoding.UTF8.GetBytes(a3laD0), txBytes, a3laD0.Length); iModemPort.Write(txBytes, 0, a3laD0.Length); } private const string a3laK0 = "AT&K0\r"; public static void sendK0() { //Debug.Print(DateTime.Now + " Sending K0"); Array.Clear(txBytes, 0, txBytes.Length); Array.Copy(UTF8Encoding.UTF8.GetBytes(a3laK0), txBytes, a3laK0.Length); iModemPort.Write(txBytes, 0, a3laK0.Length); } private static byte[] sbdwtHeader = new byte[9] { (byte)65, (byte)84, (byte)43, (byte)83, (byte)66, (byte)68, (byte)87, (byte)84, (byte)13 }; // AT+SBDWT private static readonly StringBuilder sbTemp = new StringBuilder(1024); public static void sendSBDWT(byte[] inBytes) { //Debug.Print(DateTime.Now + " Sending SBDWT"); iModemPort.Write(sbdwtHeader, 0, sbdwtHeader.Length); Thread.Sleep(2000); sbTemp.Clear(); sbTemp.Append(UTF8Encoding.UTF8.GetChars(inBytes)); //Debug.Print(DateTime.Now + " Sending:" + sbTemp.ToString()); iModemPort.Write(inBytes, 0, Array.IndexOf(inBytes, 0)); } private static readonly StringBuilder sbSBDD2 = new StringBuilder("AT+SBDD2\r"); public static void sendSBDD2() { //Debug.Print(DateTime.Now + " Sending SBDD2"); iModemPort.Write(UTF8Encoding.UTF8.GetBytes(sbSBDD2.ToString()), 0, sbSBDD2.Length); } private static readonly StringBuilder sbSBDI = new StringBuilder("AT+SBDI\r"); public static void sendSBDI() { //Debug.Print(DateTime.Now + " Sending SBDI"); Array.Clear(txBytes, 0, txBytes.Length); Array.Copy(UTF8Encoding.UTF8.GetBytes(sbSBDI.ToString()), txBytes, sbSBDI.Length); iModemPort.Write(txBytes, 0, sbSBDI.Length); } private static readonly StringBuilder sbSBDIX = new StringBuilder("AT+SBDIX\r"); public static void sendSBDIX() { //Debug.Print(DateTime.Now + " Sending SBDIX"); Array.Clear(txBytes, 0, txBytes.Length); Array.Copy(UTF8Encoding.UTF8.GetBytes(sbSBDI.ToString()), txBytes, sbSBDI.Length); iModemPort.Write(txBytes, 0, sbSBDI.Length); } public static void parseSQ(byte[] inBytes) { if ((inBytes[0] == 43) && (inBytes[1] == 67) && (inBytes[2] == 83) && (inBytes[3] == 81)) Program.signalQuality = (int)inBytes[5] - 48; } private static byte LF = 10; private static byte[] bytesRead = new byte[2048]; public static byte[] messageBytes = new byte[2048]; private static int bytesToRead; private static int messageIndex = 0; private static StructQueue.qStruct qS = new StructQueue.qStruct(); private static Object IModemLock = new Object(); private static void iModemDataReceived(object sender, SerialDataReceivedEventArgs e) { bytesToRead = iModemPort.BytesToRead; try { iModemPort.Read(bytesRead, 0, bytesToRead); } catch { Debug.Print("Iridium Modem Read Exception"); //TODO do something smarter here } for (int i = 0; i < bytesToRead; i++) { messageBytes[messageIndex] = bytesRead[i]; if (messageBytes[messageIndex] == LF) { qS.timeStamp = DateTime.Now; qS.state = Program.CPFState; //sbTemp.Clear(); //sbTemp.Append(UTF8Encoding.UTF8.GetChars(messageBytes, 0, messageIndex - 1)); //Debug.Print(DateTime.Now + " iModem Message = " + sbTemp.ToString()); Array.Clear(qS.byteArray, 0, qS.byteArray.Length); Array.Copy(messageBytes, qS.byteArray, messageIndex + 1); qS.qRecordType = StructQueue.QRecordType.IModem; lock (IModemLock) { Program.sQueue.Enqueue(qS); } messageIndex = 0; Array.Clear(messageBytes, 0, messageBytes.Length); returnCode = 0; } else { messageIndex = messageIndex + 1; } } } } }