From 71682decf0312a0f1ad67b55131849b102a931e6 Mon Sep 17 00:00:00 2001 From: The6P4C Date: Fri, 29 Sep 2017 11:17:30 +1000 Subject: [PATCH] Ensure ETX byte is preceded by a DLE byte when using the ETX as an end of packet marker --- ThunderboltTimeSync/ThunderboltSerialPort.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ThunderboltTimeSync/ThunderboltSerialPort.cs b/ThunderboltTimeSync/ThunderboltSerialPort.cs index 1d262e4..abf5bc3 100644 --- a/ThunderboltTimeSync/ThunderboltSerialPort.cs +++ b/ThunderboltTimeSync/ThunderboltSerialPort.cs @@ -86,8 +86,6 @@ namespace ThunderboltTimeSync { bool inStuffedDLE = false; foreach (byte b in data) { - Debug.WriteLine(string.Format("{0:X2}", b)); - if (b == CHAR_DLE) { if (!inStuffedDLE) { newData.Add(b); @@ -141,7 +139,8 @@ namespace ThunderboltTimeSync { // Check buffer length to ensure we've reached a plausible end of packet. // 5 bytes is [DLE]<1 byte of data>[DLE][ETX] - if (currentByte == CHAR_ETX && packetBuffer.Count >= 5) { + // Must check if previous character is a [DLE], otherwise an ETX with a malformed and unstuffed [DLE] will cause issues + if (currentByte == CHAR_ETX && packetBuffer.Count >= 5 && packetBuffer[packetBuffer.Count - 2] == CHAR_DLE) { int numberOfPrecedingDLEs = 0; // Count number of DLEs, excluding the first two bytes (initial DLE and id)