# device serial communication ## Function After opening the serial port through `device.open({type: 'serial', ...})`, you can call `read()`, `read_bytes()`, `read_text()`, `write(data)`, `flush()` and `close()`. ## Syntax ```bt port = device.open({ type: 'serial', port: 'COM3', baud_rate: 9600, data_bits: 8, stop_bits: 1, parity: 'none', timeout: 1000 }) port.write(bytes('010300000002c40b', 'hex')) data = port.read_bytes() port.flush() port.close() ``` ## Parameters | Field | Type | Required | Default value | Description | |------|------|------|------|------| | type | String | No | serial | Device type, currently only serial is supported. | | port | String | Yes | None | Serial port name. | | baud_rate | Int | No | 9600 | Baud rate. | | data_bits | Int | No | 8 | Data bits. | | stop_bits | Int | No | 1 | Stop bits. | | parity | String | No | none | Parity digit. | | timeout | Int | No | 1000 | Read and write timeout in milliseconds. | ## Return value | Type | Description | |------|------| | Device | Returns the serial port device handle. | ## Example ```bt port = device.open({type: 'serial', port: 'COM3'}) result = type(port) // Output: Device print result ``` ## Notes -Configuration fields use snake_case uniformly. - read() returns a legal UTF-8 string; non-UTF-8 data returns a byte array. - read_bytes() always returns Bytes, suitable for binary protocols such as Modbus RTU. - read_text() strictly converts strings according to UTF-8, and returns null if UTF-8 is illegal. - write(data) can write a string, byte array, or Bytes.