serial.write
serial.write
功能
向已打开串口写入数据。支持 String、字节数组和 Bytes。
语法
device.open(config).write(data)
参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| data | String/Array/Bytes | 是 | 无 | String 按 UTF-8 写入;Array 元素必须是 0 到 255;Bytes 直接写入原始字节。 |
返回值
| 类型 | 说明 |
|---|---|
| Int | 返回写入字节数。 |
示例
port = device.open({type: 'serial', port: 'COM3'}) result = port.write(bytes('010300000002c40b', 'hex')) // 输出:8 print result
注意事项
- 数组中出现非法字节时会报错。
- 写入 Bytes 时不会进行字符串转换。