Class SerialConnection
- java.lang.Object
-
- ej.serial.SerialConnection
-
- All Implemented Interfaces:
AutoCloseable
public class SerialConnection extends Object implements AutoCloseable
This class defines a connection to a serial port.A serial port offers a (bi-directional) communication channel connecting two devices (or more in the case of a bus topology such as RS-485): each device can send/receive a stream of characters to/from the other device.
A serial port can represent different serial communication interfaces: a UART, a Virtual Serial Port (e.g. over USB), a loopback interface, …A
SerialConnectionis opened by providing a port name. For the list of supported ports, please refer to your VEE Port documentation. The serial line parameters can be configured withconfigure(int, int, int, int). Whether callingconfigure(int, int, int, int)is required depends on the VEE Port implementation — if the port is already configured at open time (e.g. a virtual USB-CDC port or a platform-configured UART), the connection is ready to use immediately without calling configure().A
SerialConnectioncan be configured with the following parameters:- the baudrate: the rate at which information is transferred in a communication channel.
In the serial port context, "9600 baud" means that the serial port is capable of transferring a maximum of 9600 bits per second (including the start bit, the stop bit(s) and the parity bit if present). - the data bits: the number of data bits in each character.
- the stop bits: the duration of the stop mark (in number of bits) sent at the end of each character allow the receiving signal hardware to detect the end of a character and re-synchronize with the character stream.
- the parity mode: the parity bit in each character is a method of detecting transmission errors.
A
SerialConnectionprovides exactly oneInputStreamand oneOutputStream. Repeated calls togetInputStream()orgetOutputStream()return the same already-instantiated stream object.SerialConnectionimplementsAutoCloseable, which means it works best with the try-with-resources statement. If not, methodclose()must be explicitly called to free the physical port. TheInputStreamandOutputStreamshare the connection's lifecycle: closing any one of them closes the underlying connection.Example:
try (SerialConnection connection = new SerialConnection("COM6")) { connection.configure(115_200, SerialConnection.DATABITS_8, SerialConnection.PARITY_NONE, SerialConnection.STOPBITS_1); InputStream is = connection.getInputStream(); OutputStream os = connection.getOutputStream(); int read; while ((read = is.read(buffer)) != -1) { os.write(buffer, 0, read); } }
-
-
Field Summary
Fields Modifier and Type Field Description static intDATABITS_5Data bits: 5.static intDATABITS_6Data bits: 6.static intDATABITS_7Data bits: 7.static intDATABITS_8Data bits: 8.static intDATABITS_9Data bits: 9.static intPARITY_EVENParity mode: even.static intPARITY_NONEParity mode: none.static intPARITY_ODDParity mode: odd.static intSTOPBITS_1Stop bits: 1.static intSTOPBITS_1_5Stop bits: 1.5.static intSTOPBITS_2Stop bits: 2.
-
Constructor Summary
Constructors Constructor Description SerialConnection(String portName)Opens a connection to the serial port with no explicit line configuration.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes this connection.voidconfigure(int baudrate, int databits, int parity, int stopbits)Sets the serial line parameters for this connection.InputStreamgetInputStream()Returns theInputStreamfor this connection.OutputStreamgetOutputStream()Returns theOutputStreamfor this connection.booleanisClosed()Returns the closed state of this connection.
-
-
-
Field Detail
-
DATABITS_5
public static final int DATABITS_5
Data bits: 5.- See Also:
- Constant Field Values
-
DATABITS_6
public static final int DATABITS_6
Data bits: 6.- See Also:
- Constant Field Values
-
DATABITS_7
public static final int DATABITS_7
Data bits: 7.- See Also:
- Constant Field Values
-
DATABITS_8
public static final int DATABITS_8
Data bits: 8.- See Also:
- Constant Field Values
-
DATABITS_9
public static final int DATABITS_9
Data bits: 9.- See Also:
- Constant Field Values
-
PARITY_NONE
public static final int PARITY_NONE
Parity mode: none.- See Also:
- Constant Field Values
-
PARITY_ODD
public static final int PARITY_ODD
Parity mode: odd.- See Also:
- Constant Field Values
-
PARITY_EVEN
public static final int PARITY_EVEN
Parity mode: even.- See Also:
- Constant Field Values
-
STOPBITS_1
public static final int STOPBITS_1
Stop bits: 1.- See Also:
- Constant Field Values
-
STOPBITS_1_5
public static final int STOPBITS_1_5
Stop bits: 1.5.- See Also:
- Constant Field Values
-
STOPBITS_2
public static final int STOPBITS_2
Stop bits: 2.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
SerialConnection
public SerialConnection(String portName) throws IOException
Opens a connection to the serial port with no explicit line configuration.Whether
configure(int, int, int, int)must subsequently be called depends on the VEE Port implementation — if the underlying LLAPIopen()already configures the port (e.g. from a platform default or from a virtual port that ignores line parameters), the connection is ready to use immediately.- Parameters:
portName- the serial port name (specific to the platform used).- Throws:
IOException- if an I/O error occurs. For example,portNamedoes not exist or is already in use.SecurityException- if a security manager exists and does not allow the caller to access the serial port.
-
-
Method Detail
-
configure
public void configure(int baudrate, int databits, int parity, int stopbits) throws IOExceptionSets the serial line parameters for this connection.This method is optional. Whether it needs to be called depends on the VEE Port implementation. If the port is configured at open time by the LLAPI (e.g. a virtual USB-CDC port or a UART whose defaults are set by the platform), calling
configure()may not be necessary. For ports where the application controls the line parameters,configure()must be called before communication.The provided parameters are used to configure the physical interface. If the configuration arguments are not applicable to the target port (for example USB-CDC virtual port), the configuration is ignored. If the requested configuration is invalid, an IllegalArgumentException is thrown. If an unsupported configuration is requested, a SerialConfigurationException is thrown.
- Parameters:
baudrate- the serial port baudrate (e.g.9_600,115_200, …)databits- the serial port data bits, one ofDATABITS_5,DATABITS_6,DATABITS_7,DATABITS_8,DATABITS_9.parity- the serial port parity mode, one ofPARITY_NONE,PARITY_ODD,PARITY_EVEN.stopbits- the serial port stop bits, one ofSTOPBITS_1,STOPBITS_1_5,STOPBITS_2.- Throws:
IOException- if an I/O error occurs (e.g. the port was disconnected).IllegalArgumentException- if a configuration argument is illegalSerialConfigurationException- if the hardware does not support the requested parameter combination. The parameters are the cause; retrying with the same values will always fail.
-
getInputStream
public InputStream getInputStream() throws IOException
Returns theInputStreamfor this connection.Subsequent calls to this method return the same
InputStreaminstance.Closing the
InputStreamof a SerialConnection closes the underlying SerialConnection.- Returns:
- the
InputStreamfor reading bytes from thisSerialConnection. - Throws:
IOException- if this connection is closed.
-
getOutputStream
public OutputStream getOutputStream() throws IOException
Returns theOutputStreamfor this connection.Subsequent calls to this method return the same
OutputStreaminstance.Closing the
OutputStreamof a SerialConnection closes the underlying SerialConnection.- Returns:
- the
OutputStreamfor writing bytes to thisSerialConnection. - Throws:
IOException- if this connection is closed.
-
close
public void close() throws IOExceptionCloses this connection.If this
SerialConnectionis already closed, this method does nothing.Closing this
SerialConnectionwill also close itsInputStreamandOutputStream.- Specified by:
closein interfaceAutoCloseable- Throws:
IOException- if an I/O error occurs while closing the serial port.
-
isClosed
public boolean isClosed()
Returns the closed state of this connection.- Returns:
trueif this connection has been closed.
-
-