diff options
author | Cristian Maglie <c.maglie@arduino.cc> | 2020-03-24 14:16:06 +0300 |
---|---|---|
committer | Cristian Maglie <c.maglie@bug.st> | 2020-03-24 17:24:50 +0300 |
commit | d6667dd4caac7aa67693ebce925a36806ce00618 (patch) | |
tree | cd96d19d938ea6df4c4e15be16a10e4225dc62e1 /arduino-core | |
parent | a82f9a0e7408c40c8c53cb2af1f673009710bcf8 (diff) |
Added unit testing for UTF8 decoder in Serial
Diffstat (limited to 'arduino-core')
-rw-r--r-- | arduino-core/src/processing/app/Serial.java | 49 |
1 files changed, 29 insertions, 20 deletions
diff --git a/arduino-core/src/processing/app/Serial.java b/arduino-core/src/processing/app/Serial.java index 484ac1190..1ea670950 100644 --- a/arduino-core/src/processing/app/Serial.java +++ b/arduino-core/src/processing/app/Serial.java @@ -116,7 +116,7 @@ public class Serial implements SerialPortEventListener { } } - private Serial(String iname, int irate, char iparity, int idatabits, float istopbits, boolean setRTS, boolean setDTR) throws SerialException { + protected Serial(String iname, int irate, char iparity, int idatabits, float istopbits, boolean setRTS, boolean setDTR) throws SerialException { //if (port != null) port.close(); //this.parent = parent; //parent.attach(this); @@ -131,6 +131,11 @@ public class Serial implements SerialPortEventListener { if (istopbits == 1.5f) stopbits = SerialPort.STOPBITS_1_5; if (istopbits == 2) stopbits = SerialPort.STOPBITS_2; + // This is required for unit-testing + if (iname.equals("none")) { + return; + } + try { port = new SerialPort(iname); port.openPort(); @@ -175,31 +180,35 @@ public class Serial implements SerialPortEventListener { if (serialEvent.isRXCHAR()) { try { byte[] buf = port.readBytes(serialEvent.getEventValue()); - 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(); - 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(); - } + processSerialEvent(buf); } catch (SerialPortException e) { errorMessage("serialEvent", e); } } } + public void processSerialEvent(byte[] buf) { + 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(); + 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(); + } + } + /** * This method is intented to be extended to receive messages * coming from serial port. |