# device.open ## 功能 打开设备连接。当前实现支持 serial 串口设备。 ## 语法 ```bt device.open(config) ``` ## 参数 | 参数 | 类型 | 必填 | 默认值 | 说明 | | ------ | ------ | ------ | ------ | ------ | | config | Object | 是 | 无 | 设备配置对象。 | ## 串口配置字段 当前 `device.open(config)` 仅支持打开 serial 串口设备。`config` 字段如下: | 字段 | 类型 | 必填 | 默认值 | 说明 | | ------ | ------ | ------ | ------ | ------ | | type | String | 否 | `serial` | 设备类型。当前只支持 `serial`,传入其它值会抛出 unsupported device type 错误。 | | port | String | 是 | 无 | 系统串口名称,例如 `COM3`、`/dev/ttyUSB0`。通常来自 `device.scan()` 返回对象的 `port` 字段。 | | baud_rate | Int | 否 | `9600` | 波特率,表示每秒传输的符号数量。必须和外部设备配置一致。 | | data_bits | Int | 否 | `8` | 数据位数量。支持 `5`、`6`、`7`、`8`。 | | stop_bits | Int | 否 | `1` | 停止位数量。支持 `1`、`2`。 | | parity | String | 否 | `none` | 校验位。支持 `none`、`odd`、`even`;空字符串按 `none` 处理。 | | timeout | Int | 否 | `1000` | 读写超时时间,单位毫秒。 | ## 返回值 | 类型 | 说明 | | ------ | ------ | | Device | 返回已打开的设备句柄。 | ## 示例 ```bt // port 要替换为 device.scan() 扫描到的实际串口名。 serial = device.open({ type: 'serial', port: 'COM3', baud_rate: 9600, data_bits: 8, stop_bits: 1, parity: 'none', timeout: 1000 }) result = type(serial) // 输出:Device print result ``` ## 注意事项 - 配置字段统一使用 snake_case。 - `baud_rate`、`data_bits`、`stop_bits`、`parity` 必须和硬件设备的串口参数一致,否则可能无法正常通信。