Skip to content

Commit

Permalink
Updated jSerialComm version, made SerialPortException inherit IOExcep…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
retrodaredevil committed May 6, 2020
1 parent f81ecd1 commit 37da71e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
<dependency>
<groupId>com.fazecast</groupId>
<artifactId>jSerialComm</artifactId>
<version>2.6.0</version>
<version>2.6.2</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.1</version>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
52 changes: 41 additions & 11 deletions src/main/java/me/retrodaredevil/io/serial/JSerialIOBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ public class JSerialIOBundle implements IOBundle {
private final InputStream inputStream;
private final OutputStream outputStream;
private final SerialPort serialPort;

public JSerialIOBundle(SerialPort serialPort, SerialConfig serialConfig) throws SerialPortException {
this(serialPort, serialConfig, Config.DEFAULT);
}
public JSerialIOBundle(SerialPort serialPort, SerialConfig serialConfig, Config jSerialConfig) throws SerialPortException {
this.serialPort = serialPort;
if(!serialPort.openPort(0)){
if(!serialPort.openPort(jSerialConfig.safetySleepTimeMillis, jSerialConfig.deviceSendQueueSize, jSerialConfig.deviceReceiveQueueSize)){
throw new SerialPortException("Was unsuccessful while trying to open port: " + serialPort.getSystemPortName() + " descriptive name: " + serialPort.getDescriptivePortName() + " description: " + serialPort.getPortDescription());
}
final int stopBits;
Expand Down Expand Up @@ -49,8 +51,8 @@ public JSerialIOBundle(SerialPort serialPort, SerialConfig serialConfig) throws
inputStream = requireNonNull(serialPort.getInputStream());
outputStream = requireNonNull(serialPort.getOutputStream());
}
public static JSerialIOBundle createFromPortIndex(int index, SerialConfig serialConfig) throws SerialPortException {

public static SerialPort createSerialPortFromIndex(int index) throws SerialPortException {
final SerialPort[] ports;
try {
ports = SerialPort.getCommPorts();
Expand All @@ -60,19 +62,30 @@ public static JSerialIOBundle createFromPortIndex(int index, SerialConfig serial
if(index >= ports.length){
throw new SerialPortException("There are only " + ports.length + " serial ports! index=" + index);
}
SerialPort port = ports[index];
return new JSerialIOBundle(port, serialConfig);
return ports[index];
}
public static JSerialIOBundle createPort(String port, SerialConfig serialConfig) throws SerialPortException {
final SerialPort serialPort;

public static JSerialIOBundle createFromPortIndex(int index, SerialConfig serialConfig) throws SerialPortException {
return new JSerialIOBundle(createSerialPortFromIndex(index), serialConfig);
}
public static SerialPort createSerialPortFromName(String port) throws SerialPortException {
try {
serialPort = SerialPort.getCommPort(port);
return SerialPort.getCommPort(port);
} catch(SerialPortInvalidPortException e){
throw new SerialPortException("invalid port! port: " + port, e);
}
return new JSerialIOBundle(serialPort, serialConfig);
}

public static JSerialIOBundle createPort(String port, SerialConfig serialConfig) throws SerialPortException {
return new JSerialIOBundle(createSerialPortFromName(port), serialConfig);
}

/**
* @return The raw jSerialComm {@link SerialPort}. You likely don't need to configure this, but if you do, here you go!
*/
public SerialPort getSerialPort() {
return serialPort;
}

@Override
public void close() {
serialPort.closePort();
Expand All @@ -87,4 +100,21 @@ public InputStream getInputStream() {
public OutputStream getOutputStream() {
return outputStream;
}

/**
* Contains JSerial specific config settings
*/
public static class Config {
public static final Config DEFAULT = new Config(0, 4096, 4096);

private final int safetySleepTimeMillis;
private final int deviceSendQueueSize;
private final int deviceReceiveQueueSize;

public Config(int safetySleepTimeMillis, int deviceSendQueueSize, int deviceReceiveQueueSize) {
this.safetySleepTimeMillis = safetySleepTimeMillis;
this.deviceSendQueueSize = deviceSendQueueSize;
this.deviceReceiveQueueSize = deviceReceiveQueueSize;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package me.retrodaredevil.io.serial;

public class SerialPortException extends Exception {
import java.io.IOException;

public class SerialPortException extends IOException {
public SerialPortException() {
}

Expand All @@ -16,7 +18,4 @@ public SerialPortException(Throwable cause) {
super(cause);
}

public SerialPortException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

0 comments on commit 37da71e

Please sign in to comment.