@@ -0,0 +1,22 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<artifactId>serial</artifactId>
|
||||
<groupId>com.smartbotkit</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>..</relativePath>
|
||||
</parent>
|
||||
<artifactId>serial-core</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>SmartBotKit Serial-Core</name>
|
||||
<description>SmartBotKit Serial Communication Library Core</description>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.smartbotkit.serial;
|
||||
|
||||
/**
|
||||
* The baud rate of the serial connection.
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public enum BaudRate {
|
||||
/** 300 symbols per second **/
|
||||
RATE_300,
|
||||
/** 600 symbols per second **/
|
||||
RATE_600,
|
||||
/** 1200 symbols per second **/
|
||||
RATE_1200,
|
||||
/** 2400 symbols per second **/
|
||||
RATE_2400,
|
||||
/** 4800 symbols per second **/
|
||||
RATE_4800,
|
||||
/** 9600 symbols per second **/
|
||||
RATE_9600,
|
||||
/** 19200 symbols per second **/
|
||||
RATE_19200,
|
||||
/** 38400 symbols per second **/
|
||||
RATE_38400,
|
||||
/** 57600 symbols per second **/
|
||||
RATE_57600,
|
||||
/** 115200 symbols per second **/
|
||||
RATE_115200,
|
||||
/** 230400 symbols per second **/
|
||||
RATE_230400,
|
||||
/** 460800 symbols per second **/
|
||||
RATE_460800,
|
||||
/** 921600 symbols per second **/
|
||||
RATE_921600;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.smartbotkit.serial;
|
||||
|
||||
/**
|
||||
* The number of data bits in each character.
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public enum DataBits {
|
||||
/** 5 bits per character **/
|
||||
BITS_5,
|
||||
/** 6 bits per character **/
|
||||
BITS_6,
|
||||
/** 7 bits per character **/
|
||||
BITS_7,
|
||||
/** 8 bits per character **/
|
||||
BITS_8;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.smartbotkit.serial;
|
||||
|
||||
/**
|
||||
* The flow control used on the connection.
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public enum FlowControl {
|
||||
/** No flow control. **/
|
||||
NONE,
|
||||
/** Hardware flow control over rts and cts. **/
|
||||
RTS_CTS,
|
||||
/** Hardware flow control over dsr and dtr. **/
|
||||
DSR_DTR,
|
||||
/** Software flow control over xon and xoff. **/
|
||||
XON_XOFF;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.smartbotkit.serial;
|
||||
|
||||
/**
|
||||
* An interface to receive the state of input lines.
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public interface IFlowControlListener {
|
||||
|
||||
/**
|
||||
* Called when the input control line has been read.
|
||||
*
|
||||
* @param line The line.
|
||||
* @param state The state (true for high, false for low).
|
||||
*/
|
||||
void onLineSet(InputControlLine line, boolean state);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.smartbotkit.serial;
|
||||
|
||||
/**
|
||||
* A listener for errors raised during the
|
||||
* communication.
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public interface ILineErrorListener {
|
||||
|
||||
/**
|
||||
* Called when an error is detected.
|
||||
*
|
||||
* @param error The error type.
|
||||
*/
|
||||
void onLineError(LineError error);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.smartbotkit.serial;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* An abstraction for a serial port.
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public interface ISerialPort {
|
||||
|
||||
/**
|
||||
* Returns the serial port configuration.
|
||||
*
|
||||
* @return The serial port configuration.
|
||||
*/
|
||||
SerialConfig getConfig();
|
||||
|
||||
/**
|
||||
* Changes the value of the output line.
|
||||
*
|
||||
* @param line The line.
|
||||
* @param value The value (true for high, false for low).
|
||||
* @throws IOException Thro
|
||||
*/
|
||||
void setLine(OutputControlLine line, boolean value) throws IOException;
|
||||
|
||||
/**
|
||||
* Sets and unsets the listener that will receive errors.
|
||||
*
|
||||
* @param lineErrorListener The listener to set or null, to unset.
|
||||
*/
|
||||
void setLineErrorListener(ILineErrorListener lineErrorListener);
|
||||
|
||||
/**
|
||||
* Sets or unsets the listener to receive the state of flow control lines.
|
||||
*
|
||||
* @param flowControlListener The listener to set or null, to unset.
|
||||
*/
|
||||
void setFlowControlListener(IFlowControlListener flowControlListener);
|
||||
|
||||
/**
|
||||
* Writes a number of bytes to the port.
|
||||
*
|
||||
* @param buffer The buffer to write.
|
||||
* @param offset The offset to start from.
|
||||
* @param length The length to write.
|
||||
* @throws IOException Thrown if something fails.
|
||||
*/
|
||||
void write(byte[] buffer, int offset, int length) throws IOException;
|
||||
|
||||
/**
|
||||
* Reads a number of bytes from the port.
|
||||
*
|
||||
* @param buffer The buffer to write to.
|
||||
* @param offset The offset to start from.
|
||||
* @param length The length to write to.
|
||||
* @return The number of bytes read.
|
||||
* @throws IOException Thrown if something fails.
|
||||
*/
|
||||
int read(byte[] buffer, int offset, int length) throws IOException;
|
||||
|
||||
/**
|
||||
* Closes the serial port.
|
||||
*
|
||||
* @throws IOException Thrown if something fails.
|
||||
*/
|
||||
void close() throws IOException;
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.smartbotkit.serial;
|
||||
|
||||
/**
|
||||
* Constants for the input control lines whose value is read.
|
||||
*/
|
||||
public enum InputControlLine {
|
||||
/**
|
||||
* The clear to send line.
|
||||
*/
|
||||
CTS,
|
||||
/**
|
||||
* The data set ready line.
|
||||
*/
|
||||
DSR;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.smartbotkit.serial;
|
||||
|
||||
/**
|
||||
* An enumeration of error types signaled
|
||||
* by the error listener during communication.
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public enum LineError {
|
||||
/**
|
||||
* A break signal.
|
||||
*/
|
||||
BREAK,
|
||||
/**
|
||||
* A buffer overrun.
|
||||
*/
|
||||
OVERRUN,
|
||||
/**
|
||||
* A parity failure.
|
||||
*/
|
||||
PARITY,
|
||||
/**
|
||||
* A framing failure.
|
||||
*/
|
||||
FRAMING;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.smartbotkit.serial;
|
||||
|
||||
/**
|
||||
* The output control lines whose value can be set.
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public enum OutputControlLine {
|
||||
/**
|
||||
* The request to send line.
|
||||
*/
|
||||
RTS,
|
||||
/**
|
||||
* The data terminal ready line.
|
||||
*/
|
||||
DTR;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.smartbotkit.serial;
|
||||
|
||||
/**
|
||||
* The parity check applied to the data bits.
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public enum Parity {
|
||||
/** No parity check. **/
|
||||
NONE,
|
||||
/** Parity bit set so that the number of ones is odd. **/
|
||||
ODD,
|
||||
/** Parity bit set so that the number of ones is even. **/
|
||||
EVEN,
|
||||
/** Parity bit set to the mark signal condition. **/
|
||||
MARK,
|
||||
/** Parity bit set to the space signal condition. **/
|
||||
SPACE;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package com.smartbotkit.serial;
|
||||
|
||||
/**
|
||||
* Represents the configuration of a serial port with baud rate, data and stop bits,
|
||||
* flow control and parity settings.
|
||||
*
|
||||
* @author Marcus
|
||||
*/
|
||||
public class SerialConfig {
|
||||
|
||||
/** The baud rate. **/
|
||||
private BaudRate baudRate;
|
||||
/** The number of stop bits. **/
|
||||
private StopBits stopBits;
|
||||
/** The parity check. **/
|
||||
private Parity parity;
|
||||
/** The flow control mode. **/
|
||||
private FlowControl flowControl;
|
||||
/** The number of data bits. **/
|
||||
private DataBits dataBits;
|
||||
|
||||
/**
|
||||
* Creates a configuration for the serial port.
|
||||
*
|
||||
* @param dataBits The number of data bits.
|
||||
* @param stopBits The number of stop bits.
|
||||
* @param parity The parity check.
|
||||
* @param flowControl The flow control mode.
|
||||
* @param baudRate The baud rate.
|
||||
* @throws IllegalArgumentException Thrown if any of the parameters is null.
|
||||
*/
|
||||
public SerialConfig(DataBits dataBits, StopBits stopBits, Parity parity, FlowControl flowControl, BaudRate baudRate) {
|
||||
throwIfNull(dataBits, "DataBits must not be null.");
|
||||
this.dataBits = dataBits;
|
||||
throwIfNull(stopBits, "StopBits must not be null.");
|
||||
this.stopBits = stopBits;
|
||||
throwIfNull(parity, "Parity must not be null.");
|
||||
this.parity = parity;
|
||||
throwIfNull(flowControl, "FlowControl must not be null.");
|
||||
this.flowControl = flowControl;
|
||||
throwIfNull(baudRate, "BaudRate must not be null.");
|
||||
this.baudRate = baudRate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an illegal argument exception with the specified message, if
|
||||
* the passed object is null.
|
||||
*
|
||||
* @param o The object to test.
|
||||
* @param message The message to throw.
|
||||
* @throws IllegalArgumentException Thrown if the object is null.
|
||||
*/
|
||||
protected void throwIfNull(Object o, String message) throws IllegalArgumentException {
|
||||
if (o == null) throw new IllegalArgumentException(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the baud rate.
|
||||
*
|
||||
* @return The baud rate.
|
||||
*/
|
||||
public BaudRate getBaudRate() {
|
||||
return baudRate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of data bits.
|
||||
*
|
||||
* @return The number of data bits.
|
||||
*/
|
||||
public DataBits getDataBits() {
|
||||
return dataBits;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the flow control mode.
|
||||
*
|
||||
* @return The flow control mode.
|
||||
*/
|
||||
public FlowControl getFlowControl() {
|
||||
return flowControl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the parity check.
|
||||
*
|
||||
* @return The parity check.
|
||||
*/
|
||||
public Parity getParity() {
|
||||
return parity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of stop bits.
|
||||
*
|
||||
* @return The number of stop bits.
|
||||
*/
|
||||
public StopBits getStopBits() {
|
||||
return stopBits;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null || obj.getClass() != getClass()) return false;
|
||||
SerialConfig o = (SerialConfig)obj;
|
||||
return o.parity == parity && o.baudRate == baudRate &&
|
||||
o.flowControl == flowControl && o.stopBits == stopBits &&
|
||||
o.dataBits == dataBits;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("DataBits: %1s, StopBits: %2s, Parity: %3s, FlowControl: %4, BaudRate: %5s",
|
||||
dataBits, stopBits, parity, flowControl, baudRate);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.smartbotkit.serial;
|
||||
|
||||
/**
|
||||
* The number of bits sent after each character.
|
||||
*/
|
||||
public enum StopBits {
|
||||
/** One stop bit. **/
|
||||
BITS_1,
|
||||
/** One and a half stop bits. **/
|
||||
BITS_1_5,
|
||||
/** Two stop bits. **/
|
||||
BITS_2;
|
||||
}
|
||||
Reference in New Issue
Block a user