From f89665c7e991e35b905aeff68b107dc36f9f106d Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Tue, 4 May 2021 12:53:54 -0400 Subject: [PATCH] Serial handle newlines better Closes #795 --- src/qz/communication/SerialIO.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/qz/communication/SerialIO.java b/src/qz/communication/SerialIO.java index 365e786ce..cad5ecf0b 100644 --- a/src/qz/communication/SerialIO.java +++ b/src/qz/communication/SerialIO.java @@ -83,9 +83,17 @@ public String processSerialEvent(SerialPortEvent event) { if (format.isBoundNewline()) { //process as line delimited - //find closest line delimiter - Integer endIdx = min(ByteUtilities.firstMatchingIndex(data.getByteArray(), new byte[] {'\r'}), - ByteUtilities.firstMatchingIndex(data.getByteArray(), new byte[] {'\n'})); + // check for CR AND NL + Integer endIdx = ByteUtilities.firstMatchingIndex(data.getByteArray(), new byte[] {'\r', '\n'}); + int delimSize = 2; + + // check for CR OR NL + if(endIdx == null) { + endIdx = min( + ByteUtilities.firstMatchingIndex(data.getByteArray(), new byte[] {'\r'}), + ByteUtilities.firstMatchingIndex(data.getByteArray(), new byte[] {'\n'})); + delimSize = 1; + } if (endIdx != null) { log.trace("Reading newline-delimited response"); byte[] output = new byte[endIdx]; @@ -97,7 +105,7 @@ public String processSerialEvent(SerialPortEvent event) { response = buffer; } - data.clearRange(0, endIdx + 1); + data.clearRange(0, endIdx + delimSize); } } else if (format.getBoundStart() != null && format.getBoundStart().length > 0) { //process as formatted response