-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added purejavacomm library, uses api
- Loading branch information
1 parent
8cbb971
commit 7dea636
Showing
13 changed files
with
202 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 11 additions & 1 deletion
12
core/src/main/java/me/retrodaredevil/io/modbus/ModbusMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,25 @@ | ||
package me.retrodaredevil.io.modbus; | ||
|
||
/** | ||
* Represents the function code and data of a Modbus message. | ||
* <p> | ||
* This does not contain the checksum or the slave address, or MBAP header data. | ||
*/ | ||
public interface ModbusMessage { | ||
/** | ||
* @return The function code | ||
*/ | ||
int getFunctionCode(); | ||
byte getByteFunctionCode(); | ||
|
||
/** | ||
* NOTE: Do not modify the returned array. Doing so may produce undefined results | ||
* @return An array where each element represents a single byte (8 bit number). | ||
*/ | ||
int[] getData(); | ||
/** | ||
* NOTE: Do not modify the returned array. Doing so may produce undefined results | ||
* @return An array where each element represents a single byte (8 bit number). | ||
*/ | ||
byte[] getByteData(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
plugins { | ||
id 'java' | ||
} | ||
|
||
version "0.0.1-SNAPSHOT" | ||
|
||
dependencies { | ||
api project(":core") | ||
api "com.github.purejavacomm:purejavacomm:1.0.2.RELEASE" | ||
} |
90 changes: 90 additions & 0 deletions
90
purejavacomm/src/main/java/me/retrodaredevil/io/serial/PureJavaCommIOBundle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package me.retrodaredevil.io.serial; | ||
|
||
import me.retrodaredevil.io.IOBundle; | ||
import purejavacomm.*; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
public class PureJavaCommIOBundle implements IOBundle { | ||
private final SerialPort serialPort; | ||
private final InputStream inputStream; | ||
private final OutputStream outputStream; | ||
|
||
public PureJavaCommIOBundle(SerialPort serialPort) throws IOException { | ||
this.serialPort = serialPort; | ||
inputStream = serialPort.getInputStream(); | ||
outputStream = serialPort.getOutputStream(); | ||
} | ||
public static PureJavaCommIOBundle create(String port, SerialConfig serialConfig) throws SerialPortException { | ||
final CommPortIdentifier portIdentifier; | ||
try { | ||
portIdentifier = CommPortIdentifier.getPortIdentifier(port); | ||
} catch (NoSuchPortException e) { | ||
throw new SerialPortException(e); | ||
} | ||
final CommPort commPort; | ||
try { | ||
commPort = portIdentifier.open("io-lib", 0); | ||
} catch (PortInUseException e) { | ||
throw new SerialPortException(e); | ||
} | ||
final SerialPort serialPort = (SerialPort) commPort; // right now all CommPorts should be SerialPorts (maybe this could be changed in a future version) | ||
|
||
try { | ||
serialPort.setSerialPortParams(serialConfig.getBaudRateValue(), serialConfig.getDataBitsValue(), convertStopBits(serialConfig.getStopBits()), convertParity(serialConfig.getParity())); | ||
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE); | ||
} catch (UnsupportedCommOperationException e) { | ||
throw new SerialPortException(e); | ||
} | ||
try { | ||
if (serialConfig.isRTS()) { | ||
serialPort.setRTS(true); | ||
} | ||
if (serialConfig.isDTR()) { | ||
serialPort.setDTR(true); | ||
} | ||
} catch (PureJavaIllegalStateException e) { | ||
throw new SerialPortException("It's likely that RTS or DTR is not supported", e); | ||
} | ||
try { | ||
return new PureJavaCommIOBundle(serialPort); | ||
} catch (IOException e) { | ||
throw new SerialPortException(e); | ||
} | ||
} | ||
private static int convertStopBits(SerialConfig.StopBits stopBits) { | ||
switch (stopBits) { | ||
case ONE: return SerialPort.STOPBITS_1; | ||
case TWO: return SerialPort.STOPBITS_2; | ||
case ONE_POINT_FIVE: return SerialPort.STOPBITS_1_5; | ||
} | ||
throw new IllegalArgumentException("Unsupported stop bits: " + stopBits); | ||
} | ||
private static int convertParity(SerialConfig.Parity parity) { | ||
switch (parity) { | ||
case NONE: return SerialPort.PARITY_NONE; | ||
case ODD: return SerialPort.PARITY_ODD; | ||
case EVEN: return SerialPort.PARITY_EVEN; | ||
case MARK: return SerialPort.PARITY_MARK; | ||
case SPACE: return SerialPort.PARITY_SPACE; | ||
} | ||
throw new IllegalArgumentException("Unsupported parity: " + parity); | ||
} | ||
|
||
@Override | ||
public InputStream getInputStream() { | ||
return inputStream; | ||
} | ||
|
||
@Override | ||
public OutputStream getOutputStream() { | ||
return outputStream; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
serialPort.close(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
purejavacomm/src/test/java/me/retrodaredevil/io/serial/PureJavaSerialPortExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package me.retrodaredevil.io.serial; | ||
|
||
public class PureJavaSerialPortExample { | ||
public static void main(String[] args) throws SerialPortException { | ||
try (PureJavaCommIOBundle ioBundle = PureJavaCommIOBundle.create("/dev/ttyS10", new SerialConfigBuilder(9600).build())) { | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
rootProject.name = 'io-lib' | ||
include 'core' | ||
include 'jSerialComm' | ||
include 'purejavacomm' |