Skip to content

Properly decode UTF-8 characters comming in from serial one byte at a time. #5967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions arduino-core/src/processing/app/Serial.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@
import jssc.SerialPortException;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

import static processing.app.I18n.tr;
import static processing.app.I18n.format;
import static processing.app.I18n.tr;

public class Serial implements SerialPortEventListener {

Expand All @@ -47,6 +53,12 @@ public class Serial implements SerialPortEventListener {

private SerialPort port;

private CharsetDecoder bytesToStrings;
private static final int IN_BUFFER_CAPACITY = 128;
private static final int OUT_BUFFER_CAPACITY = 128;
private ByteBuffer inFromSerial = ByteBuffer.allocate(IN_BUFFER_CAPACITY);
private CharBuffer outToMessage = CharBuffer.allocate(OUT_BUFFER_CAPACITY);

public Serial() throws SerialException {
this(PreferencesData.get("serial.port"),
PreferencesData.getInteger("serial.debug_rate", 9600),
Expand Down Expand Up @@ -101,6 +113,8 @@ private Serial(String iname, int irate, char iparity, int idatabits, float istop
//this.parent = parent;
//parent.attach(this);

resetDecoding(StandardCharsets.UTF_8);

int parity = SerialPort.PARITY_NONE;
if (iparity == 'E') parity = SerialPort.PARITY_EVEN;
if (iparity == 'O') parity = SerialPort.PARITY_ODD;
Expand Down Expand Up @@ -153,10 +167,24 @@ public synchronized void serialEvent(SerialPortEvent serialEvent) {
if (serialEvent.isRXCHAR()) {
try {
byte[] buf = port.readBytes(serialEvent.getEventValue());
if (buf.length > 0) {
String msg = new String(buf);
char[] chars = msg.toCharArray();
message(chars, chars.length);
int next = 0;
while(next < buf.length) {
while(next < buf.length && outToMessage.hasRemaining()) {
int spaceInIn = inFromSerial.remaining();
int copyNow = buf.length - next < spaceInIn ? buf.length - next : spaceInIn;
inFromSerial.put(buf, next, copyNow);
next += copyNow;
inFromSerial.flip();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why flip() is called?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The buffer needs to be switched from being written to to being read from, which is what flip does.

Both clear and compact switch the buffer from having being read to being ready to accept writes.

bytesToStrings.decode(inFromSerial, outToMessage, false);
inFromSerial.compact();
}
outToMessage.flip();
if(outToMessage.hasRemaining()) {
char[] chars = new char[outToMessage.remaining()];
outToMessage.get(chars);
message(chars, chars.length);
}
outToMessage.clear();
}
} catch (SerialPortException e) {
errorMessage("serialEvent", e);
Expand Down Expand Up @@ -226,6 +254,17 @@ public void setRTS(boolean state) {
}
}

/**
* Reset the encoding used to convert the bytes coming in
* before they are handed as Strings to {@Link #message(char[], int)}.
*/
public synchronized void resetDecoding(Charset charset) {
bytesToStrings = charset.newDecoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.replaceWith("\u2e2e");
}

static public List<String> list() {
return Arrays.asList(SerialPortList.getPortNames());
}
Expand Down