device serial communication

device serial communication

device serial communication

Function

After opening the serial port through device.open({type: 'serial', ...}), you can call read(), read_bytes(), read_text(), write(data), flush() and close().

Syntax

Parameters

FieldTypeRequiredDefault valueDescription
typeStringNoserialDevice type, currently only serial is supported.
portStringYesNoneSerial port name.
baud_rateIntNo9600Baud rate.
data_bitsIntNo8Data bits.
stop_bitsIntNo1Stop bits.
parityStringNononeParity digit.
timeoutIntNo1000Read and write timeout in milliseconds.

Return value

TypeDescription
DeviceReturns the serial port device handle.

Example

Notes

  • Configuration fields use snake_case uniformly.
  • read() returns a legal UTF-8 string; non-UTF-8 data returns a byte array.
  • read_bytes() always returns Bytes, suitable for binary protocols such as Modbus RTU.
  • read_text() strictly converts strings according to UTF-8, and returns null if UTF-8 is illegal.
  • write(data) can write a string, byte array, or Bytes.