device.open
device.open
Function
Open the device connection. The current implementation supports serial serial devices.
Syntax
device.open(config)
Parameters
| Parameters | Type | Required | Default value | Description |
|---|---|---|---|---|
| config | Object | Yes | None | The device configuration object. |
Serial port configuration field
Currently device.open(config) only supports opening serial serial devices. config fields are as follows:
| Field | Type | Required | Default value | Description |
|---|---|---|---|---|
| type | String | No | serial | Device type. Currently only serial is supported, passing in other values will throw an unsupported device type error. |
| port | String | Yes | None | System serial port name, such as COM3, /dev/ttyUSB0. Usually comes from the device.scan() return object's port fields. |
| baud_rate | Int | No | 9600 | Baud rate, indicating the number of symbols transmitted per second. Must be consistent with the external device configuration. |
| data_bits | Int | No | 8 | Number of data bits. Supports 5, 6, 7, 8. |
| stop_bits | Int | No | 1 | Number of stop bits. Supports 1, 2. |
| parity | String | No | none | Parity digit. Supports none, odd, even; empty strings are processed as none. |
| timeout | Int | No | 1000 | Read and write timeout, in milliseconds. |
Return value
| Type | Description |
|---|---|
| Device | Returns the opened device handle. |
Example
// Replace port with the actual serial port name returned by 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) // Output: Device print result
Notes
- Configuration fields use snake_case uniformly.
-
baud_rate,data_bits,stop_bits,paritymust be consistent with the serial port parameters of the hardware device, otherwise normal communication may not be possible.