33
33
import java .nio .charset .Charset ;
34
34
import java .nio .charset .CharsetDecoder ;
35
35
import java .nio .charset .CodingErrorAction ;
36
- import java .nio .charset .StandardCharsets ;
37
36
import java .util .Arrays ;
38
37
import java .util .List ;
39
38
@@ -60,39 +59,28 @@ public class Serial implements SerialPortEventListener {
60
59
private CharBuffer outToMessage = CharBuffer .allocate (OUT_BUFFER_CAPACITY );
61
60
62
61
public Serial () throws SerialException {
63
- this (PreferencesData .get ("serial.port" ),
64
- PreferencesData .getInteger ("serial.debug_rate" , 9600 ),
65
- PreferencesData .getNonEmpty ("serial.parity" , "N" ).charAt (0 ),
66
- PreferencesData .getInteger ("serial.databits" , 8 ),
67
- PreferencesData .getFloat ("serial.stopbits" , 1 ),
68
- !BaseNoGui .getBoardPreferences ().getBoolean ("serial.disableRTS" ),
69
- !BaseNoGui .getBoardPreferences ().getBoolean ("serial.disableDTR" ));
62
+ this (PreferencesData .get ("serial.port" ), PreferencesData .getInteger ("serial.debug_rate" , 9600 ));
70
63
}
71
64
72
65
public Serial (int irate ) throws SerialException {
73
- this (PreferencesData .get ("serial.port" ), irate ,
74
- PreferencesData .getNonEmpty ("serial.parity" , "N" ).charAt (0 ),
75
- PreferencesData .getInteger ("serial.databits" , 8 ),
76
- PreferencesData .getFloat ("serial.stopbits" , 1 ),
77
- !BaseNoGui .getBoardPreferences ().getBoolean ("serial.disableRTS" ),
78
- !BaseNoGui .getBoardPreferences ().getBoolean ("serial.disableDTR" ));
66
+ this (PreferencesData .get ("serial.port" ), irate );
67
+ }
68
+
69
+ public Serial (String iname ) throws SerialException {
70
+ this (iname , PreferencesData .getInteger ("serial.debug_rate" , 9600 ));
79
71
}
80
72
81
73
public Serial (String iname , int irate ) throws SerialException {
82
- this (iname , irate , PreferencesData .getNonEmpty ("serial.parity" , "N" ).charAt (0 ),
83
- PreferencesData .getInteger ("serial.databits" , 8 ),
84
- PreferencesData .getFloat ("serial.stopbits" , 1 ),
85
- !BaseNoGui .getBoardPreferences ().getBoolean ("serial.disableRTS" ),
86
- !BaseNoGui .getBoardPreferences ().getBoolean ("serial.disableDTR" ));
74
+ this (iname , irate , Charset .defaultCharset ());
87
75
}
88
76
89
- public Serial (String iname ) throws SerialException {
90
- this (iname , PreferencesData .getInteger ("serial.debug_rate" , 9600 ),
91
- PreferencesData .getNonEmpty ("serial.parity" , "N" ).charAt (0 ),
77
+ public Serial (String iname , int irate , Charset charset ) throws SerialException {
78
+ this (iname , irate , PreferencesData .getNonEmpty ("serial.parity" , "N" ).charAt (0 ),
92
79
PreferencesData .getInteger ("serial.databits" , 8 ),
93
80
PreferencesData .getFloat ("serial.stopbits" , 1 ),
94
81
!BaseNoGui .getBoardPreferences ().getBoolean ("serial.disableRTS" ),
95
- !BaseNoGui .getBoardPreferences ().getBoolean ("serial.disableDTR" ));
82
+ !BaseNoGui .getBoardPreferences ().getBoolean ("serial.disableDTR" ),
83
+ charset );
96
84
}
97
85
98
86
public static boolean touchForCDCReset (String iname ) throws SerialException {
@@ -116,12 +104,13 @@ public static boolean touchForCDCReset(String iname) throws SerialException {
116
104
}
117
105
}
118
106
119
- private Serial (String iname , int irate , char iparity , int idatabits , float istopbits , boolean setRTS , boolean setDTR ) throws SerialException {
107
+ private Serial (String iname , int irate , char iparity , int idatabits ,
108
+ float istopbits , boolean setRTS , boolean setDTR , Charset charset ) throws SerialException {
120
109
//if (port != null) port.close();
121
110
//this.parent = parent;
122
111
//parent.attach(this);
123
112
124
- resetDecoding (StandardCharsets . UTF_8 );
113
+ resetDecoding (charset );
125
114
126
115
int parity = SerialPort .PARITY_NONE ;
127
116
if (iparity == 'E' ) parity = SerialPort .PARITY_EVEN ;
@@ -175,24 +164,32 @@ public synchronized void serialEvent(SerialPortEvent serialEvent) {
175
164
if (serialEvent .isRXCHAR ()) {
176
165
try {
177
166
byte [] buf = port .readBytes (serialEvent .getEventValue ());
178
- int next = 0 ;
179
- while (next < buf .length ) {
180
- while (next < buf .length && outToMessage .hasRemaining ()) {
181
- int spaceInIn = inFromSerial .remaining ();
182
- int copyNow = buf .length - next < spaceInIn ? buf .length - next : spaceInIn ;
183
- inFromSerial .put (buf , next , copyNow );
184
- next += copyNow ;
185
- inFromSerial .flip ();
186
- bytesToStrings .decode (inFromSerial , outToMessage , false );
187
- inFromSerial .compact ();
167
+ if (bytesToStrings == null ) {
168
+ char [] chars = new char [buf .length ];
169
+ for (int i = 0 ; i < buf .length ; i ++) {
170
+ chars [i ] = (char ) buf [i ];
188
171
}
189
- outToMessage .flip ();
190
- if (outToMessage .hasRemaining ()) {
191
- char [] chars = new char [outToMessage .remaining ()];
192
- outToMessage .get (chars );
193
- message (chars , chars .length );
172
+ message (chars , chars .length );
173
+ } else {
174
+ int next = 0 ;
175
+ while (next < buf .length ) {
176
+ while (next < buf .length && outToMessage .hasRemaining ()) {
177
+ int spaceInIn = inFromSerial .remaining ();
178
+ int copyNow = buf .length - next < spaceInIn ? buf .length - next : spaceInIn ;
179
+ inFromSerial .put (buf , next , copyNow );
180
+ next += copyNow ;
181
+ inFromSerial .flip ();
182
+ bytesToStrings .decode (inFromSerial , outToMessage , false );
183
+ inFromSerial .compact ();
184
+ }
185
+ outToMessage .flip ();
186
+ if (outToMessage .hasRemaining ()) {
187
+ char [] chars = new char [outToMessage .remaining ()];
188
+ outToMessage .get (chars );
189
+ message (chars , chars .length );
190
+ }
191
+ outToMessage .clear ();
194
192
}
195
- outToMessage .clear ();
196
193
}
197
194
} catch (SerialPortException e ) {
198
195
errorMessage ("serialEvent" , e );
@@ -264,15 +261,25 @@ public void setRTS(boolean state) {
264
261
265
262
/**
266
263
* Reset the encoding used to convert the bytes coming in
267
- * before they are handed as Strings to {@Link #message(char[], int)}.
264
+ * before they are handed as char arrays to {@Link #message(char[], int)}.
265
+ * @param charset - The character set that will be used for conversion or null to not perform conversion,
266
+ * in which case bytes are simply cast to chars.
268
267
*/
269
268
public synchronized void resetDecoding (Charset charset ) {
270
- bytesToStrings = charset .newDecoder ()
269
+ bytesToStrings = charset == null ? null : charset .newDecoder ()
271
270
.onMalformedInput (CodingErrorAction .REPLACE )
272
271
.onUnmappableCharacter (CodingErrorAction .REPLACE )
273
272
.replaceWith ("\u2e2e " );
274
273
}
275
274
275
+ /**
276
+ * Get the {@link Charset} used to convert the incoming bytes to chars.
277
+ * @return The {@link Charset} or null when no conversion is performed, in which case bytes are simply cast to chars.
278
+ */
279
+ public Charset getCharset () {
280
+ return bytesToStrings == null ? null : bytesToStrings .charset ();
281
+ }
282
+
276
283
static public List <String > list () {
277
284
return Arrays .asList (SerialPortList .getPortNames ());
278
285
}
0 commit comments