Package com.diozero.api
Class SerialDevice
java.lang.Object
com.diozero.api.SerialDevice
- All Implemented Interfaces:
DeviceInterface
,SerialConstants
,SerialDeviceInterface
,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)
-
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 filenamestatic List<SerialDevice.DeviceInfo>
getLocalSerialDevices()
Attempt to discover the locally attached serial devices using Linux device tree.int
read()
Read a single byte returning error responsesint
read(byte[] buffer)
Attempt to read buffer.length bytes into the specified buffer, returning the number of bytes actually read; throw a RuntimeIOException if an I/O error occursbyte
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
-
-
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
-
close
public void close()Close this device- Specified by:
close
in interfaceAutoCloseable
- 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, returning the number of bytes actually read; throw a RuntimeIOException if an I/O error occurs- 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
-