device.open

device.open

device.open

功能

打开设备连接。当前实现支持 serial 串口设备。

语法

参数

参数类型必填默认值说明
configObject设备配置对象。

串口配置字段

当前 device.open(config) 仅支持打开 serial 串口设备。config 字段如下:

字段类型必填默认值说明
typeStringserial设备类型。当前只支持 serial,传入其它值会抛出 unsupported device type 错误。
portString系统串口名称,例如 COM3/dev/ttyUSB0。通常来自 device.scan() 返回对象的 port 字段。
baud_rateInt9600波特率,表示每秒传输的符号数量。必须和外部设备配置一致。
data_bitsInt8数据位数量。支持 5678
stop_bitsInt1停止位数量。支持 12
parityStringnone校验位。支持 noneoddeven;空字符串按 none 处理。
timeoutInt1000读写超时时间,单位毫秒。

返回值

类型说明
Device返回已打开的设备句柄。

示例

注意事项

  • 配置字段统一使用 snake_case。
  • baud_ratedata_bitsstop_bitsparity 必须和硬件设备的串口参数一致,否则可能无法正常通信。