Skip to content

Commit 1f3819c

Browse files
Prevent bytes from lingering in the serial buffer
This fixes a problem with the Serial UTF-8 decoder. This decoding moves data from char[] buf, into a ByteBuffer inFromSerial, then decodes them into a CharBuffer outToMessage and converts to a char[] to pass on. When the buf read contained just over a full buffer worth of bytes and contained some multi-byte characters, a situation could arise where two decodes were needed to fill up outToMessage, leaving some data in inFromSerial. If in this case no data would be left in buf, decoding would stop until more data came in from serial. This commit fixes this problem by: - Changing the outer loop to continue running when buf is empty, but inFromSerial is not. - Changing the inner loop to run at least once (so it runs when buf is empty, but inFromSerial is no). - Breaking out of the outer loop when no characters were produced (this handles the case where only an incomplete UTF-8 character remains in inFromSerial, which would otherwise prevent the loop from terminating. - Removes a `if (outToMessage.hasRemaining()` check that is now necessarily true if the break was not done. This fixes arduino#9808.
1 parent 990e934 commit 1f3819c

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

Diff for: arduino-core/src/processing/app/Serial.java

+28-8
Original file line numberDiff line numberDiff line change
@@ -176,21 +176,41 @@ public synchronized void serialEvent(SerialPortEvent serialEvent) {
176176
try {
177177
byte[] buf = port.readBytes(serialEvent.getEventValue());
178178
int next = 0;
179-
while(next < buf.length) {
180-
while(next < buf.length && outToMessage.hasRemaining()) {
179+
// This uses a CharsetDecoder to convert from bytes to UTF-8 in
180+
// a streaming fashion (i.e. where characters might be split
181+
// over multiple reads). This needs the data to be in a
182+
// ByteBuffer (inFromSerial, which we also use to store leftover
183+
// incomplete characters for the nexst run) and produces a
184+
// CharBuffer (outToMessage), which we then convert to char[] to
185+
// pass onwards.
186+
// Note that these buffers switch from input to output mode
187+
// using flip/compact/clear
188+
while(next < buf.length || inFromSerial.position() > 0) {
189+
do {
190+
// This might be 0 when all data was already read from buf
191+
// (but then there will be data in inFromSerial left to
192+
// decode).
181193
int copyNow = Math.min(buf.length - next, inFromSerial.remaining());
182194
inFromSerial.put(buf, next, copyNow);
183195
next += copyNow;
196+
184197
inFromSerial.flip();
185198
bytesToStrings.decode(inFromSerial, outToMessage, false);
186199
inFromSerial.compact();
187-
}
200+
201+
// When there are multi-byte characters, outToMessage might
202+
// still have room, so add more bytes if we have any.
203+
} while (next < buf.length && outToMessage.hasRemaining());
204+
205+
// If no output was produced, the input only contained
206+
// incomplete characters, so we're done processing
207+
if (outToMessage.position() == 0)
208+
break;
209+
188210
outToMessage.flip();
189-
if(outToMessage.hasRemaining()) {
190-
char[] chars = new char[outToMessage.remaining()];
191-
outToMessage.get(chars);
192-
message(chars, chars.length);
193-
}
211+
char[] chars = new char[outToMessage.remaining()];
212+
outToMessage.get(chars);
213+
message(chars, chars.length);
194214
outToMessage.clear();
195215
}
196216
} catch (SerialPortException e) {

0 commit comments

Comments
 (0)