# serial.write

## 功能

向已打开串口写入数据。

## 语法

```bt
device.open(config).write(data)
```

## 参数

| 参数 | 类型 | 必填 | 默认值 | 说明 |
| ------ | ------ | ------ | ------ | ------ |
| data | String/Array | 是 | 无 | 字符串会按 UTF-8 写入；数组元素必须是 0 到 255 的字节。 |

## 返回值

| 类型 | 说明 |
| ------ | ------ |
| Int | 返回写入字节数。 |

## 示例

```bt
port = device.open({type: 'serial', port: 'COM3'})
result = port.write('BT')

// 输出：2
print result
```

## 注意事项

- 数组中出现非法字节时会报错。
