public class Serial extends Object
WiringPi includes a simplified serial port handling library. It can use the on-board serial port, or any USB serial device with no special distinctions between them. You just specify the device name in the initial open function.
Note: The file descriptor (fd) returned is a standard Linux filehandle. You can use the standard read(), write(), etc. system calls on this filehandle as required. E.g. you may wish to write a larger block of binary data where the serialPutchar() or serialPuts() function may not be the most appropriate function to use, in which case, you can use write() to send the data.
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/)
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_COM_PORT
The default hardware COM port provided via the Raspberry Pi GPIO header.
|
Modifier and Type | Method and Description |
---|---|
static void |
serialClose(int fd)
void serialClose (int fd);
|
static int |
serialDataAvail(int fd)
int serialDataAvail (int fd);
|
static void |
serialFlush(int fd)
void serialFlush (int fd);
|
static int |
serialGetchar(int fd)
int serialGetchar (int fd);
|
static int |
serialOpen(String device,
int baud)
int serialOpen (char *device, int baud);
|
static void |
serialPutchar(int fd,
char data)
void serialPutchar (int fd, unsigned char c);
|
static void |
serialPuts(int fd,
String data)
void serialPuts (int fd, char *s);
|
static void |
serialPuts(int fd,
String data,
String... args)
void serialPuts (int fd, String data, String...arguments);
|
public static final String DEFAULT_COM_PORT
serialOpen(String,int)
,
Constant Field Valuespublic static int serialOpen(String device, int baud)
int serialOpen (char *device, int baud);
This opens and initializes the serial device and sets the baud rate. It sets the port into raw mode (character at a time and no translations), and sets the read timeout to 10 seconds. The return value is the file descriptor or -1 for any error, in which case errno will be set as appropriate.
(ATTENTION: the 'device' argument can only be a maximum of 128 characters.)
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.DEFAULT_COM_PORT
,
http://wiringpi.com/reference/serial-library/public static void serialClose(int fd)
void serialClose (int fd);
Closes the device identified by the file descriptor given.
fd
- The file descriptor of the serial port.
public static void serialFlush(int fd)
This discards all data received, or waiting to be send down the given device.
fd
- The file descriptor of the serial port.public static void serialPutchar(int fd, char data)
void serialPutchar (int fd, unsigned char c);
Sends the single byte to the serial device identified by the given file descriptor.
fd
- The file descriptor of the serial port.data
- The character to transmit to the serial port.public static void serialPuts(int fd, String data)
void serialPuts (int fd, char *s);
Sends the nul-terminated string to the serial device identified by the given file descriptor.
(ATTENTION: the 'data' argument can only be a maximum of 1024 characters.)
fd
- The file descriptor of the serial port.data
- The data string to transmit to the serial port.public static void serialPuts(int fd, String data, String... args)
void serialPuts (int fd, String data, String...arguments);
Sends the nul-terminated formatted string to the serial device identified by the given file descriptor.
(ATTENTION: the 'data' argument can only be a maximum of 1024 characters.)
fd
- The file descriptor of the serial port.data
- The formatted data string to transmit to the serial port.args
- Arguments to the format string.public static int serialDataAvail(int fd)
int serialDataAvail (int fd);
Returns the number of characters available for reading, or -1 for any error condition, in which case errno will be set appropriately.fd
- The file descriptor of the serial port.public static int serialGetchar(int fd)
int serialGetchar (int fd);
Returns the next character available on the serial device. This call will block for up to 10 seconds if no data is available (when it will return -1)
fd
- The file descriptor of the serial port.Copyright © 2012–2019 Pi4J. All rights reserved.