device 串口通信

device 串口通信

device 串口通信

功能

通过 device.open({type: 'serial', ...}) 打开串口后,可以调用 read()read_bytes()read_text()write(data)flush()close()

语法

参数

字段类型必填默认值说明
typeStringserial设备类型,当前仅支持 serial。
portString串口名称。
baud_rateInt9600波特率。
data_bitsInt8数据位。
stop_bitsInt1停止位。
parityStringnone校验位。
timeoutInt1000读写超时毫秒数。

返回值

类型说明
Device返回串口设备句柄。

示例

注意事项

  • 配置字段统一使用 snake_case。
  • read() 返回合法 UTF-8 字符串;非 UTF-8 数据返回字节数组。
  • read_bytes() 始终返回 Bytes,适合 Modbus RTU 等二进制协议。
  • read_text() 严格按 UTF-8 转字符串,非法 UTF-8 返回 null。
  • write(data) 可写入字符串、字节数组或 Bytes。