public bool TryReadSerialHeader(SerialPort serial, ref SerialHeader header) { if (!serial.IsNotNullAndOpened()) { return(false); } var buf = new byte[SerialHeaderDef.HeaderLength]; if (serial.Read(buf, 0, SerialHeaderDef.HeaderLength) != SerialHeaderDef.HeaderLength) { return(false); } header = new SerialHeader(buf); return(true); }
void SerialRecieveLoop() { SerialHeader header = default; AxisOrangeData data = default; AxisOrangeButton button = default; while (serialDevice.IsNotNullAndOpened()) { // serial read if (serialDevice.BytesToRead >= SerialHeaderDef.HeaderLength && serialReader.TryReadSerialHeader(serialDevice, ref header)) { if (header.dataId == SerialHeaderDef.ImuDataId) { if (serialReader.TryReadImuData(serialDevice, header.dataLength, ref data)) { OnSensorDataUpdate.Invoke(data); } } else if (header.dataId == SerialHeaderDef.ButtonDataId) { if (serialReader.TryReadButtonData(serialDevice, header.dataLength, ref button)) { OnSensorButtonUpdate.Invoke(button); } } else { // Do Nothing } } // finish loop? if (isListening) { System.Threading.Thread.Sleep(1); } else { break; } } }