Class SerialLink

java.lang.Object
com.codename1.io.link.ByteLink
com.codename1.io.link.SerialLink

public class SerialLink extends ByteLink

A serial port: an RS-232 cable, or a USB serial adapter the operating system presents as one (COM3, /dev/ttyUSB0, /dev/ttyACM0).

Needs Hardware.FEATURE_SERIAL: the Windows and Linux desktop ports and the simulator. On Android, reach a USB serial device through UsbLink instead.

  • Constructor Details

    • SerialLink

      public SerialLink(String device, int baud)

      8 data bits, no parity, one stop bit.

      Parameters
      • device: the port's device path or name

      • baud: the speed

    • SerialLink

      public SerialLink(String device, int baud, int dataBits, int parity, int stopBits)
      Parameters
      • device: the port's device path or name

      • baud: the speed

      • dataBits: 5 to 8

      • parity: one of the SerialPort.PARITY_ constants

      • stopBits: 1 or 2

  • Method Details

    • open

      public void open() throws IOException
      Description copied from class: ByteLink

      Connects. Does nothing when already open.

      Throws
      • IOException: when the device cannot be reached
      Specified by:
      open in class ByteLink
      Throws:
      IOException
    • isOpen

      public boolean isOpen()
      Description copied from class: ByteLink

      Whether ByteLink.open() succeeded and ByteLink.close() has not been called.

      Returns

      true while open

      Specified by:
      isOpen in class ByteLink
    • write

      public void write(byte[] data, int offset, int length) throws IOException
      Description copied from class: ByteLink

      Writes bytes, returning once they are handed to the device.

      Parameters
      • data: the buffer

      • offset: where to start in it

      • length: how many bytes

      Throws
      • IOException: when the link is closed or broken
      Specified by:
      write in class ByteLink
      Throws:
      IOException
    • read

      public int read(byte[] buffer, int offset, int length, int timeout) throws IOException
      Description copied from class: ByteLink

      Reads what has arrived, waiting up to timeout milliseconds for the first byte.

      Parameters
      • buffer: where to put the bytes

      • offset: where to start in it

      • length: at most how many bytes

      • timeout: how long to wait, in milliseconds

      Returns

      the number of bytes read; 0 when nothing arrived in time

      Throws
      • IOException: when the link is closed or broken, including when the other side closed it
      Specified by:
      read in class ByteLink
      Throws:
      IOException
    • close

      public void close()
      Description copied from class: ByteLink
      Disconnects. Safe to call more than once, and from any thread; a read blocked in another thread fails soon after.
      Specified by:
      close in class ByteLink