Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public class 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 IOException | Connects. |
public boolean isOpen() | Whether open() succeeded and close() has not been called. |
public void write(byte[] data, int offset, int length)
throws IOException | Writes bytes, returning once they are handed to the device. |
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. |
public void close() | Disconnects. |
Inherited methods
Constructor details
SerialLink
public SerialLink(String device, int baud)8 data bits, no parity, one stop bit.
Parameters
deviceString- the port’s device path or name
baudint- the speed
SerialLink
public SerialLink(String device, int baud, int dataBits, int parity, int stopBits)Parameters
deviceString- the port’s device path or name
baudint- the speed
dataBitsint- 5 to 8
parityint- one of the
SerialPort.PARITY_constants stopBitsint- 1 or 2
Method details
open
public void open()
throws IOExceptionConnects. Does nothing when already open.
Throws
IOException- when the device cannot be reached
isOpen
public boolean isOpen()Returns
true while open
write
public void write(byte[] data, int offset, int length)
throws IOExceptionWrites bytes, returning once they are handed to the device.
Parameters
databyte[]- the buffer
offsetint- where to start in it
lengthint- 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 IOExceptionReads what has arrived, waiting up to
timeout milliseconds for the
first byte.Parameters
bufferbyte[]- where to put the bytes
offsetint- where to start in it
lengthint- at most how many bytes
timeoutint- 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.