Skip to content

Commit 9b86c29

Browse files
Copilotjyong15
andcommitted
Add jSerialComm library and create jSerialComm implementations for Bluetooth
Co-authored-by: jyong15 <26561407+jyong15@users.noreply.github.com>
1 parent 99c5739 commit 9b86c29

7 files changed

Lines changed: 619 additions & 13 deletions

File tree

ShimmerDriverPC/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ dependencies {
8383
//implementation group: 'org.scream3r', name: 'jssc', version: '2.9.1'
8484
api group: 'io.github.java-native', name: 'jssc', version: '2.9.6'
8585

86+
// https://mvnrepository.com/artifact/com.fazecast/jSerialComm
87+
api group: 'com.fazecast', name: 'jSerialComm', version: '2.11.0'
8688

8789
// https://mvnrepository.com/artifact/net.sf.bluecove/bluecove
8890
implementation group: 'net.sf.bluecove', name: 'bluecove', version: '2.1.0'

ShimmerDriverPC/gradlew

100644100755
File mode changed.

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

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
import com.shimmerresearch.sensors.mpu9x50.SensorMPU9X50;
8383
import com.shimmerresearch.shimmer3.communication.ByteCommunication;
8484
import com.shimmerresearch.shimmer3.communication.ByteCommunicationJSSC;
85+
import com.shimmerresearch.shimmer3.communication.ByteCommunicationJSerialComm;
8586
import com.shimmerresearch.shimmerConfig.FixedShimmerConfigs.FIXED_SHIMMER_CONFIG_MODE;
8687
import com.shimmerresearch.driver.CallbackObject;
8788
import com.shimmerresearch.driver.ObjectCluster;
@@ -355,7 +356,8 @@ public void run(){
355356
if (mByteCommunication==null || mTesting){
356357
setComPort(address);
357358
if (!mTesting) {
358-
mByteCommunication = new ByteCommunicationJSSC(address);
359+
// Use jSerialComm for better compatibility, especially on macOS
360+
mByteCommunication = new ByteCommunicationJSerialComm(address);
359361
} else { // do nothingit should already be set
360362

361363
}
@@ -638,9 +640,29 @@ private void closeConnection(){
638640

639641
public void setSerialPort(SerialPort sp){
640642
if (mByteCommunication == null) {
641-
mByteCommunication = new ByteCommunicationJSSC(sp);
643+
// Use jSerialComm for better compatibility, especially on macOS
644+
mByteCommunication = new ByteCommunicationJSerialComm(convertJsscToJSerialComm(sp));
642645
}
643-
if(mByteCommunication instanceof ByteCommunicationJSSC) {
646+
if(mByteCommunication instanceof ByteCommunicationJSerialComm) {
647+
((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+
}
664+
} else if(mByteCommunication instanceof ByteCommunicationJSSC) {
665+
// Fallback to JSSC if needed
644666
((ByteCommunicationJSSC)mByteCommunication).setSerialPort(sp);
645667
getSamplingRateShimmer();
646668

@@ -662,6 +684,16 @@ public void setSerialPort(SerialPort sp){
662684
}
663685
}
664686
}
687+
688+
/**
689+
* Helper method to convert JSSC SerialPort to jSerialComm SerialPort
690+
* @param jsscPort JSSC SerialPort object
691+
* @return jSerialComm SerialPort object for the same port
692+
*/
693+
private com.fazecast.jSerialComm.SerialPort convertJsscToJSerialComm(jssc.SerialPort jsscPort) {
694+
String portName = jsscPort.getPortName();
695+
return com.fazecast.jSerialComm.SerialPort.getCommPort(portName);
696+
}
665697

666698
@Override
667699
protected void sendStatusMSGtoUI(String msg) {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.shimmerresearch.pcSerialPort;
2+
3+
import com.fazecast.jSerialComm.SerialPort;
4+
5+
import java.io.IOException;
6+
import java.util.concurrent.TimeoutException;
7+
8+
/**
9+
* JSerialCommByteWriter: an implementation of ByteWriter for jSerialComm
10+
*
11+
* @author Shimmer Research
12+
* Based on JsscByteWriter by Bastien Aracil
13+
*/
14+
public class JSerialCommByteWriter implements ByteWriter {
15+
16+
private final SerialPort serialPort;
17+
18+
public JSerialCommByteWriter(SerialPort serialPort) {
19+
this.serialPort = serialPort;
20+
}
21+
22+
@Override
23+
public void cancelWrite() throws IOException {
24+
serialPort.flushIOBuffers();
25+
}
26+
27+
@Override
28+
public void write(byte[] bytes) throws IOException {
29+
int bytesWritten = serialPort.writeBytes(bytes, bytes.length);
30+
if (bytesWritten != bytes.length) {
31+
throw new IOException("Failed to write all bytes. Expected: " + bytes.length + ", Written: " + bytesWritten);
32+
}
33+
}
34+
35+
@Override
36+
public void write(byte oneByte) throws IOException {
37+
byte[] singleByte = new byte[]{oneByte};
38+
int bytesWritten = serialPort.writeBytes(singleByte, 1);
39+
if (bytesWritten != 1) {
40+
throw new IOException("Failed to write byte");
41+
}
42+
}
43+
44+
@Override
45+
public void write(byte[] bytes, long timeout) throws IOException, InterruptedException, TimeoutException {
46+
if (timeout <= 0) {
47+
this.write(bytes);
48+
} else {
49+
new TimedOutByteWriting(this, bytes, timeout).write();
50+
}
51+
}
52+
53+
@Override
54+
public void write(byte oneByte, long timeout) throws IOException, InterruptedException, TimeoutException {
55+
if (timeout <= 0) {
56+
this.write(oneByte);
57+
} else {
58+
new TimedOutByteWriting(this, oneByte, timeout).write();
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)