9
9
import qz .common .ByteArrayBuilder ;
10
10
import qz .utils .ByteUtilities ;
11
11
import qz .utils .DeviceUtilities ;
12
+ import qz .ws .SocketConnection ;
12
13
13
14
import java .io .IOException ;
14
15
15
16
/**
16
17
* @author Tres
17
18
*/
18
- public class SerialIO {
19
+ public class SerialIO implements DeviceListener {
19
20
20
21
private static final Logger log = LogManager .getLogger (SerialIO .class );
21
22
@@ -28,14 +29,17 @@ public class SerialIO {
28
29
29
30
private ByteArrayBuilder data = new ByteArrayBuilder ();
30
31
32
+ private SocketConnection websocket ;
33
+
31
34
32
35
/**
33
36
* Controller for serial communications
34
37
*
35
38
* @param portName Port name to open, such as "COM1" or "/dev/tty0/"
36
39
*/
37
- public SerialIO (String portName ) {
40
+ public SerialIO (String portName , SocketConnection websocket ) {
38
41
this .portName = portName ;
42
+ this .websocket = websocket ;
39
43
}
40
44
41
45
/**
@@ -257,26 +261,31 @@ public void sendData(JSONObject params, SerialOptions opts) throws JSONException
257
261
/**
258
262
* Closes the serial port, if open.
259
263
*
260
- * @return Boolean indicating success.
261
264
* @throws SerialPortException If the port fails to close.
262
265
*/
263
- public boolean close () throws SerialPortException {
266
+ @ Override
267
+ public void close () {
268
+ // Remove orphaned reference
269
+ websocket .removeSerialPort (portName );
270
+
264
271
if (!isOpen ()) {
265
272
log .warn ("Serial port [{}] is not open." , portName );
266
- return false ;
267
273
}
268
274
269
- boolean closed = port .closePort ();
270
- if (closed ) {
271
- log .info ("Serial port [{}] closed successfully." , portName );
272
- } else {
275
+ try {
276
+ boolean closed = port .closePort ();
277
+ if (closed ) {
278
+ log .info ("Serial port [{}] closed successfully." , portName );
279
+ } else {
280
+ // Handle ambiguity in JSSCs API
281
+ throw new SerialPortException (portName , "closePort" , "Port not closed" );
282
+ }
283
+ } catch (SerialPortException e ) {
273
284
log .warn ("Serial port [{}] was not closed properly." , portName );
274
285
}
275
286
276
287
port = null ;
277
288
portName = null ;
278
-
279
- return closed ;
280
289
}
281
290
282
291
private Integer min (Integer a , Integer b ) {
0 commit comments