Only process data_received events if >0 bytes were read
This commit is contained in:
parent
23f29278c2
commit
daa26dd8ad
|
@ -2,7 +2,7 @@
|
||||||
* SerialPort.java
|
* SerialPort.java
|
||||||
*
|
*
|
||||||
* Created on: Feb 25, 2012
|
* Created on: Feb 25, 2012
|
||||||
* Last Updated on: Jan 27, 2022
|
* Last Updated on: Jan 28, 2022
|
||||||
* Author: Will Hedgecock
|
* Author: Will Hedgecock
|
||||||
*
|
*
|
||||||
* Copyright (C) 2012-2022 Fazecast, Inc.
|
* Copyright (C) 2012-2022 Fazecast, Inc.
|
||||||
|
@ -1759,45 +1759,48 @@ public final class SerialPort
|
||||||
newBytesIndex = 0;
|
newBytesIndex = 0;
|
||||||
byte[] newBytes = new byte[numBytesAvailable];
|
byte[] newBytes = new byte[numBytesAvailable];
|
||||||
bytesRemaining = readBytes(portHandle, newBytes, newBytes.length, 0, timeoutMode, readTimeout);
|
bytesRemaining = readBytes(portHandle, newBytes, newBytes.length, 0, timeoutMode, readTimeout);
|
||||||
if (delimiters.length > 0)
|
if (bytesRemaining > 0)
|
||||||
{
|
{
|
||||||
int startIndex = 0;
|
if (delimiters.length > 0)
|
||||||
for (int offset = 0; offset < bytesRemaining; ++offset)
|
{
|
||||||
if (newBytes[offset] == delimiters[delimiterIndex])
|
int startIndex = 0;
|
||||||
{
|
for (int offset = 0; offset < bytesRemaining; ++offset)
|
||||||
if ((++delimiterIndex) == delimiters.length)
|
if (newBytes[offset] == delimiters[delimiterIndex])
|
||||||
{
|
{
|
||||||
messageBytes.write(newBytes, startIndex, 1 + offset - startIndex);
|
if ((++delimiterIndex) == delimiters.length)
|
||||||
byte[] byteArray = (messageEndIsDelimited ? messageBytes.toByteArray() : Arrays.copyOf(messageBytes.toByteArray(), messageBytes.size() - delimiters.length));
|
{
|
||||||
if ((byteArray.length > 0) && (messageEndIsDelimited || (delimiters[0] == byteArray[0])))
|
messageBytes.write(newBytes, startIndex, 1 + offset - startIndex);
|
||||||
userDataListener.serialEvent(new SerialPortEvent(SerialPort.this, LISTENING_EVENT_DATA_RECEIVED, byteArray));
|
byte[] byteArray = (messageEndIsDelimited ? messageBytes.toByteArray() : Arrays.copyOf(messageBytes.toByteArray(), messageBytes.size() - delimiters.length));
|
||||||
startIndex = offset + 1;
|
if ((byteArray.length > 0) && (messageEndIsDelimited || (delimiters[0] == byteArray[0])))
|
||||||
messageBytes.reset();
|
userDataListener.serialEvent(new SerialPortEvent(SerialPort.this, LISTENING_EVENT_DATA_RECEIVED, byteArray));
|
||||||
delimiterIndex = 0;
|
startIndex = offset + 1;
|
||||||
if (!messageEndIsDelimited)
|
messageBytes.reset();
|
||||||
messageBytes.write(delimiters, 0, delimiters.length);
|
delimiterIndex = 0;
|
||||||
|
if (!messageEndIsDelimited)
|
||||||
|
messageBytes.write(delimiters, 0, delimiters.length);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
else if (delimiterIndex != 0)
|
||||||
else if (delimiterIndex != 0)
|
delimiterIndex = (newBytes[offset] == delimiters[0]) ? 1 : 0;
|
||||||
delimiterIndex = (newBytes[offset] == delimiters[0]) ? 1 : 0;
|
messageBytes.write(newBytes, startIndex, bytesRemaining - startIndex);
|
||||||
messageBytes.write(newBytes, startIndex, bytesRemaining - startIndex);
|
|
||||||
}
|
|
||||||
else if (dataPacket.length == 0)
|
|
||||||
userDataListener.serialEvent(new SerialPortEvent(SerialPort.this, LISTENING_EVENT_DATA_RECEIVED, newBytes.clone()));
|
|
||||||
else
|
|
||||||
{
|
|
||||||
while (bytesRemaining >= (dataPacket.length - dataPacketIndex))
|
|
||||||
{
|
|
||||||
System.arraycopy(newBytes, newBytesIndex, dataPacket, dataPacketIndex, dataPacket.length - dataPacketIndex);
|
|
||||||
bytesRemaining -= (dataPacket.length - dataPacketIndex);
|
|
||||||
newBytesIndex += (dataPacket.length - dataPacketIndex);
|
|
||||||
dataPacketIndex = 0;
|
|
||||||
userDataListener.serialEvent(new SerialPortEvent(SerialPort.this, LISTENING_EVENT_DATA_RECEIVED, dataPacket.clone()));
|
|
||||||
}
|
}
|
||||||
if (bytesRemaining > 0)
|
else if (dataPacket.length == 0)
|
||||||
|
userDataListener.serialEvent(new SerialPortEvent(SerialPort.this, LISTENING_EVENT_DATA_RECEIVED, newBytes.clone()));
|
||||||
|
else
|
||||||
{
|
{
|
||||||
System.arraycopy(newBytes, newBytesIndex, dataPacket, dataPacketIndex, bytesRemaining);
|
while (bytesRemaining >= (dataPacket.length - dataPacketIndex))
|
||||||
dataPacketIndex += bytesRemaining;
|
{
|
||||||
|
System.arraycopy(newBytes, newBytesIndex, dataPacket, dataPacketIndex, dataPacket.length - dataPacketIndex);
|
||||||
|
bytesRemaining -= (dataPacket.length - dataPacketIndex);
|
||||||
|
newBytesIndex += (dataPacket.length - dataPacketIndex);
|
||||||
|
dataPacketIndex = 0;
|
||||||
|
userDataListener.serialEvent(new SerialPortEvent(SerialPort.this, LISTENING_EVENT_DATA_RECEIVED, dataPacket.clone()));
|
||||||
|
}
|
||||||
|
if (bytesRemaining > 0)
|
||||||
|
{
|
||||||
|
System.arraycopy(newBytes, newBytesIndex, dataPacket, dataPacketIndex, bytesRemaining);
|
||||||
|
dataPacketIndex += bytesRemaining;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue