//public static void Main() public static void Main() { SerialPort port = new SerialPort("COM7", 115200, Parity.None, 8, StopBits.One); SerialPortInterface portInterface = new SerialPortInterface(port); portInterface.Open(); port.DiscardInBuffer(); while (true) { while (portInterface.ReadByte() != 'S') { } float sintime = portInterface.ReadShort(); float sinval = portInterface.ReadFloat(); float fsintime = portInterface.ReadShort(); float fsinval = portInterface.ReadFloat(); Console.WriteLine(String.Format("{0},{1},{2},{3}", sintime, sinval, fsintime, fsinval)); /* float accx = portInterface.ReadFloat(); float accy = portInterface.ReadFloat() * (float)(180.0f / Math.PI); float accz = portInterface.ReadFloat() * (float)(180.0f / Math.PI); float magx = portInterface.ReadFloat(); float magy = portInterface.ReadFloat(); float magz = portInterface.ReadFloat(); Console.WriteLine(String.Format("{0},{1},{2},{3},{4},{5}", accx, accy, accz, magx, magy, magz)); */ } }
//Test reading GPS. public static void Main6() { //SerialPort port = new SerialPort("COM7", 9600, Parity.None, 8, StopBits.One); //SerialPort port = new SerialPort("COM7", 250000, Parity.None, 8, StopBits.One); SerialPort port = new SerialPort("COM12", 57600, Parity.None, 8, StopBits.One); SerialPortInterface portInterface = new SerialPortInterface(port); portInterface.Open(); GraphForm lat = new GraphForm("Latitude"); GraphForm lon = new GraphForm("Longitude"); lat.Visible = true; lon.Visible = true; byte var = 0; int counter = 0; byte[] bytes = new byte[80]; port.DiscardInBuffer(); while (true) { List<byte> bts = new List<byte>(); while (true) { // bts.Add(portInterface.ReadByte()); /* byte[] buff = new byte[34]; for (int i = 0; i < 34; i++) { buff[i] = portInterface.ReadByte(); } int a = 0; a++; */ /** * This is needed to prevent getting out of synch. After a restart * the system could have already transmitted half of the data, so the first message * after a restart will be junk. Then this will snychronize the messages after the first * transmit. */ while (portInterface.ReadByte() != 'T') { } //byte bb = 0; //bb = portInterface.ReadByte(); //bb = portInterface.ReadByte(); //bb = portInterface.ReadByte(); int latitude = portInterface.ReadInt(); int longitude = portInterface.ReadInt(); int positionAcc = portInterface.ReadInt(); short fixStatus = portInterface.ReadShort(); int x = portInterface.ReadInt(); int y = portInterface.ReadInt(); int z = portInterface.ReadInt(); int b = 0; Console.WriteLine("lat: " + latitude.ToString() + " long: " + longitude.ToString() + " pos acc " + positionAcc.ToString() + " fixStatus " + fixStatus.ToString() + " x: " + x.ToString() + " y: " + y.ToString() + " z " + z.ToString()); } } // /* // bytes = new byte[80]; // for (int i = 0; i < 80; i++) // { // var = portInterface.ReadByte(); // bytes[i] = var; // } // byte b = 2; // */ // /* // var = portInterface.ReadByte(); // if (var == 'S') // { // counter = 0; // System.Diagnostics.Debug.WriteLine("Counter " + (bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3])); // } // else if (var == 'B') // { // System.Diagnostics.Debug.WriteLine("Timeout " + ((int)portInterface.ReadByte()).ToString()); // } // else // { // bytes[counter++] = var; // } // * */ // counter++; // /*byte[] bytes2 = new byte[28]; // for (int i = 0; i < 28; i++) // { // var = portInterface.ReadByte(); // bytes2[i] = var; // } // int b = 3; // System.Diagnostics.Debug.WriteLine("Counter " + (bytes2[13] << 24 | bytes2[12] << 16 | bytes2[11] << 8 | bytes2[10])); // */ // /* // bytes [counter++] = var; // if (counter == 3) // { // counter = 0; // System.Diagnostics.Debug.WriteLine("Counter " + (bytes[0] <<24 | bytes[1] <<16 | bytes[2] << 8 | bytes[3])); // } // */ // /* // if (var == 0xB5) // { // System.Diagnostics.Debug.WriteLine("Counter" + counter); // counter = 1; // } // if (counter == 11 | counter == 12 | counter == 13 | counter == 14) // { // bytes[14 - counter] = var; // bytes[counter - 11] = var; // } // if (counter == 15) // { // System.Diagnostics.Debug.WriteLine("Number" + BitConverter.ToInt32(bytes,0)); // } // System.Diagnostics.Debug.Write((char)var); // */ // /* // if (var == 'S') // { // int bb = portInterface.ReadInt(); // System.Diagnostics.Debug.WriteLine(" Int: " + bb.ToString()); // }*/ // } // while (var != 'S') // { // var = portInterface.ReadByte(); // } // Note:The status value might come back as a large value because after restart, the value S gets sent as one of the bytes ruining the synchronization // /* // Read raw lat/long // System.Diagnostics.Debug.WriteLine(portInterface.ReadInt(portInterface)); // int rwLat = portInterface.ReadInt(portInterface); // lat.AddValueToGraph(rwLat); // int rwLon = portInterface.ReadInt(portInterface); // lon.AddValueToGraph(rwLon);*/ // Read raw messages // var = 0; // short status = portInterface.ReadShort(); // System.Diagnostics.Debug.WriteLine("status: " + status); // status // while (var != 'S') // { // var = portInterface.ReadByte(); // System.Diagnostics.Debug.Write((char)var); // } // Read xned // System.Diagnostics.Debug.WriteLine(portInterface.ReadFloat().ToString()); // Application.DoEvents(); //} }