@@ -23,7 +23,11 @@ public ByteCommunicationJSerialComm(SerialPort serialPort) {
2323
2424 @ Override
2525 public int getInputBufferBytesCount () throws jssc .SerialPortException {
26- return mSerialPort .bytesAvailable ();
26+ try {
27+ return mSerialPort .bytesAvailable ();
28+ } catch (Exception e ) {
29+ throw new jssc .SerialPortException (mAddress , "getInputBufferBytesCount" , e .getMessage ());
30+ }
2731 }
2832
2933 @ Override
@@ -33,65 +37,87 @@ public boolean isOpened() {
3337
3438 @ Override
3539 public boolean closePort () throws jssc .SerialPortException {
36- return mSerialPort .closePort ();
40+ try {
41+ return mSerialPort .closePort ();
42+ } catch (Exception e ) {
43+ throw new jssc .SerialPortException (mAddress , "closePort" , e .getMessage ());
44+ }
3745 }
3846
3947 @ Override
4048 public boolean openPort () throws jssc .SerialPortException {
41- boolean opened = mSerialPort .openPort ();
42- if (opened ) {
43- mSerialPort .setComPortParameters (115200 , 8 , SerialPort .ONE_STOP_BIT , SerialPort .NO_PARITY );
44- mSerialPort .setFlowControl (SerialPort .FLOW_CONTROL_DISABLED );
49+ try {
50+ boolean opened = mSerialPort .openPort ();
51+ if (opened ) {
52+ mSerialPort .setComPortParameters (115200 , 8 , SerialPort .ONE_STOP_BIT , SerialPort .NO_PARITY );
53+ mSerialPort .setFlowControl (SerialPort .FLOW_CONTROL_DISABLED );
54+ }
55+ return mSerialPort .isOpen ();
56+ } catch (Exception e ) {
57+ throw new jssc .SerialPortException (mAddress , "openPort" , e .getMessage ());
4558 }
46- return mSerialPort .isOpen ();
4759 }
4860
4961 @ Override
5062 public byte [] readBytes (int byteCount , int timeout ) throws jssc .SerialPortTimeoutException , jssc .SerialPortException {
51- mSerialPort .setComPortTimeouts (SerialPort .TIMEOUT_READ_BLOCKING , timeout , 0 );
52- byte [] rxbytes = new byte [byteCount ];
53- int bytesRead = mSerialPort .readBytes (rxbytes , byteCount );
54-
55- if (bytesRead < byteCount ) {
56- // Timeout occurred
57- throw new jssc .SerialPortTimeoutException (mAddress , "readBytes" , timeout );
63+ try {
64+ mSerialPort .setComPortTimeouts (SerialPort .TIMEOUT_READ_BLOCKING , timeout , 0 );
65+ byte [] rxbytes = new byte [byteCount ];
66+ int bytesRead = mSerialPort .readBytes (rxbytes , byteCount );
67+
68+ if (bytesRead < byteCount ) {
69+ // Timeout occurred
70+ throw new jssc .SerialPortTimeoutException (mAddress , "readBytes" , timeout );
71+ }
72+
73+ if (mPrintValues ) {
74+ System .out .println ("READ BYTES: " + UtilShimmer .bytesToHexString (rxbytes ));
75+ }
76+ return rxbytes ;
77+ } catch (jssc .SerialPortTimeoutException e ) {
78+ throw e ; // Re-throw timeout exceptions as-is
79+ } catch (Exception e ) {
80+ throw new jssc .SerialPortException (mAddress , "readBytes" , e .getMessage ());
5881 }
59-
60- if (mPrintValues ) {
61- System .out .println ("READ BYTES: " + UtilShimmer .bytesToHexString (rxbytes ));
62- }
63- return rxbytes ;
6482 }
6583
6684 @ Override
6785 public boolean writeBytes (byte [] buffer ) throws jssc .SerialPortException {
68- if (mPrintValues ) {
69- System .out .println ("WRITE BYTES: " + UtilShimmer .bytesToHexString (buffer ));
86+ try {
87+ if (mPrintValues ) {
88+ System .out .println ("WRITE BYTES: " + UtilShimmer .bytesToHexString (buffer ));
89+ }
90+ int bytesWritten = mSerialPort .writeBytes (buffer , buffer .length );
91+ return bytesWritten == buffer .length ;
92+ } catch (Exception e ) {
93+ throw new jssc .SerialPortException (mAddress , "writeBytes" , e .getMessage ());
7094 }
71- int bytesWritten = mSerialPort .writeBytes (buffer , buffer .length );
72- return bytesWritten == buffer .length ;
7395 }
7496
7597 @ Override
7698 public boolean setParams (int baudRate , int dataBits , int stopBits , int parity ) throws jssc .SerialPortException {
77- // Map parameters to jSerialComm constants
78- int stopBitsJSC = (stopBits == 1 ) ? SerialPort .ONE_STOP_BIT : SerialPort .TWO_STOP_BITS ;
79- int parityJSC = SerialPort .NO_PARITY ;
80- if (parity == 1 ) parityJSC = SerialPort .ODD_PARITY ;
81- else if (parity == 2 ) parityJSC = SerialPort .EVEN_PARITY ;
82-
83- return mSerialPort .setComPortParameters (baudRate , dataBits , stopBitsJSC , parityJSC );
99+ try {
100+ // Map parameters to jSerialComm constants
101+ int stopBitsJSC = (stopBits == 1 ) ? SerialPort .ONE_STOP_BIT : SerialPort .TWO_STOP_BITS ;
102+ int parityJSC = SerialPort .NO_PARITY ;
103+ if (parity == 1 ) parityJSC = SerialPort .ODD_PARITY ;
104+ else if (parity == 2 ) parityJSC = SerialPort .EVEN_PARITY ;
105+
106+ return mSerialPort .setComPortParameters (baudRate , dataBits , stopBitsJSC , parityJSC );
107+ } catch (Exception e ) {
108+ throw new jssc .SerialPortException (mAddress , "setParams" , e .getMessage ());
109+ }
84110 }
85111
86112 @ Override
87113 public boolean purgePort (int flags ) throws jssc .SerialPortException {
88- // JSSC purge flags: 1=PURGE_RXCLEAR, 2=PURGE_TXCLEAR
89- if (flags == 1 ) {
90- return mSerialPort .flushIOBuffers ();
91- } else if (flags == 2 ) {
114+ try {
115+ // JSSC purge flags: 1=PURGE_RXCLEAR, 2=PURGE_TXCLEAR
116+ // jSerialComm flushIOBuffers() clears both RX and TX
92117 return mSerialPort .flushIOBuffers ();
118+ } catch (Exception e ) {
119+ throw new jssc .SerialPortException (mAddress , "purgePort" , e .getMessage ());
93120 }
94- return mSerialPort .flushIOBuffers ();
95121 }
96122
97123 @ Override
0 commit comments