freedomotic

Serial helper

This service is based on JSSC library.

First of all you must create a new SerialHelper and set your port parameters in the following order:

  • port name (/dev/ttyUSBx or /dev/ttyACMx for Linux; COMx for Windows)

  • baud rate

  • data bits

  • parity bit

  • stop bits

Also you can override onDataAvailable(String data) method to define how to manage read data. For example you can send received data to another method.

SerialHelper usb = new SerialHelper("/dev/ttyUSB0", 19200, 8, 1, 0, new SerialPortListener() {
  @Override
  public void onDataAvailable(String data) {
    System.out.println("DEBUG: received: " + data);
  }
});

By default data are read continuously. If you want to read a chunk of data with a specific terminator char you can set it as

usb.setReadTerminator("\n");

or if you want to read a chunk with a fixed dimension you can set it as

usb.setChunkSize(5);

Sending a string to the serial port is very simple

usb.write("ABCD");

Here the complete example

SerialHelper usb = new SerialHelper("/dev/ttyUSB0", 19200, 8, 1, 0, new SerialPortListener() {
  @Override
  public void onDataAvailable(String data) {
    System.out.println("DEBUG: received: " + data);
  }
});

usb.setReadTerminator("\n"); //receive until this terminator char
//ALTERNATIVE: usb.setChunkSize(5); //receive messages of 5 chars each
usb.write("ABCD");

Complete examples

Arduino USB plugin