2424*/
2525
2626package processing .serial ;
27+
2728import processing .core .*;
29+
2830import java .lang .reflect .*;
2931import java .util .Map ;
32+
3033import jssc .*;
3134
32- public class Serial implements SerialPortEventListener {
3335
36+ public class Serial implements SerialPortEventListener {
3437 PApplet parent ;
3538 public SerialPort port ;
3639 Method serialAvailableMethod ;
@@ -50,22 +53,27 @@ public class Serial implements SerialPortEventListener {
5053 // * state of the RING, RLSD line
5154 // * sending breaks
5255
56+
5357 public Serial (PApplet parent ) {
5458 this (parent , "COM1" , 9600 , 'N' , 8 , 1 );
5559 }
5660
61+
5762 public Serial (PApplet parent , int baudRate ) {
5863 this (parent , "COM1" , baudRate , 'N' , 8 , 1 );
5964 }
6065
66+
6167 public Serial (PApplet parent , String portName ) {
6268 this (parent , portName , 9600 , 'N' , 8 , 1 );
6369 }
6470
71+
6572 public Serial (PApplet parent , String portName , int baudRate ) {
6673 this (parent , portName , baudRate , 'N' , 8 , 1 );
6774 }
6875
76+
6977 public Serial (PApplet parent , String portName , int baudRate , char parity , int dataBits , float stopBits ) {
7078 this .parent = parent ;
7179 parent .registerMethod ("dispose" , this );
@@ -101,7 +109,7 @@ public Serial(PApplet parent, String portName, int baudRate, char parity, int da
101109 port .addEventListener (this , SerialPort .MASK_RXCHAR );
102110 } catch (SerialPortException e ) {
103111 // this used to be a RuntimeException before, so stick with it
104- throw new RuntimeException ("Error opening serial port " + e .getPortName ()+ ": " + e .getExceptionType ());
112+ throw new RuntimeException ("Error opening serial port " + e .getPortName () + ": " + e .getExceptionType ());
105113 }
106114
107115 try {
@@ -115,10 +123,12 @@ public Serial(PApplet parent, String portName, int baudRate, char parity, int da
115123 }
116124 }
117125
126+
118127 public void dispose () {
119128 stop ();
120129 }
121130
131+
122132 public void pre () {
123133 if (serialAvailableMethod != null && invokeSerialAvailable ) {
124134 invokeSerialAvailable = false ;
@@ -137,43 +147,51 @@ public int available() {
137147 return (inBuffer -readOffset );
138148 }
139149
150+
140151 public void buffer (int size ) {
141152 bufferUntilSize = size ;
142153 }
143154
155+
144156 public void bufferUntil (int inByte ) {
145157 bufferUntilSize = 0 ;
146158 bufferUntilByte = (byte )inByte ;
147159 }
148160
161+
149162 public void clear () {
150163 synchronized (buffer ) {
151164 inBuffer = 0 ;
152165 readOffset = 0 ;
153166 }
154167 }
155168
169+
156170 public boolean getCTS () {
157171 try {
158172 return port .isCTS ();
159173 } catch (SerialPortException e ) {
160- throw new RuntimeException ("Error reading the CTS line: " + e .getExceptionType ());
174+ throw new RuntimeException ("Error reading the CTS line: " + e .getExceptionType ());
161175 }
162176 }
163177
178+
164179 public boolean getDSR () {
165180 try {
166181 return port .isDSR ();
167182 } catch (SerialPortException e ) {
168- throw new RuntimeException ("Error reading the DSR line: " + e .getExceptionType ());
183+ throw new RuntimeException ("Error reading the DSR line: " + e .getExceptionType ());
169184 }
170185 }
171186
187+
172188 public static Map <String , String > getProperties (String portName ) {
173- SerialPortList list = new SerialPortList ();
174- return list .getPortProperties (portName );
189+ //SerialPortList list = new SerialPortList();
190+ //return list.getPortProperties(portName);
191+ return SerialPortList .getPortProperties (portName );
175192 }
176193
194+
177195 public int last () {
178196 if (inBuffer == readOffset ) {
179197 return -1 ;
@@ -187,17 +205,21 @@ public int last() {
187205 }
188206 }
189207
208+
190209 public char lastChar () {
191210 return (char )last ();
192211 }
193212
213+
194214 public static String [] list () {
195- SerialPortList list = new SerialPortList ();
196215 // returns list sorted alphabetically, thus cu.* comes before tty.*
197216 // this was different with RXTX
198- return list .getPortNames ();
217+ //SerialPortList list = new SerialPortList();
218+ //return list.getPortNames();
219+ return SerialPortList .getPortNames ();
199220 }
200221
222+
201223 public int read () {
202224 if (inBuffer == readOffset ) {
203225 return -1 ;
@@ -213,6 +235,7 @@ public int read() {
213235 }
214236 }
215237
238+
216239 public byte [] readBytes () {
217240 if (inBuffer == readOffset ) {
218241 return null ;
@@ -226,6 +249,7 @@ public byte[] readBytes() {
226249 return ret ;
227250 }
228251 }
252+
229253
230254 public int readBytes (byte [] dest ) {
231255 if (inBuffer == readOffset ) {
@@ -246,6 +270,7 @@ public int readBytes(byte[] dest) {
246270 return toCopy ;
247271 }
248272 }
273+
249274
250275 public byte [] readBytesUntil (int inByte ) {
251276 if (inBuffer == readOffset ) {
@@ -277,6 +302,7 @@ public byte[] readBytesUntil(int inByte) {
277302 }
278303 }
279304
305+
280306 public int readBytesUntil (int inByte , byte [] dest ) {
281307 if (inBuffer == readOffset ) {
282308 return 0 ;
@@ -313,18 +339,20 @@ public int readBytesUntil(int inByte, byte[] dest) {
313339 }
314340 }
315341
342+
316343 public char readChar () {
317- return (char )read ();
344+ return (char ) read ();
318345 }
319346
347+
320348 public String readString () {
321349 if (inBuffer == readOffset ) {
322350 return null ;
323351 }
324-
325352 return new String (readBytes ());
326353 }
327354
355+
328356 public void serialEvent (SerialPortEvent event ) {
329357 if (event .getEventType () == SerialPortEvent .RXCHAR ) {
330358 int toRead ;
@@ -365,61 +393,68 @@ public void serialEvent(SerialPortEvent event) {
365393 }
366394 }
367395 } catch (SerialPortException e ) {
368- throw new RuntimeException ("Error reading from serial port " + e .getPortName ()+ ": " + e .getExceptionType ());
396+ throw new RuntimeException ("Error reading from serial port " + e .getPortName () + ": " + e .getExceptionType ());
369397 }
370398 }
371399 }
372400
401+
373402 public void setDTR (boolean state ) {
374403 // there is no way to influence the behavior of the DTR line when opening the serial port
375404 // this means that at least on Linux and OS X, Arduino devices are always reset
376405 try {
377406 port .setDTR (state );
378407 } catch (SerialPortException e ) {
379- throw new RuntimeException ("Error setting the DTR line: " + e .getExceptionType ());
408+ throw new RuntimeException ("Error setting the DTR line: " + e .getExceptionType ());
380409 }
381410 }
382411
412+
383413 public void setRTS (boolean state ) {
384414 try {
385415 port .setRTS (state );
386416 } catch (SerialPortException e ) {
387- throw new RuntimeException ("Error setting the RTS line: " + e .getExceptionType ());
417+ throw new RuntimeException ("Error setting the RTS line: " + e .getExceptionType ());
388418 }
389419 }
390420
421+
391422 public void stop () {
392423 try {
393424 port .closePort ();
394425 } catch (SerialPortException e ) {
426+ // ignored
395427 }
396428 inBuffer = 0 ;
397429 readOffset = 0 ;
398430 }
399431
432+
400433 public void write (byte [] src ) {
401434 try {
402435 // this might block if the serial device is not yet ready (esp. tty devices under OS X)
403436 port .writeBytes (src );
404437 // we used to call flush() here
405438 } catch (SerialPortException e ) {
406- throw new RuntimeException ("Error writing to serial port " + e .getPortName ()+ ": " + e .getExceptionType ());
439+ throw new RuntimeException ("Error writing to serial port " + e .getPortName () + ": " + e .getExceptionType ());
407440 }
408441 }
409442
443+
410444 public void write (int src ) {
411445 try {
412446 port .writeInt (src );
413447 } catch (SerialPortException e ) {
414- throw new RuntimeException ("Error writing to serial port " + e .getPortName ()+ ": " + e .getExceptionType ());
448+ throw new RuntimeException ("Error writing to serial port " + e .getPortName () + ": " + e .getExceptionType ());
415449 }
416450 }
417451
452+
418453 public void write (String src ) {
419454 try {
420455 port .writeString (src );
421456 } catch (SerialPortException e ) {
422- throw new RuntimeException ("Error writing to serial port " + e .getPortName ()+ ": " + e .getExceptionType ());
457+ throw new RuntimeException ("Error writing to serial port " + e .getPortName () + ": " + e .getExceptionType ());
423458 }
424459 }
425460}
0 commit comments