public class SerialImpl extends Object 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
,
SerialDataListener
,
SerialFactory
,
http://www.pi4j.com/Modifier and Type | Field and Description |
---|---|
protected int |
fileDescriptor |
protected boolean |
isshutdown |
protected CopyOnWriteArrayList<SerialDataListener> |
listeners |
protected SerialDataMonitorThread |
monitor |
protected int |
monitorInterval |
DEFAULT_COM_PORT, DEFAULT_MONITOR_INTERVAL
Constructor and Description |
---|
SerialImpl() |
Modifier and Type | Method and Description |
---|---|
void |
addListener(SerialDataListener... listener)
Add Serial Event Listener
|
int |
availableBytes()
This method is called to determine if and how many bytes are available on the serial received
data buffer.
|
void |
close()
This method is called to close a currently open open serial port.
|
void |
flush()
This method is called to immediately flush the serial data transmit buffer and force any
pending data to be sent to the serial port immediately.
|
int |
getMonitorInterval()
This method returns the serial data receive monitor delay interval in milliseconds.
|
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.
|
boolean |
isShutdown()
This method returns TRUE if the serial interface has been shutdown.
|
void |
open(String device,
int baudRate)
This method is call to open a serial port for communication.
|
char |
read()
This method will read the next character available from the serial port receive buffer.
|
void |
removeListener(SerialDataListener... listener)
Remove Serial Event Listener
|
void |
setMonitorInterval(int interval)
This method set the serial data receive monitor delay interval in milliseconds.
|
void |
shutdown()
This method can be called to forcefully shutdown all
serial data monitoring threads.
|
void |
write(byte data)
This method is called to submit a single byte of data to the serial port transmit buffer.
|
void |
write(byte[] data)
This method is called to submit a byte array of data to the serial port transmit buffer.
|
void |
write(char data)
This method is called to submit a single character of data to the serial port transmit
buffer.
|
void |
write(char[] data)
This method is called to submit a character array of data to the serial port transmit buffer.
|
void |
write(String data)
This method is called to submit a string of data to the serial port transmit buffer.
|
void |
write(String data,
String... args)
This method is called to submit a string of formatted data to the serial port transmit
buffer.
|
void |
writeln(String data)
This method is called to submit a string of data with trailing CR + LF characters to the
serial port transmit buffer.
|
void |
writeln(String data,
String... args)
This method is called to submit a string of formatted data with trailing CR + LF characters
to the serial port transmit buffer.
|
protected int fileDescriptor
protected final CopyOnWriteArrayList<SerialDataListener> listeners
protected SerialDataMonitorThread monitor
protected int monitorInterval
protected boolean isshutdown
public SerialImpl()
public void open(String device, int baudRate) throws SerialPortException
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.baudRate
- The baud rate to use with the serial port.SerialPortException
- Exception 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
close
in interface Serial
IllegalStateException
public void flush() throws IllegalStateException
flush
in interface Serial
IllegalStateException
public char read() throws IllegalStateException
This method will read the next character available from the serial port receive buffer.
NOTE: If a serial data listener has been implemented and registered with this class, then this method should not be called directly. A background thread will be running to collect received data from the serial port receive buffer and the received data will be available on via the event.
read
in interface Serial
IllegalStateException
public void write(char data) throws IllegalStateException
write
in interface Serial
data
- A single character to be transmitted.IllegalStateException
public void write(char[] data) throws IllegalStateException
write
in interface Serial
data
- A character array of data to be transmitted.IllegalStateException
public void write(byte data) throws IllegalStateException
write
in interface Serial
data
- A single byte to be transmitted.IllegalStateException
public void write(byte[] data) throws IllegalStateException
write
in interface Serial
data
- A byte array of data to be transmitted.IllegalStateException
public void write(String data) throws IllegalStateException
write
in interface Serial
data
- A string of data to be transmitted.IllegalStateException
public void writeln(String data) throws IllegalStateException
writeln
in interface Serial
data
- A string of data to be transmitted.IllegalStateException
public void write(String data, String... args) throws IllegalStateException
write
in interface Serial
data
- A string of formatted data to be transmitted.args
- A series of arguments that can be included for the format string variable
replacements.IllegalStateException
public void writeln(String data, String... args) throws IllegalStateException
writeln
in interface Serial
data
- A string of formatted data to be transmitted.args
- A series of arguments that can be included for the format string variable
replacements.IllegalStateException
public int availableBytes() throws IllegalStateException
availableBytes
in interface Serial
IllegalStateException
public void addListener(SerialDataListener... 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.SerialDataListener
,
SerialDataEvent
public void removeListener(SerialDataListener... 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.SerialDataListener
,
SerialDataEvent
public boolean isShutdown()
isShutdown
in interface Serial
public void shutdown()
public int getMonitorInterval()
getMonitorInterval
in interface Serial
public void setMonitorInterval(int interval)
setMonitorInterval
in interface Serial
interval
- number of millisecondsCopyright © 2012–2019 Pi4J. All rights reserved.