Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public class SerialLink

  1. Object
  2. ByteLink
  3. SerialLink

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.

Constructors

public SerialLink(String device, int baud)8 data bits, no parity, one stop bit.
public SerialLink(String device, int baud, int dataBits, int parity, int stopBits)

Methods

public void open() throws IOExceptionConnects.
public boolean isOpen()Whether open() succeeded and close() has not been called.
public void write(byte[] data, int offset, int length) throws IOExceptionWrites bytes, returning once they are handed to the device.
public int read(byte[] buffer, int offset, int length, int timeout) throws IOExceptionReads what has arrived, waiting up to timeout milliseconds for the first byte.
public void close()Disconnects.

Inherited methods

Constructor details

SerialLink

public SerialLink(String device, int baud)
8 data bits, no parity, one stop bit.

Parameters

device String
the port’s device path or name
baud int
the speed

SerialLink

public SerialLink(String device, int baud, int dataBits, int parity, int stopBits)

Parameters

device String
the port’s device path or name
baud int
the speed
dataBits int
5 to 8
parity int
one of the SerialPort.PARITY_ constants
stopBits int
1 or 2

Method details

open

public void open() throws IOException
Connects. Does nothing when already open.

Throws

IOException
when the device cannot be reached

isOpen

public boolean isOpen()
Whether open() succeeded and close() has not been called.

Returns

true while open

write

public void write(byte[] data, int offset, int length) throws IOException
Writes bytes, returning once they are handed to the device.

Parameters

data byte[]
the buffer
offset int
where to start in it
length int
how many bytes

Throws

IOException
when the link is closed or broken

read

public int read(byte[] buffer, int offset, int length, int timeout) throws IOException
Reads what has arrived, waiting up to timeout milliseconds for the first byte.

Parameters

buffer byte[]
where to put the bytes
offset int
where to start in it
length int
at most how many bytes
timeout int
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

close

public void close()
Disconnects. Safe to call more than once, and from any thread; a read blocked in another thread fails soon after.