protected virtual void OnUpdated(MindwaveEventArgs e) { if (Updated != null) { Updated(this, e); } }
/// <summary> /// Main loop /// </summary> private void ContinuousRead() { while (true) { // Synchronize on SYNC bytes Command c1 = (Command)serialPort.ReadByte(); if (c1 != Command.Sync) { continue; } Command c2 = (Command)serialPort.ReadByte(); if (c2 != Command.Sync) { continue; } int pLength = 0; while (true) { pLength = serialPort.ReadByte(); if (pLength != 170) { break; } } if (pLength > 169) continue; // read payload while (serialPort.BytesToRead < pLength) { Thread.Sleep(1); } serialPort.Read(payload, 0, pLength); // calcurate checksum int checksum = 0; for (int i = 0; i < pLength; i++) { checksum += payload[i]; } checksum ^= 0xff; checksum &= 0xff; int c = serialPort.ReadByte(); if (c != checksum) { continue; } // Parse Payload bool completed = ParsePayload(payload, pLength); if (completed) { MindwaveEventArgs e = new MindwaveEventArgs(); e.Data = signal; OnUpdated(e); signal = new Signal(); } } }
/// <summary> /// Main loop /// </summary> private void ContinuousRead() { while (true) { // Synchronize on SYNC bytes Command c1 = (Command)serialPort.ReadByte(); if (c1 != Command.Sync) { continue; } Command c2 = (Command)serialPort.ReadByte(); if (c2 != Command.Sync) { continue; } int pLength = 0; while (true) { pLength = serialPort.ReadByte(); if (pLength != 170) { break; } } if (pLength > 169) { continue; } // read payload while (serialPort.BytesToRead < pLength) { Thread.Sleep(1); } serialPort.Read(payload, 0, pLength); // calcurate checksum int checksum = 0; for (int i = 0; i < pLength; i++) { checksum += payload[i]; } checksum ^= 0xff; checksum &= 0xff; int c = serialPort.ReadByte(); if (c != checksum) { continue; } // Parse Payload bool completed = ParsePayload(payload, pLength); if (completed) { MindwaveEventArgs e = new MindwaveEventArgs(); e.Data = signal; OnUpdated(e); signal = new Signal(); } } }