Class Serial


  • 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:

    • 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:
    https://pi4j.com/, http://wiringpi.com/reference/serial-library/
    • Method Detail

      • serialOpen

        public 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.)

        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.
        Returns:
        The return value is the file descriptor or -1 for any error, in which case errno will be set as appropriate.
        See Also:
        DEFAULT_COM_PORT, http://wiringpi.com/reference/serial-library/
      • serialClose

        public static void serialClose​(int fd)

        void serialClose (int fd);

        Closes the device identified by the file descriptor given.

        Parameters:
        fd -

        The file descriptor of the serial port.

        See Also:
        http://wiringpi.com/reference/serial-library/
      • serialFlush

        public static void serialFlush​(int fd)

        void serialFlush (int fd);

        This discards all data received, or waiting to be send down the given device.

        Parameters:
        fd - The file descriptor of the serial port.
        See Also:
        http://wiringpi.com/reference/serial-library/
      • serialPutByte

        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.

        Parameters:
        fd - The file descriptor of the serial port.
        data - The byte to transmit to the serial port.
        See Also:
        http://wiringpi.com/reference/serial-library/
      • serialPutchar

        @Deprecated
        public static void serialPutchar​(int fd,
                                         char data)
        Deprecated.
        Use the serialPutByte() method instead.

        void serialPutchar (int fd, char c);

        Sends a single character () to the serial device identified by the given file descriptor.

        Parameters:
        fd - The file descriptor of the serial port.
        data - The byte to transmit to the serial port.
        See Also:
        http://wiringpi.com/reference/serial-library/
      • serialPutBytes

        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.

        Parameters:
        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.
        See Also:
        http://wiringpi.com/reference/serial-library/
      • serialPutBytes

        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.

        Parameters:
        fd - The file descriptor of the serial port.
        data - The byte array to transmit to the serial port.
        See Also:
        http://wiringpi.com/reference/serial-library/
      • serialPuts

        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.)

        Parameters:
        fd - The file descriptor of the serial port.
        data - The data string to transmit to the serial port.
        See Also:
        http://wiringpi.com/reference/serial-library/
      • serialPuts

        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.)

        Parameters:
        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.
        See Also:
        http://wiringpi.com/reference/serial-library/
      • serialDataAvail

        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.
        Parameters:
        fd - The file descriptor of the serial port.
        Returns:
        Returns the number of characters available for reading, or -1 for any error condition, in which case errno will be set appropriately.
        See Also:
        http://wiringpi.com/reference/serial-library/
      • serialGetByte

        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)

        Parameters:
        fd - The file descriptor of the serial port.
        Returns:
        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 NULL)
        See Also:
        http://wiringpi.com/reference/serial-library/
      • serialGetBytes

        public static byte[] serialGetBytes​(int fd,
                                            int length)

        int serialGetBytes (int fd, int length);

        Returns the length of bytes available on the serial device.

        Parameters:
        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.
        Returns:
        Returns the length of byte available on the serial device.
        See Also:
        http://wiringpi.com/reference/serial-library/
      • serialGetAvailableBytes

        public static byte[] serialGetAvailableBytes​(int fd)

        int serialGetAvailableBytes (int fd);

        Returns on array of bytes currently available on the serial device.

        Parameters:
        fd - The file descriptor of the serial port.
        Returns:
        Returns the current bytes available on the serial device.
        See Also:
        http://wiringpi.com/reference/serial-library/
      • serialGetchar

        @Deprecated
        public static int serialGetchar​(int fd)
        Deprecated.
        Use the serialGetByte() method instead.

        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)

        Parameters:
        fd - The file descriptor of the serial port.
        Returns:
        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)
        See Also:
        http://wiringpi.com/reference/serial-library/