Skip to content

Commit e794a25

Browse files
Copilotjyong15
andcommitted
Fix exception handling and refactor duplicated code
Co-authored-by: jyong15 <26561407+jyong15@users.noreply.github.com>
1 parent 9b86c29 commit e794a25

2 files changed

Lines changed: 83 additions & 67 deletions

File tree

ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerPC.java

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -645,43 +645,33 @@ public void setSerialPort(SerialPort sp){
645645
}
646646
if(mByteCommunication instanceof ByteCommunicationJSerialComm) {
647647
((ByteCommunicationJSerialComm)mByteCommunication).setSerialPort(convertJsscToJSerialComm(sp));
648-
getSamplingRateShimmer();
649-
650-
if (mByteCommunication.isOpened()){
651-
setBluetoothRadioState(BT_STATE.CONNECTING);
652-
}
653-
if (mByteCommunication.isOpened() && mBluetoothRadioState!=BT_STATE.DISCONNECTED){
654-
setIsConnected(true);
655-
656-
mIOThread = new IOThread();
657-
mIOThread.start();
658-
if(mUseProcessingThread){
659-
mPThread = new ProcessingThread();
660-
mPThread.start();
661-
}
662-
initialize();
663-
}
648+
initializeConnectionAndThreads();
664649
} else if(mByteCommunication instanceof ByteCommunicationJSSC) {
665650
// Fallback to JSSC if needed
666651
((ByteCommunicationJSSC)mByteCommunication).setSerialPort(sp);
667-
getSamplingRateShimmer();
668-
669-
if (mByteCommunication.isOpened()){
670-
setBluetoothRadioState(BT_STATE.CONNECTING);
671-
}
672-
if (mByteCommunication.isOpened() && mBluetoothRadioState!=BT_STATE.DISCONNECTED){
673-
// if (mSerialPort.isOpened() && mState!=BT_STATE.NONE && mState!=BT_STATE.DISCONNECTED){
674-
// setState(BT_STATE.CONNECTED);
675-
setIsConnected(true);
652+
initializeConnectionAndThreads();
653+
}
654+
}
676655

677-
mIOThread = new IOThread();
678-
mIOThread.start();
679-
if(mUseProcessingThread){
680-
mPThread = new ProcessingThread();
681-
mPThread.start();
682-
}
683-
initialize();
656+
/**
657+
* Helper method to initialize connection and start IO threads after setting serial port
658+
*/
659+
private void initializeConnectionAndThreads() {
660+
getSamplingRateShimmer();
661+
662+
if (mByteCommunication.isOpened()){
663+
setBluetoothRadioState(BT_STATE.CONNECTING);
664+
}
665+
if (mByteCommunication.isOpened() && mBluetoothRadioState!=BT_STATE.DISCONNECTED){
666+
setIsConnected(true);
667+
668+
mIOThread = new IOThread();
669+
mIOThread.start();
670+
if(mUseProcessingThread){
671+
mPThread = new ProcessingThread();
672+
mPThread.start();
684673
}
674+
initialize();
685675
}
686676
}
687677

ShimmerDriverPC/src/main/java/com/shimmerresearch/shimmer3/communication/ByteCommunicationJSerialComm.java

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)