void readEEPROM() { if (serialPort1.BytesToRead < 256) { return; } StreamWriter CSV = new StreamWriter("EEPROM_DATA.CSV", true); byte[] newBytes = new byte[256]; serialPort1.Read(newBytes, 0, 256); string Str = BitConverter.ToString(newBytes, 0, 256); richTextBox1.Invoke(new MethodInvoker(delegate() { richTextBox1.AppendText(Str + Environment.NewLine + Environment.NewLine); })); TelemetryPacket packet = new TelemetryPacket(); for (int i = 0; i <= 12; i++) { if (newBytes[21 * i] == 0x03 && newBytes[21 * i + 1] == 0xE9) { packet.team_id = 1001; packet.time = ((int)newBytes[21 * i + 2] << 8) + (int)newBytes[21 * i + 3]; packet.altitude = unchecked ((short)(((ushort)newBytes[21 * i + 4] << 8) + newBytes[21 * i + 5])); packet.outside_temp = (((int)newBytes[21 * i + 6] << 8) + (int)newBytes[21 * i + 7]) / 10; packet.inside_temp = (((int)newBytes[21 * i + 8] << 8) + (int)newBytes[21 * i + 9]) / 10; packet.voltage = unchecked ((ushort)(((ushort)newBytes[21 * i + 10] << 8) + newBytes[21 * i + 11])); packet.voltage = unchecked ((float)(((packet.voltage) / 65535) * (maxVoltage * voltageScale))); packet.state = packet.stateName((int)newBytes[21 * i + 12]); packet.x_accel = unchecked ((short)(((ushort)newBytes[21 * i + 13] << 8) + newBytes[21 * i + 14])); packet.x_accel = unchecked ((float)(packet.x_accel / 16384)); packet.y_accel = unchecked ((short)(((ushort)newBytes[21 * i + 15] << 8) + newBytes[21 * i + 16])); packet.y_accel = unchecked ((float)(packet.y_accel / 16384)); packet.z_accel = unchecked ((short)(((ushort)newBytes[21 * i + 17] << 8) + newBytes[21 * i + 18])); packet.z_accel = unchecked ((float)(packet.z_accel / 16384)); packet.count = ((int)newBytes[21 * i + 19] << 8) + (int)newBytes[21 * i + 20]; string[] packetStr = HandleData.packetToString(packet); string row = String.Format("{0, 8}, {1, 8}, {2, 8}, {3, 14}, {4, 14}, {5, 8}, {6, 6}, {7, 6}, {8, 6}, {9, 6}, {10, 6}", packetStr[0], packetStr[1], packetStr[2], packetStr[3], packetStr[4], packetStr[5], packetStr[6], packetStr[7], packetStr[8], packetStr[9], packetStr[10]); CSV.WriteLine(row); } } CSV.Close(); //MessageBox.Show("Done"); }
public static void addNewPacket(TelemetryPacket telemetry) { time.Add(telemetry.time); altitude.Add(telemetry.altitude); outside_temp.Add(telemetry.outside_temp); inside_temp.Add(telemetry.inside_temp); voltage.Add(telemetry.voltage); x_accel.Add(telemetry.x_accel); y_accel.Add(telemetry.y_accel); z_accel.Add(telemetry.z_accel); state = telemetry.state; }
void readTransmittedTelemetry() { if (serialPort1.BytesToRead <= packetSize) { return; } //StreamWriter rawDataFile = new StreamWriter("raw_packets.txt"); byte[] firstByte = new byte[4]; serialPort1.Read(firstByte, 0, 4); //rawDataFile.Write(firstByte); if (firstByte[0] != 0x7E || firstByte[1] != 0x00 || firstByte[2] != 0x21 || firstByte[3] != 0x90) { return; } byte[] rawPacket = new byte[packetSize - 4]; serialPort1.Read(rawPacket, 0, packetSize - 4); //rawDataFile.Write(rawPacket); //rawDataFile.Close(); var ms = new MemoryStream(new byte[packetSize], 0, packetSize, true, true); ms.Write(firstByte, 0, 4); ms.Write(rawPacket, 0, packetSize - 4); byte[] fullPacket = ms.GetBuffer(); //maybe use ToArray?? string packetStr = BitConverter.ToString(fullPacket, 0, packetSize); richTextBox1.Invoke(new MethodInvoker(delegate() { richTextBox1.AppendText(packetStr + Environment.NewLine); })); richTextBox1.Invoke(new MethodInvoker(delegate() { richTextBox1.ScrollToCaret(); })); TelemetryPacket receivedPacket = new TelemetryPacket(); parsePacket(ref receivedPacket, fullPacket); if (receivedPacket.time < 0) { return; } //reset graphs when leaving lest state; if (lastState.Equals("Test") && receivedPacket.state.Equals("Lauch")) { HandleData.clearData(); updateCharts(); } HandleData.addPacketToCSV(HandleData.packetToString(receivedPacket)); // add ToString method to TelemetryPacket HandleData.addNewPacket(receivedPacket); updateCharts(); lastState = receivedPacket.state; }
public static string[] packetToString(TelemetryPacket data) { string[] packetStr = new string[11]; packetStr[0] = data.team_id.ToString(); packetStr[1] = data.time.ToString(); packetStr[2] = data.altitude.ToString(); packetStr[3] = data.outside_temp.ToString(); packetStr[4] = data.inside_temp.ToString(); packetStr[5] = data.voltage.ToString(); packetStr[6] = data.state; packetStr[7] = data.x_accel.ToString(); packetStr[8] = data.y_accel.ToString(); packetStr[9] = data.z_accel.ToString(); packetStr[10] = data.count.ToString(); return(packetStr); }
void parsePacket(ref TelemetryPacket packet, byte[] fullData) { packet.team_id = ((int)fullData[15] << 8) + (int)fullData[16]; packet.time = ((int)fullData[17] << 8) + (int)fullData[18]; packet.altitude = unchecked ((short)(((ushort)fullData[19] << 8) + fullData[20])); packet.outside_temp = (((int)fullData[21] << 8) + (int)fullData[22]) / 10; packet.inside_temp = (((int)fullData[23] << 8) + (int)fullData[24]) / 10; packet.voltage = unchecked ((ushort)(((ushort)fullData[25] << 8) + fullData[26])); packet.voltage = unchecked ((float)(((packet.voltage) / 65536) * (maxVoltage * voltageScale))); packet.state = packet.stateName((int)fullData[27]); packet.x_accel = unchecked ((short)(((ushort)fullData[28] << 8) + fullData[29])); packet.x_accel = unchecked ((float)(packet.x_accel / 16384)); packet.y_accel = unchecked ((short)(((ushort)fullData[30] << 8) + fullData[31])); packet.y_accel = unchecked ((float)(packet.y_accel / 16384)); packet.z_accel = unchecked ((short)(((ushort)fullData[32] << 8) + fullData[33])); packet.z_accel = unchecked ((float)(packet.z_accel / 16384)); packet.count = ((int)fullData[34] << 8) + (int)fullData[35]; }