public interface Serial
This interface provides a set of functions for 'Serial' communication.
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/)
SerialFactory
,
SerialDataEvent
,
SerialDataListener
,
http://www.pi4j.com/Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_COM_PORT
The default hardware COM port provided via the Raspberry Pi GPIO header.
|
static int |
DEFAULT_MONITOR_INTERVAL |
Modifier and Type | Method and Description |
---|---|
void |
addListener(SerialDataListener... listener)
Java consumer code can call this method to register itself as a listener for serial data
events.
|
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)
Java consumer code can call this method to unregister itself as a listener for serial data
events.
|
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.
|
static final String DEFAULT_COM_PORT
open(String, int)
,
Constant Field Valuesstatic final int DEFAULT_MONITOR_INTERVAL
void open(String device, int baudRate) throws SerialPortException
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.DEFAULT_COM_PORT
void close() throws IllegalStateException
IllegalStateException
boolean isOpen()
open(String, int)
boolean isClosed()
open(String, int)
void flush() throws IllegalStateException
IllegalStateException
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.
IllegalStateException
void write(char data) throws IllegalStateException
data
- A single character to be transmitted.IllegalStateException
void write(char[] data) throws IllegalStateException
data
- A character array of data to be transmitted.IllegalStateException
void write(byte data) throws IllegalStateException
data
- A single byte to be transmitted.IllegalStateException
void write(byte[] data) throws IllegalStateException
data
- A byte array of data to be transmitted.IllegalStateException
void write(String data) throws IllegalStateException
data
- A string of data to be transmitted.IllegalStateException
void writeln(String data) throws IllegalStateException
data
- A string of data to be transmitted.IllegalStateException
void write(String data, String... args) throws IllegalStateException
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
void writeln(String data, String... args) throws IllegalStateException
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
int availableBytes() throws IllegalStateException
IllegalStateException
void addListener(SerialDataListener... listener)
Java consumer code can call this method to register itself as a listener for serial data events.
listener
- A class instance that implements the SerialListener interface.SerialDataListener
,
SerialDataEvent
void removeListener(SerialDataListener... listener)
Java consumer code can call this method to unregister itself as a listener for serial data events.
listener
- A class instance that implements the SerialListener interface.SerialDataListener
,
SerialDataEvent
boolean isShutdown()
void shutdown()
int getMonitorInterval()
void setMonitorInterval(int interval)
interval
- number of millisecondsCopyright © 2012–2019 Pi4J. All rights reserved.