Shortcuts

Received and send

Set data format

Use the methods setReadDataType(type) and setSendDataType(type) to format the technique data:

Set the received message format

//SerialPort.READ_HEX hex
//SerialPort.READ_STRING string
//If not set, the default string form
serialPort.setReadDataType(SerialPort.READ_HEX);

In addition to this, you can also set the received data format when building the instance:

//SerialPort.READ_HEX hex
//SerialPort.READ_STRING string
//If not set, the default string form
SerialPort serialPort = SerialPortBuilder.INSTANCE
                .setReadDataType(SerialPort.READ_HEX)
                .build(this);

Set the send data format

//SerialPort.SEND_HEX hex
//SerialPort.SEND_STRING string
//If not set, the default string form
serialPort.setSendDataType(SerialPort.SEND_HEX);

In addition to this, you can also set the send data format when building the instance:

//SerialPort.SEND_HEX hex
//SerialPort.SEND_STRING string
//If not set, the default string form
SerialPort serialPort = SerialPortBuilder.INSTANCE
                .setSendDataType(SerialPort.SEND_HEX)
                .build(this);

Currently, the data sending and receiving for BLE devices does not support the setting format, only the string format is supported. If you really need the hexadecimal data format, you can temporarily implement it by referring to the processing method of traditional equipment.

Reference code link: HexStringToStringStringToHex

Receive message

String and hex

Use the method setReceivedDataCallback(receivedDataCallback) to set up a received message listener:

serialPort.setReceivedDataCallback( (data) -> {
            
            return null;
        });

In addition to this, you can also configure the listener when building the instance:

SerialPort serialPort = SerialPortBuilder.INSTANCE
                .setReceivedDataCallback( (data) -> {

                    return null;
                })
                .build(this);

Byte array

When receiving a message, you can also choose to obtain a byte array as follows:

serialPort.setReceivedBytesCallback( (bytes) -> {
            
            return null;
        });

In addition to this, you can also configure the listener when building the instance:

SerialPort serialPort = SerialPortBuilder.INSTANCE
                .setReceivedBytesCallback( (bytes) -> {

                    return null;
                })
                .build(this);

Send a message

Send a message using the method sendData(data):

String

serialPort.sendData("Hello World");

hex

serialPort.sendData("0C FF");

All hexadecimals should be two digits, with 0 in front of the less than two digits, case-insensitive.

BLE device send bytes

Now, BLE device support send bytes

serialPort.sendData(bytes);
Read the Docs v: latest
Versions
latest
stable
4.1.9
Downloads
pdf
html
epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.