public static SensorMsg CreateSensorMessage(DemoSerialMsg msg) { SensorMsg sensorMsg = new SensorMsg(); sensorMsg.Timestamp = msg.Data[0] + ":" + msg.Data[1] + ":" + msg.Data[2] + "." + msg.Data[3]; sensorMsg.Pressure = convertIntToFloat(getInt(msg.Data, 4, 2), getInt(msg.Data, 6, 2)); sensorMsg.Temperature = convertIntToFloat(getInt(msg.Data, 8, 1), getInt(msg.Data, 9, 1)); sensorMsg.Humidity = convertIntToFloat(getInt(msg.Data, 10, 1), getInt(msg.Data, 11, 1)); sensorMsg.AccelerationX = getInt(msg.Data, 12, 4); sensorMsg.AccelerationY = getInt(msg.Data, 16, 4); sensorMsg.AccelerationZ = getInt(msg.Data, 20, 4); sensorMsg.GyroX = getInt(msg.Data, 24, 4); sensorMsg.GyroY = getInt(msg.Data, 28, 4); sensorMsg.GyroZ = getInt(msg.Data, 32, 4); sensorMsg.MagnetoX = getInt(msg.Data, 36, 4); sensorMsg.MagnetoY = getInt(msg.Data, 40, 4); sensorMsg.MagnetoZ = getInt(msg.Data, 44, 4); return sensorMsg; }
private void outputSerialMessage(DemoSerialMsg msg) { if (msg.Data.Length < 1) { // パリティデータなしの不正なメッセージ this.txtConsole.AppendText("[Invalid data] Parity data is not exist.\n"); this.txtConsole.AppendText(msg + "\n"); return; } if (CheckDemoParity(msg) == false) { // パリティチェックエラー this.txtConsole.AppendText("[Invalid data] Parity check error.\n"); this.txtConsole.AppendText(msg + "\n"); return; } if (msg.CmdType == 1 && msg.CmdStatus == 8) { if (msg.Data.Length != 49) { this.txtConsole.AppendText("[Invalid data] Data length is " + msg.Data.Length + ".\n"); this.txtConsole.AppendText(msg + "\n"); return; } // センサデータ SensorMsg sensorMsg = SensorMsg.CreateSensorMessage(msg); this.txtConsole.AppendText(sensorMsg + "\n"); uploader.UpdateSensorMessage(sensorMsg); System.Console.WriteLine(sensorMsg.Humidity); //System.Console.WriteLine(sensorMsg.Temperature); } else { // unknown this.txtConsole.AppendText("[Unknown Message]\n"); this.txtConsole.AppendText(msg + "\n"); } }
private void ReceiveData(int len) { bool receivedEof = false; for (int i = 0; i < len; i++) { queue.Enqueue(readBuf[i]); if (readBuf[i] == MSG_EOF) { receivedEof = true; } } if (receivedEof) { try { DemoSerialMsg msg = new DemoSerialMsg(); msg.CmdType = dequeueProtocol(queue).Value; msg.DevAddr = dequeueProtocol(queue).Value; msg.CmdStatus = dequeueProtocol(queue).Value; List<byte> bytes = new List<byte>(); while (true) { byte? d = dequeueProtocol(queue); if (d == null) break; bytes.Add(d.Value); } msg.Data = bytes.ToArray(); outputSerialMessage(msg); } catch (InvalidOperationException e) { this.txtConsole.AppendText("[Dequeue Invalid] " + e.Message + "\n"); } } }
// パリティチェック private bool CheckDemoParity(DemoSerialMsg msg) { byte check = 0; byte parity = msg.Data[msg.Data.Length - 1]; check -= msg.CmdType; check -= msg.DevAddr; check -= msg.CmdStatus; for (int i = 0; i < (msg.Data.Length - 1); i++) { check -= msg.Data[i]; } if (check == parity) { return true; } return false; }