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 byte[] |
serialGetAvailableBytes(int fd)
int serialGetAvailableBytes (int fd);
|
static byte |
serialGetByte(int fd)
byte serialGetByte (int fd);
|
static byte[] |
serialGetBytes(int fd,
int length)
int serialGetBytes (int fd, int length);
|
static int |
serialGetchar(int fd)
Deprecated.
Use the serialGetByte() method instead.
|
static int |
serialOpen(String device,
int baud)
int serialOpen (char *device, int baud);
|
static void |
serialPutByte(int fd,
byte data)
void serialPutByte (int fd, unsigned char c);
|
static void |
serialPutBytes(int fd,
byte... data)
void serialPutBytes (int fd, byte[] data);
|
static void |
serialPutBytes(int fd,
byte[] data,
int length)
void serialPutBytes (int fd, byte[] data);
|
static void |
serialPutchar(int fd,
char data)
Deprecated.
Use the serialPutByte() method instead.
|
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 serialPutByte(int fd, byte data)
void serialPutByte (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 byte to transmit to the serial port.@Deprecated public static void serialPutchar(int fd, char data)
void serialPutchar (int fd, char c);
Sends a single character () to the serial device identified by the given file descriptor.
fd
- The file descriptor of the serial port.data
- The byte to transmit to the serial port.public static void serialPutBytes(int fd, byte[] data, int length)
void serialPutBytes (int fd, byte[] data);
Sends any array of bytes to the serial device identified by the given file descriptor.
fd
- The file descriptor of the serial port.data
- The byte array to transmit to the serial port.length
- The number of bytes from the byte array to transmit to the serial port.public static void serialPutBytes(int fd, byte... data)
void serialPutBytes (int fd, byte[] data);
Sends any array of bytes to the serial device identified by the given file descriptor.
fd
- The file descriptor of the serial port.data
- The byte array 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 byte serialGetByte(int fd)
byte serialGetByte (int fd);
Returns the next byte 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.public static byte[] serialGetBytes(int fd, int length)
int serialGetBytes (int fd, int length);
Returns the length of bytes available on the serial device.
fd
- The file descriptor of the serial port.length
- The number of bytes to get from the serial port. This number must not be higher than the number of available bytes.public static byte[] serialGetAvailableBytes(int fd)
int serialGetAvailableBytes (int fd);
Returns on array of bytes currently available on the serial device.
fd
- The file descriptor of the serial port.@Deprecated public static int serialGetchar(int fd)
int serialGetchar (int fd);
Returns the next byte 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.