Package com.diozero.api
Class SerialDevice
java.lang.Object
com.diozero.api.SerialDevice
- All Implemented Interfaces:
DeviceInterface
,SerialConstants
,SerialDeviceInterface
,Closeable
,AutoCloseable
public class SerialDevice extends Object implements SerialConstants, SerialDeviceInterface
Serial device. The SerialDevice represents serial devices connected via USB
or via the serial RX/TX pins on the GPIO header.
On the Raspberry Pi, to use the serial RX/TX pins on the GPIO header, the serial interface must be enabled and the login shell must be disabled. The device file name for the serial RX/TX pins is /dev/serial0. See Raspberry Pi UART configuration for additional detail.
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SerialDevice.Builder
Serial device builder.static class
SerialDevice.DeviceInfo
Provides descriptive information for a connected serial device.Nested classes/interfaces inherited from interface com.diozero.api.SerialConstants
SerialConstants.DataBits, SerialConstants.Parity, SerialConstants.ReadMode, SerialConstants.StopBits
-
Field Summary
Fields inherited from interface com.diozero.api.SerialConstants
BAUD_1000000, BAUD_110, BAUD_115200, BAUD_1152000, BAUD_1200, BAUD_134, BAUD_150, BAUD_1500000, BAUD_1800, BAUD_19200, BAUD_200, BAUD_2000000, BAUD_230400, BAUD_2400, BAUD_2500000, BAUD_300, BAUD_3000000, BAUD_3500000, BAUD_38400, BAUD_4000000, BAUD_460800, BAUD_4800, BAUD_50, BAUD_500000, BAUD_57600, BAUD_576000, BAUD_600, BAUD_921600, BAUD_9600, BAUD_B75, DEFAULT_BAUD, DEFAULT_DATA_BITS, DEFAULT_MIN_READ_CHARS, DEFAULT_PARITY, DEFAULT_READ_BLOCKING, DEFAULT_READ_TIMEOUT_MILLIS, DEFAULT_STOP_BITS, READ_TIMEOUT
-
Constructor Summary
Constructors Constructor Description SerialDevice(String deviceFilename)
Create a new serial device using default values forbaud
,data bits
,stop bits
,parity
,read blocking
,min read chars
andread timeout
SerialDevice(String deviceFilename, int baud, SerialConstants.DataBits dataBits, SerialConstants.StopBits stopBits, SerialConstants.Parity parity)
SerialDevice(String deviceFilename, int baud, SerialConstants.DataBits dataBits, SerialConstants.StopBits stopBits, SerialConstants.Parity parity, boolean readBlocking, int minReadChars, int readTimeoutMillis)
Create a new serial device -
Method Summary
Modifier and Type Method Description static SerialDevice.Builder
builder(String deviceFilename)
int
bytesAvailable()
Get the number of bytes that are available to be readvoid
close()
Close this deviceString
getDeviceFilename()
Get the device filenameString
getKey()
Get the unique device identifier for this devicestatic List<SerialDevice.DeviceInfo>
getLocalSerialDevices()
Attempt to discover the locally attached serial devices using Linux device tree.boolean
isOpen()
Check if this device is open or closedint
read()
Read a single byte returning error responsesint
read(byte[] buffer)
Attempt to read buffer.length bytes into the specified buffer, throw an exception if unable to read any databyte
readByte()
Read a single byte, throw an exception if unable to read any datavoid
write(byte... buffer)
Write the byte buffer to the devicevoid
writeByte(byte bVal)
Write a single byte, throw an exception if unable to write the data
-
Constructor Details
-
SerialDevice
Create a new serial device using default values forbaud
,data bits
,stop bits
,parity
,read blocking
,min read chars
andread timeout
- Parameters:
deviceFilename
- The O/S file name for the device, e.g. /dev/ttyACM0- Throws:
RuntimeIOException
- If an I/O error occurs
-
SerialDevice
public SerialDevice(String deviceFilename, int baud, SerialConstants.DataBits dataBits, SerialConstants.StopBits stopBits, SerialConstants.Parity parity) throws RuntimeIOException- Parameters:
deviceFilename
- The O/S file name for the device, e.g. /dev/ttyACM0baud
- Baud rate, seeSerialConstants
for valid baud rate valuesdataBits
- Number ofdata bits
stopBits
- Number ofstop bits
parity
- Device error detectionparity
- Throws:
RuntimeIOException
- If an I/O error occurs
-
SerialDevice
public SerialDevice(String deviceFilename, int baud, SerialConstants.DataBits dataBits, SerialConstants.StopBits stopBits, SerialConstants.Parity parity, boolean readBlocking, int minReadChars, int readTimeoutMillis) throws RuntimeIOExceptionCreate a new serial device- Parameters:
deviceFilename
- The O/S file name for the device, e.g. /dev/ttyACM0baud
- Baud rate, seeSerialConstants
for valid baud rate valuesdataBits
- Number ofdata bits
stopBits
- Number ofstop bits
parity
- Device error detectionparity
readBlocking
- Should all read operations block until data is available?minReadChars
- Minimum number of characters to read (note actually an unsigned char hence max value is 255)readTimeoutMillis
- The read timeout value in milliseconds (note converted to tenths of a second as an unsigned char)- Throws:
RuntimeIOException
- If an I/O error occurs
-
-
Method Details
-
getLocalSerialDevices
Attempt to discover the locally attached serial devices using Linux device tree.- Returns:
- A list of locally attached devices
-
builder
-
getDeviceFilename
Get the device filename- Returns:
- the device filename, e.g. /dev/ttyUSB0
-
getKey
Get the unique device identifier for this device- Specified by:
getKey
in interfaceDeviceInterface
- Returns:
- unique device identifier
-
isOpen
public boolean isOpen()Check if this device is open or closed- Specified by:
isOpen
in interfaceDeviceInterface
- Returns:
- true if this device is open
-
close
public void close()Close this device- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceDeviceInterface
-
read
Read a single byte returning error responses- Specified by:
read
in interfaceSerialDeviceInterface
- Returns:
- Signed integer representation of the data read, including error responses (values < 0)
- Throws:
RuntimeIOException
- if an I/O error occurs
-
readByte
Read a single byte, throw an exception if unable to read any data- Specified by:
readByte
in interfaceSerialDeviceInterface
- Returns:
- The data read
- Throws:
RuntimeIOException
- if an I/O error occurs
-
writeByte
Write a single byte, throw an exception if unable to write the data- Specified by:
writeByte
in interfaceSerialDeviceInterface
- Parameters:
bVal
- The data to write- Throws:
RuntimeIOException
- if an I/O error occurs
-
read
Attempt to read buffer.length bytes into the specified buffer, throw an exception if unable to read any data- Specified by:
read
in interfaceSerialDeviceInterface
- Parameters:
buffer
- The buffer to read into, the length of this buffer specifies the number of bytes to read- Returns:
- The number of bytes read
- Throws:
RuntimeIOException
- if an I/O error occurs
-
write
Write the byte buffer to the device- Specified by:
write
in interfaceSerialDeviceInterface
- Parameters:
buffer
- The data to write- Throws:
RuntimeIOException
- if an I/O error occurs
-
bytesAvailable
Get the number of bytes that are available to be read- Specified by:
bytesAvailable
in interfaceSerialDeviceInterface
- Returns:
- The number of bytes that are available to read
- Throws:
RuntimeIOException
- if an I/O error occurs
-