Serial Port Development
Introduction:
This guide provides a demonstration on how to develop an application that utilizes serial ports, using both Java and C APIs. Included are sources for a serial port test tool and downloadable demo applications.
Demo and Tool Downloads:
Serial Port Test Tool:
Download the Source APP and APK for a serial port testing tool.
Best Practices for Reading Data from a Serial Port:
Known Data Length:
If the length of the data (
len
) is known, set the receive buffer to this length (len
).Set the timeout to forever. The SDK will return once it has received the specified length of data.
Unknown Data Length:
Set the receive buffer to 1 byte and the timeout to forever.
When data is received, continue reading in a loop.
Set the buffer to 256 bytes (or another length) and the timeout to 200ms to receive all data.
Good Practice - Define a Data Packet Protocol:
Define a packet header (e.g., 3 bytes) that includes the data length.
Set the receive buffer to 3 bytes and the timeout to forever.
After receiving the header, set the buffer to the parsed data length (
len
) and the timeout to forever to receive all data.
API Usage:
Java API for Serial Port:
When opening a serial port using the Java API, set the logic ID of the serial port as a parameter.
SerialPortDevice.ID_USB_SLAVE_SERIAL : works for USB serial port in slave mode.
SerialPortDevice.ID_USB_HOST_SERIAL : works for USB serial port in master mode.
SerialPortDevice.ID_SERIAL_EXT : works for internal fiscal/ other serial port module.
SerialPortDevice.ID_USB_CDC: works for USB Communication Device Class.
SerialPortDevice.ID_USB_GPRINTER: works for USB GPRINTER.
SerialPortDevice.ID_USB_SLAVE_SERIAL_ACM: works for ACM without Qualcomm driver.
C API for Serial Port:
When using the C API to open a serial port, the device name of the serial port must be specified as a parameter.
Device Name:
Cable Connection Guidelines:
For Model Q4:
Refer to Picture 2 and Picture 3 for connecting using a USB to DB9 cable.
Alternatively, refer to Picture 4 for connection using a UU cable.
Please ensure all connections and API parameters are correctly set for successful communication with the serial port.
Cable pictures:
Last updated