device.scan
device.scan
功能
扫描系统设备。当前实现支持 serial 串口扫描。
语法
device.scan(type)
参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| type | String | 否 | serial | 设备类型。当前仅支持 serial。 |
返回值
| 类型 | 说明 |
|---|---|
| Array | 返回设备信息对象数组。 |
串口信息字段
当前 device.scan() 返回 serial 串口信息对象数组。数组中每个对象的字段如下:
| 字段 | 类型 | 必定存在 | 说明 |
|---|---|---|---|
| type | String | 是 | 设备类型,当前固定为 serial。 |
| port | String | 是 | 系统串口名称。Windows 常见值如 COM3;Linux/macOS 常见值如 /dev/ttyUSB0、/dev/tty.usbserial。打开串口时传给 device.open({port: ...})。 |
| name | String | 是 | 设备友好名称。USB 串口优先使用产品名或厂商名;无法识别时等于 port。 |
| kind | String | 是 | 串口来源类型。常见值为 usb、bluetooth、pci、unknown。 |
| vid | Int | 否 | USB Vendor ID。只有 USB 串口能识别到厂商 ID 时存在。 |
| pid | Int | 否 | USB Product ID。只有 USB 串口能识别到产品 ID 时存在。 |
| serial_number | String | 是 | USB 设备序列号;系统未提供时为空字符串。 |
| manufacturer | String | 是 | USB 设备厂商名;系统未提供时为空字符串。 |
| product | String | 是 | USB 设备产品名;系统未提供时为空字符串。 |
示例
// 扫描当前系统可见的串口设备。 ports = device.scan('serial') // 读取第一个串口的系统端口名,这个值可用于 device.open({port: ...})。 first = ports[0] result = first.port // 输出示例:COM3 print result
注意事项
- 当前实现仅支持
serial,未传 type 时默认扫描串口。 -
vid和pid不是所有串口都有;字段不存在时读取结果为 Empty。