Class SerialImpl
- java.lang.Object
-
- com.pi4j.io.serial.impl.AbstractSerialDataWriter
-
- com.pi4j.io.serial.impl.AbstractSerialDataReaderWriter
-
- com.pi4j.io.serial.impl.SerialImpl
-
- All Implemented Interfaces:
Serial,SerialDataReader,SerialDataWriter,AutoCloseable
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:
- pi4j
- wiringPi
This library depends on the wiringPi native system library. (developed by Gordon Henderson @ http://wiringpi.com/)
- Author:
- Robert Savage (http://www.savagehomeautomation.com)
- See Also:
Serial,SerialDataEvent,SerialDataEventListener,SerialFactory, https://pi4j.com/
-
-
Field Summary
Fields Modifier and Type Field Description protected booleanbufferingDataReceivedprotected ExecutorServiceexecutorprotected intfileDescriptorprotected CopyOnWriteArrayList<SerialDataEventListener>listenersprotected SerialByteBufferreceiveBuffer-
Fields inherited from interface com.pi4j.io.serial.Serial
DEFAULT_COM_PORT, FIRST_USB_COM_PORT, PRIMARY_COM_PORT, SECOND_USB_COM_PORT, SECONDARY_COM_PORT
-
-
Constructor Summary
Constructors Constructor Description SerialImpl()default constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddListener(SerialDataEventListener... listener)Add Serial Event Listenerintavailable()Gets the number of bytes available for reading, or -1 for any error condition.voidclose()This method is called to close a currently open open serial port.voiddiscardAll()Discards any data in both the serial receive and transmit buffers.voiddiscardInput()Discards any data in the serial receive (input) buffer.voiddiscardOutput()Discards any data in the serial transmit (output) buffer.voidflush()Forces the transmission of any remaining data in the serial port transmit buffer.booleangetCD()Get the CD (carrier-detect) pin state.booleangetCTS()Get the CTS (clean-to-send) pin state.booleangetDSR()Get the DSR (data-set-ready) pin state.booleangetDTR()Get the DTR (data-terminal-ready) pin state.intgetFileDescriptor()This method returns the serial device file descriptorInputStreamgetInputStream()This method returns the input data stream for the serial port's receive bufferOutputStreamgetOutputStream()This method returns the output data stream for the serial port's transmit bufferbooleangetRI()Get the RI (ring-indicator) pin state.booleangetRTS()Get the RTS (request-to-send) pin state.booleanisBufferingDataReceived()This method returns the buffering state for data received from the serial device/port.booleanisClosed()This method is called to determine if the serial port is already closed.booleanisOpen()This method is called to determine if the serial port is already open.voidopen(SerialConfig serialConfig)This opens and initializes the serial port/device and sets the communication parameters.voidopen(String device, int baud)This opens and initializes the serial port/device and sets the communication parameters.voidopen(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.voidopen(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.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.voidremoveListener(SerialDataEventListener... listener)Remove Serial Event ListenervoidsendBreak()Send a BREAK signal to connected device for at least 0.25 seconds, and not more than 0.5 secondsvoidsendBreak(int duration)Send a BREAK signal to connected device.voidsetBreak(boolean enabled)Send a constant BREAK signal to connected device.voidsetBufferingDataReceived(boolean enabled)This method controls the buffering state for data received from the serial device/port.voidsetDTR(boolean enabled)Control the DTR (data-terminal-ready) pin state.voidsetRTS(boolean enabled)Control the RTS (request-to-send) pin state.voidwrite(byte[] data, int offset, int length)Sends an array of bytes to the serial port/device identified by the given file descriptor.-
Methods inherited from class com.pi4j.io.serial.impl.AbstractSerialDataReaderWriter
discardData, read, read, read, read, read, read, read, read, read, read
-
Methods inherited from class com.pi4j.io.serial.impl.AbstractSerialDataWriter
appendNewLine, write, write, write, write, write, write, write, write, write, write, write, write, write, writeln, writeln, writeln, writeln
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
-
-
Field Detail
-
fileDescriptor
protected int fileDescriptor
-
listeners
protected final CopyOnWriteArrayList<SerialDataEventListener> listeners
-
executor
protected final ExecutorService executor
-
receiveBuffer
protected final SerialByteBuffer receiveBuffer
-
bufferingDataReceived
protected boolean bufferingDataReceived
-
-
Method Detail
-
open
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.)
- Specified by:
openin interfaceSerial- Parameters:
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)- Throws:
IOException- thrown on any error.- See Also:
Serial.DEFAULT_COM_PORT
-
open
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.)
- Specified by:
openin interfaceSerial- Parameters:
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.- Throws:
IOException- thrown on any error.- See Also:
Serial.DEFAULT_COM_PORT
-
open
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.)
- Specified by:
openin interfaceSerial- Parameters:
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)- Throws:
IOException- thrown on any error.- See Also:
Serial.DEFAULT_COM_PORT
-
open
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.)
- Specified by:
openin interfaceSerial- Parameters:
serialConfig- A serial configuration object that contains the device, baud rate, data bits, parity, stop bits, and flow control settings.- Throws:
IOException- thrown on any error.- See Also:
Serial.DEFAULT_COM_PORT
-
isOpen
public boolean isOpen()
This method is called to determine if the serial port is already open.- Specified by:
isOpenin interfaceSerial- Returns:
- a value of 'true' is returned if the serial port is already open.
- See Also:
open(String, int)
-
isClosed
public boolean isClosed()
This method is called to determine if the serial port is already closed.- Specified by:
isClosedin interfaceSerial- Returns:
- a value of 'true' is returned if the serial port is already in the closed state.
- See Also:
open(String, int)
-
close
public void close() throws IllegalStateException, IOExceptionThis method is called to close a currently open open serial port.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceSerial- Throws:
IllegalStateException- thrown if the serial port is not already open.IOException- thrown on any error.
-
flush
public void flush() throws IllegalStateException, IOExceptionForces the transmission of any remaining data in the serial port transmit buffer.
- Specified by:
flushin interfaceSerial- Throws:
IllegalStateException- thrown if the serial port is not already open.IOException- thrown on any error.
-
discardInput
public void discardInput() throws IllegalStateException, IOExceptionDiscards any data in the serial receive (input) buffer.
- Specified by:
discardInputin interfaceSerial- Throws:
IllegalStateException- thrown if the serial port is not already open.IOException- thrown on any error.
-
discardOutput
public void discardOutput() throws IllegalStateException, IOExceptionDiscards any data in the serial transmit (output) buffer.
- Specified by:
discardOutputin interfaceSerial- Throws:
IllegalStateException- thrown if the serial port is not already open.IOException- thrown on any error.
-
discardAll
public void discardAll() throws IllegalStateException, IOExceptionDiscards any data in both the serial receive and transmit buffers.
- Specified by:
discardAllin interfaceSerial- Throws:
IllegalStateException- thrown if the serial port is not already open.IOException- thrown on any error.
-
sendBreak
public void sendBreak(int duration) throws IllegalStateException, IOExceptionSend a BREAK signal to connected device.
- Specified by:
sendBreakin interfaceSerial- Parameters:
duration- The length of time (milliseconds) to send the BREAK signal- Throws:
IllegalStateException- thrown if the serial port is not already open.IOException- thrown on any error.
-
sendBreak
public void sendBreak() throws IllegalStateException, IOExceptionSend a BREAK signal to connected device for at least 0.25 seconds, and not more than 0.5 seconds
- Specified by:
sendBreakin interfaceSerial- Throws:
IllegalStateException- thrown if the serial port is not already open.IOException- thrown on any error.
-
setBreak
public void setBreak(boolean enabled) throws IllegalStateException, IOExceptionSend 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.
- Specified by:
setBreakin interfaceSerial- Parameters:
enabled- The enable or disable state to control the BREAK signal- Throws:
IllegalStateException- thrown if the serial port is not already open.IOException- thrown on any error.
-
setRTS
public void setRTS(boolean enabled) throws IllegalStateException, IOExceptionControl the RTS (request-to-send) pin state. When enabled this will set the RTS pin to the HIGH state.
- Specified by:
setRTSin interfaceSerial- Parameters:
enabled- The enable or disable state to control the RTS pin state.- Throws:
IllegalStateException- thrown if the serial port is not already open.IOException- thrown on any error.
-
setDTR
public void setDTR(boolean enabled) throws IllegalStateException, IOExceptionControl the DTR (data-terminal-ready) pin state. When enabled this will set the DTR pin to the HIGH state.
- Specified by:
setDTRin interfaceSerial- Parameters:
enabled- The enable or disable state to control the RTS pin state.- Throws:
IllegalStateException- thrown if the serial port is not already open.IOException- thrown on any error.
-
getRTS
public boolean getRTS() throws IllegalStateException, IOExceptionGet the RTS (request-to-send) pin state.
- Specified by:
getRTSin interfaceSerial- Throws:
IllegalStateException- thrown if the serial port is not already open.IOException- thrown on any error.
-
getDTR
public boolean getDTR() throws IllegalStateException, IOExceptionGet the DTR (data-terminal-ready) pin state.
- Specified by:
getDTRin interfaceSerial- Throws:
IllegalStateException- thrown if the serial port is not already open.IOException- thrown on any error.
-
getCTS
public boolean getCTS() throws IllegalStateException, IOExceptionGet the CTS (clean-to-send) pin state.
- Specified by:
getCTSin interfaceSerial- Throws:
IllegalStateException- thrown if the serial port is not already open.IOException- thrown on any error.
-
getDSR
public boolean getDSR() throws IllegalStateException, IOExceptionGet the DSR (data-set-ready) pin state.
- Specified by:
getDSRin interfaceSerial- Throws:
IllegalStateException- thrown if the serial port is not already open.IOException- thrown on any error.
-
getRI
public boolean getRI() throws IllegalStateException, IOExceptionGet the RI (ring-indicator) pin state.
- Specified by:
getRIin interfaceSerial- Throws:
IllegalStateException- thrown if the serial port is not already open.IOException- thrown on any error.
-
getCD
public boolean getCD() throws IllegalStateException, IOExceptionGet the CD (carrier-detect) pin state.
- Specified by:
getCDin interfaceSerial- Throws:
IllegalStateException- thrown if the serial port is not already open.IOException- thrown on any error.
-
available
public int available() throws IllegalStateException, IOExceptionGets the number of bytes available for reading, or -1 for any error condition.- Specified by:
availablein interfaceSerialDataReader- Specified by:
availablein classAbstractSerialDataReaderWriter- Returns:
- Returns the number of bytes available for reading, or -1 for any error
- Throws:
IllegalStateException- thrown if the serial port is not already open.IOException- thrown on any error.
-
read
public byte[] read() throws IllegalStateException, IOExceptionReads all available bytes from the serial port/device.
- Specified by:
readin interfaceSerialDataReader- Specified by:
readin classAbstractSerialDataReaderWriter- Returns:
- Returns a byte array with the data read from the serial port.
- Throws:
IllegalStateException- thrown if the serial port is not already open.IOException- thrown on any error.
-
read
public byte[] read(int length) throws IllegalStateException, IOExceptionReads a length of bytes from the port/serial device.
- Specified by:
readin interfaceSerialDataReader- Specified by:
readin classAbstractSerialDataReaderWriter- Parameters:
length- The number of bytes to get from the serial port/device. This number must not be higher than the number of available bytes.- Returns:
- Returns a byte array with the data read from the serial port.
- Throws:
IllegalStateException- thrown if the serial port is not already open.IOException- thrown on any error.
-
write
public void write(byte[] data, int offset, int length) throws IllegalStateException, IOExceptionSends an array of bytes to the serial port/device identified by the given file descriptor.
- Specified by:
writein interfaceSerialDataWriter- Specified by:
writein classAbstractSerialDataWriter- Parameters:
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.- Throws:
IllegalStateException- thrown if the serial port is not already open.IOException- thrown on any error.
-
addListener
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.
- Specified by:
addListenerin interfaceSerial- Parameters:
listener- A class instance that implements the SerialListener interface.- See Also:
SerialDataEventListener,SerialDataEvent
-
removeListener
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.
- Specified by:
removeListenerin interfaceSerial- Parameters:
listener- A class instance that implements the SerialListener interface.- See Also:
SerialDataEventListener,SerialDataEvent
-
getFileDescriptor
public int getFileDescriptor()
This method returns the serial device file descriptor- Specified by:
getFileDescriptorin interfaceSerial- Returns:
- fileDescriptor file descriptor
-
getInputStream
public InputStream getInputStream()
This method returns the input data stream for the serial port's receive buffer- Specified by:
getInputStreamin interfaceSerial- Returns:
- InputStream input stream
-
getOutputStream
public OutputStream getOutputStream()
This method returns the output data stream for the serial port's transmit buffer- Specified by:
getOutputStreamin interfaceSerial- Returns:
- OutputStream output stream
-
isBufferingDataReceived
public boolean isBufferingDataReceived()
This method returns the buffering state for data received from the serial device/port.- Specified by:
isBufferingDataReceivedin interfaceSerial- Returns:
- 'true' if buffering is enabled; else 'false'
-
setBufferingDataReceived
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.
- Specified by:
setBufferingDataReceivedin interfaceSerial- Parameters:
enabled- sets the buffering behavior state
-
-