// /// Begins processing serial data and firing PacketReceived events.
// /// </summary>
// public void Open() {
// running = true;
// serialPort.Open();
// readThread.Start();
// }
// /// <summary>
// /// Stops processing serial data and firing PacketReceived events.
// /// </summary>
// public void Close() {
// running = false;
// readThread.Join();
// serialPort.Close();
// }
// /// <summary>
// /// Converts a stuffed byte list (one where all 0x10 bytes are replaced with 0x10 0x10) into an unstuffed byte list.
// /// </summary>
// /// <param name="data">A reference to the list containing the data to be unstuffed.</param>
// /// <returns>True if the unstuffing was successful, false otherwise.</returns>
// private bool Unstuff(ref List<byte> data) {
// List<byte> newData = new List<byte>();
// bool inStuffedDLE = false;
// foreach (byte b in data) {
// if (b == CHAR_DLE) {
// if (!inStuffedDLE) {
// newData.Add(b);
// inStuffedDLE = true;
// } else {
// // If we see a DLE after we've already seen one (inStuffedDLE == true), we don't need to add the byte to the list because we already did when the first byte was encountered
// // Just set the flag to false so that if this stuffed DLE is immediately followed by another, it will be correctly parsed
// inStuffedDLE = false;
// }
// } else {
// if (inStuffedDLE) {
// return false;
// }
// newData.Add(b);
// }
// }
// data = newData;
// return true;
// }
// private void ProcessPacket() {
// byte id = packetBuffer[1];
// // Grab only the data - not the first [DLE]<id> or the last [DLE][ETX]
// List<byte> data = packetBuffer.GetRange(2, packetBuffer.Count - 4);
// bool isPacketValid = Unstuff(ref data);
// ThunderboltPacket packet;
// if (isPacketValid) {
// packet = new ThunderboltPacket(isPacketValid, id, data, packetBuffer);
// } else {
// packet = new ThunderboltPacket(isPacketValid, 0, new List<byte>(), new List<byte>());
// }
// PacketReceived?.Invoke(packet);
// }
// // TODO: Improve this? Currently, if a malformed packet comes in it can take many more packets (totalling up to almost 5 to 10 seconds of time)
// // to bring the decoder back into sync with the true packets. It's probably only an issue when the Thunderbolt is initially plugged in,
// // as that's probably the only time we'd see malformed packets on the serial port, since we could connect in the middle of a packet.
// // Consider if this is really an issue, and think of a better way of decoding the packets to avoid this.
// private void ProcessByte(byte b) {
// // There aren't any packets this long, but during a corrupted or malformed packet the buffer can get quite large before the error
// // is fixed somehow. To prevent the almost 20 second delay between time packets that can be caused by some malformed packets,
// // just reset everything if the buffer's getting too long.
// // We should make sure the user knows, so send a invalid packet to them.
// if (packetBuffer.Count >= 128) {
// packetBuffer.Clear();
// inPacket = false;
// PacketReceived?.Invoke(new ThunderboltPacket(false, 0, new List<byte>(), new List<byte>()));
// }
// if (inPacket) {
// packetBuffer.Add(b);
// // Check buffer length to ensure we've reached a plausible end of packet.
// // 5 bytes is [DLE]<id><1 byte of data>[DLE][ETX]
// // Must check if previous character is a [DLE], otherwise an ETX with a malformed and unstuffed [DLE] will cause issues