public class SerialImpl extends AbstractSerialDataReaderWriter implements Serial
This implementation class implements the 'Serial' interface using the WiringPi Serial library.
Before using the Pi4J library, you need to ensure that the Java VM in configured with access to the following system libraries:
This library depends on the wiringPi native system library. (developed by Gordon Henderson @ http://wiringpi.com/)
Serial
,
SerialDataEvent
,
SerialDataEventListener
,
SerialFactory
,
http://www.pi4j.com/Modifier and Type | Field and Description |
---|---|
protected boolean |
bufferingDataReceived |
protected ExecutorService |
executor |
protected int |
fileDescriptor |
protected CopyOnWriteArrayList<SerialDataEventListener> |
listeners |
protected SerialByteBuffer |
receiveBuffer |
DEFAULT_COM_PORT, FIRST_USB_COM_PORT, SECOND_USB_COM_PORT
Constructor and Description |
---|
SerialImpl()
default constructor
|
Modifier and Type | Method and Description |
---|---|
void |
addListener(SerialDataEventListener... listener)
Add Serial Event Listener
|
int |
available()
Gets the number of bytes available for reading, or -1 for any error condition.
|
void |
close()
This method is called to close a currently open open serial port.
|
void |
discardAll()
Discards any data in both the serial receive and transmit buffers.
|
void |
discardInput()
Discards any data in the serial receive (input) buffer.
|
void |
discardOutput()
Discards any data in the serial transmit (output) buffer.
|
void |
flush()
Forces the transmission of any remaining data in the serial port transmit buffer.
|
boolean |
getCD()
Get the CD (carrier-detect) pin state.
|
boolean |
getCTS()
Get the CTS (clean-to-send) pin state.
|
boolean |
getDSR()
Get the DSR (data-set-ready) pin state.
|
boolean |
getDTR()
Get the DTR (data-terminal-ready) pin state.
|
int |
getFileDescriptor()
This method returns the serial device file descriptor
|
InputStream |
getInputStream()
This method returns the input data stream for the serial port's receive buffer
|
OutputStream |
getOutputStream()
This method returns the output data stream for the serial port's transmit buffer
|
boolean |
getRI()
Get the RI (ring-indicator) pin state.
|
boolean |
getRTS()
Get the RTS (request-to-send) pin state.
|
boolean |
isBufferingDataReceived()
This method returns the buffering state for data received from the serial device/port.
|
boolean |
isClosed()
This method is called to determine if the serial port is already closed.
|
boolean |
isOpen()
This method is called to determine if the serial port is already open.
|
void |
open(SerialConfig serialConfig)
This opens and initializes the serial port/device and sets the communication parameters.
|
void |
open(String device,
Baud baud,
DataBits dataBits,
Parity parity,
StopBits stopBits,
FlowControl flowControl)
This opens and initializes the serial port/device and sets the communication parameters.
|
void |
open(String device,
int baud)
This opens and initializes the serial port/device and sets the communication parameters.
|
void |
open(String device,
int baud,
int dataBits,
int parity,
int stopBits,
int flowControl)
This opens and initializes the serial port/device and sets the communication parameters.
|
byte[] |
read()
Reads all available bytes from the serial port/device.
|
byte[] |
read(int length)
Reads a length of bytes from the port/serial device.
|
void |
removeListener(SerialDataEventListener... listener)
Remove Serial Event Listener
|
void |
sendBreak()
Send a BREAK signal to connected device for at least 0.25 seconds, and not more than 0.5 seconds
|
void |
sendBreak(int duration)
Send a BREAK signal to connected device.
|
void |
setBreak(boolean enabled)
Send a constant BREAK signal to connected device.
|
void |
setBufferingDataReceived(boolean enabled)
This method controls the buffering state for data received from the serial device/port.
|
void |
setDTR(boolean enabled)
Control the DTR (data-terminal-ready) pin state.
|
void |
setRTS(boolean enabled)
Control the RTS (request-to-send) pin state.
|
void |
write(byte[] data,
int offset,
int length)
Sends an array of bytes to the serial port/device identified by the given file descriptor.
|
discardData, read, read, read, read, read, read, read, read, read, read
appendNewLine, write, write, write, write, write, write, write, write, write, write, write, write, write, writeln, writeln, writeln, writeln
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
protected int fileDescriptor
protected final CopyOnWriteArrayList<SerialDataEventListener> listeners
protected final ExecutorService executor
protected final SerialByteBuffer receiveBuffer
protected boolean bufferingDataReceived
public SerialImpl()
public void open(String device, int baud, int dataBits, int parity, int stopBits, int flowControl) throws IOException
This opens and initializes the serial port/device and sets the communication parameters. It sets the port into raw mode (character at a time and no translations).
(ATTENTION: the 'device' argument can only be a maximum of 128 characters.)
open
in interface Serial
device
- The device address of the serial port to access. You can use constant
'DEFAULT_COM_PORT' if you wish to access the default serial port provided via the
GPIO header.baud
- The baud rate to use with the serial port. (Custom baud rate are not supported)dataBits
- The data bits to use for serial communication. (5,6,7,8)parity
- The parity setting to use for serial communication. (None, Event, Odd, Mark, Space)stopBits
- The stop bits to use for serial communication. (1,2)flowControl
- The flow control option to use for serial communication. (none, hardware, software)IOException
- thrown on any error.Serial.DEFAULT_COM_PORT
public void open(String device, int baud) throws IOException
This opens and initializes the serial port/device and sets the communication parameters. It sets the port into raw mode (character at a time and no translations). This method will use the following default serial configuration parameters: - DATA BITS = 8 - PARITY = NONE - STOP BITS = 1 - FLOW CONTROL = NONE
(ATTENTION: the 'device' argument can only be a maximum of 128 characters.)
open
in interface Serial
device
- The device address of the serial port to access. You can use constant
'DEFAULT_COM_PORT' if you wish to access the default serial port provided via the
GPIO header.baud
- The baud rate to use with the serial port.IOException
- thrown on any error.Serial.DEFAULT_COM_PORT
public void open(String device, Baud baud, DataBits dataBits, Parity parity, StopBits stopBits, FlowControl flowControl) throws IOException
This opens and initializes the serial port/device and sets the communication parameters. It sets the port into raw mode (character at a time and no translations).
(ATTENTION: the 'device' argument can only be a maximum of 128 characters.)
open
in interface Serial
device
- The device address of the serial port to access. You can use constant
'DEFAULT_COM_PORT' if you wish to access the default serial port provided via the
GPIO header.baud
- The baud rate to use with the serial port.dataBits
- The data bits to use for serial communication. (5,6,7,8)parity
- The parity setting to use for serial communication. (None, Event, Odd, Mark, Space)stopBits
- The stop bits to use for serial communication. (1,2)flowControl
- The flow control option to use for serial communication. (none, hardware, software)IOException
- thrown on any error.Serial.DEFAULT_COM_PORT
public void open(SerialConfig serialConfig) throws IOException
This opens and initializes the serial port/device and sets the communication parameters. It sets the port into raw mode (character at a time and no translations).
(ATTENTION: the 'device' argument can only be a maximum of 128 characters.)
open
in interface Serial
serialConfig
- A serial configuration object that contains the device, baud rate, data bits, parity,
stop bits, and flow control settings.IOException
- thrown on any error.Serial.DEFAULT_COM_PORT
public boolean isOpen()
isOpen
in interface Serial
open(String, int)
public boolean isClosed()
isClosed
in interface Serial
open(String, int)
public void close() throws IllegalStateException, IOException
close
in interface Serial
close
in interface AutoCloseable
IllegalStateException
- thrown if the serial port is not already open.IOException
- thrown on any error.public void flush() throws IllegalStateException, IOException
Forces the transmission of any remaining data in the serial port transmit buffer. Please note that this does not force the transmission of data, it discards it!
flush
in interface Serial
IllegalStateException
- thrown if the serial port is not already open.IOException
- thrown on any error.public void discardInput() throws IllegalStateException, IOException
Discards any data in the serial receive (input) buffer. Please note that this does not force the transmission of data, it discards it!
discardInput
in interface Serial
IllegalStateException
- thrown if the serial port is not already open.IOException
- thrown on any error.public void discardOutput() throws IllegalStateException, IOException
Discards any data in the serial transmit (output) buffer. Please note that this does not force the transmission of data, it discards it!
discardOutput
in interface Serial
IllegalStateException
- thrown if the serial port is not already open.IOException
- thrown on any error.public void discardAll() throws IllegalStateException, IOException
Discards any data in both the serial receive and transmit buffers. Please note that this does not force the transmission of data, it discards it!
discardAll
in interface Serial
IllegalStateException
- thrown if the serial port is not already open.IOException
- thrown on any error.public void sendBreak(int duration) throws IllegalStateException, IOException
Send a BREAK signal to connected device.
sendBreak
in interface Serial
duration
- The length of time (milliseconds) to send the BREAK signalIllegalStateException
- thrown if the serial port is not already open.IOException
- thrown on any error.public void sendBreak() throws IllegalStateException, IOException
Send a BREAK signal to connected device for at least 0.25 seconds, and not more than 0.5 seconds
sendBreak
in interface Serial
IllegalStateException
- thrown if the serial port is not already open.IOException
- thrown on any error.public void setBreak(boolean enabled) throws IllegalStateException, IOException
Send a constant BREAK signal to connected device. (Turn break on/off) When enabled this will send a steady stream of zero bits. When enabled, no (other) data transmitting is possible.
setBreak
in interface Serial
enabled
- The enable or disable state to control the BREAK signalIllegalStateException
- thrown if the serial port is not already open.IOException
- thrown on any error.public void setRTS(boolean enabled) throws IllegalStateException, IOException
Control the RTS (request-to-send) pin state. When enabled this will set the RTS pin to the HIGH state.
setRTS
in interface Serial
enabled
- The enable or disable state to control the RTS pin state.IllegalStateException
- thrown if the serial port is not already open.IOException
- thrown on any error.public void setDTR(boolean enabled) throws IllegalStateException, IOException
Control the DTR (data-terminal-ready) pin state. When enabled this will set the DTR pin to the HIGH state.
setDTR
in interface Serial
enabled
- The enable or disable state to control the RTS pin state.IllegalStateException
- thrown if the serial port is not already open.IOException
- thrown on any error.public boolean getRTS() throws IllegalStateException, IOException
Get the RTS (request-to-send) pin state.
getRTS
in interface Serial
IllegalStateException
- thrown if the serial port is not already open.IOException
- thrown on any error.public boolean getDTR() throws IllegalStateException, IOException
Get the DTR (data-terminal-ready) pin state.
getDTR
in interface Serial
IllegalStateException
- thrown if the serial port is not already open.IOException
- thrown on any error.public boolean getCTS() throws IllegalStateException, IOException
Get the CTS (clean-to-send) pin state.
getCTS
in interface Serial
IllegalStateException
- thrown if the serial port is not already open.IOException
- thrown on any error.public boolean getDSR() throws IllegalStateException, IOException
Get the DSR (data-set-ready) pin state.
getDSR
in interface Serial
IllegalStateException
- thrown if the serial port is not already open.IOException
- thrown on any error.public boolean getRI() throws IllegalStateException, IOException
Get the RI (ring-indicator) pin state.
getRI
in interface Serial
IllegalStateException
- thrown if the serial port is not already open.IOException
- thrown on any error.public boolean getCD() throws IllegalStateException, IOException
Get the CD (carrier-detect) pin state.
getCD
in interface Serial
IllegalStateException
- thrown if the serial port is not already open.IOException
- thrown on any error.public int available() throws IllegalStateException, IOException
available
in interface SerialDataReader
available
in class AbstractSerialDataReaderWriter
IllegalStateException
- thrown if the serial port is not already open.IOException
- thrown on any error.public byte[] read() throws IllegalStateException, IOException
Reads all available bytes from the serial port/device.
read
in interface SerialDataReader
read
in class AbstractSerialDataReaderWriter
IllegalStateException
- thrown if the serial port is not already open.IOException
- thrown on any error.public byte[] read(int length) throws IllegalStateException, IOException
Reads a length of bytes from the port/serial device.
read
in interface SerialDataReader
read
in class AbstractSerialDataReaderWriter
length
- The number of bytes to get from the serial port/device.
This number must not be higher than the number of available bytes.IllegalStateException
- thrown if the serial port is not already open.IOException
- thrown on any error.public void write(byte[] data, int offset, int length) throws IllegalStateException, IOException
Sends an array of bytes to the serial port/device identified by the given file descriptor.
write
in interface SerialDataWriter
write
in class AbstractSerialDataWriter
data
- A ByteBuffer of data to be transmitted.offset
- The starting index (inclusive) in the array to send from.length
- The number of bytes from the byte array to transmit to the serial port.IllegalStateException
- thrown if the serial port is not already open.IOException
- thrown on any error.public void addListener(SerialDataEventListener... listener)
Add Serial Event Listener
Java consumer code can call this method to register itself as a listener for serial data events.
addListener
in interface Serial
listener
- A class instance that implements the SerialListener interface.SerialDataEventListener
,
SerialDataEvent
public void removeListener(SerialDataEventListener... listener)
Remove Serial Event Listener
Java consumer code can call this method to unregister itself as a listener for serial data events.
removeListener
in interface Serial
listener
- A class instance that implements the SerialListener interface.SerialDataEventListener
,
SerialDataEvent
public int getFileDescriptor()
getFileDescriptor
in interface Serial
public InputStream getInputStream()
getInputStream
in interface Serial
public OutputStream getOutputStream()
getOutputStream
in interface Serial
public boolean isBufferingDataReceived()
isBufferingDataReceived
in interface Serial
public void setBufferingDataReceived(boolean enabled)
This method controls the buffering state for data received from the serial device/port.
If the buffering state is enabled, then all data bytes received from the serial port will get copied into a data receive buffer. You can use the 'getInputStream()' or and of the 'read()' methods to access this data. The data will also be available via the 'SerialDataEvent' event. It is important to know that if you are using data buffering, the data will continue to grow in memory until your program consume it from the data reader/stream.
If the buffering state is disabled, then all data bytes received from the serial port will NOT get copied into the data receive buffer, but will be included in the 'SerialDataEvent' event's data payload. If you program does not care about or use data received from the serial port, then you should disable the data buffering state to prevent memory waste/leak.
setBufferingDataReceived
in interface Serial
enabled
- sets the buffering behavior stateCopyright © 2012–2019 Pi4J. All rights reserved.