Merge pull request #71 from FrozenOne/master

Fix reads that do not use packets getting stuck in infinite loop.
This commit is contained in:
Will Hedgecock 2018-01-03 11:16:33 -06:00 committed by GitHub
commit c41f3f640c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 11 deletions

View File

@ -1004,18 +1004,21 @@ public final class SerialPort
byte[] newBytes = new byte[numBytesAvailable];
newBytesIndex = 0;
bytesRemaining = readBytes(portHandle, newBytes, newBytes.length);
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(dataPacket.length == 0) {
userDataListener.serialEvent(new SerialPortEvent(SerialPort.this, LISTENING_EVENT_DATA_RECEIVED, newBytes.clone()));
}
if (bytesRemaining > 0)
{
System.arraycopy(newBytes, newBytesIndex, dataPacket, dataPacketIndex, bytesRemaining);
dataPacketIndex += bytesRemaining;
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) {
System.arraycopy(newBytes, newBytesIndex, dataPacket, dataPacketIndex, bytesRemaining);
dataPacketIndex += bytesRemaining;
}
}
}
}