# 脚本 UART 通信

此示例使用 [Script](https://docs.luxonis.com/software/depthai-components/nodes/script.md) 节点进行 [UART
通信](https://en.wikipedia.org/wiki/Universal_asynchronous_receiver-transmitter)。请注意，OAK 相机没有易于使用的 UART 引脚，我们在
[OAK-FFC-4P](https://shop71313603.taobao.com/?spm=pc_detail.30350276.shop_block.dshopinfo.27a17dd635FNDA) 上焊接了导线以暴露 UART 引脚。

> 此操作仅应在 OAK-FFC-4P 上运行，因为其他 OAK 相机可能具有不同的 GPIO 配置。

## Demo

## 设置

请运行[安装脚本](https://github.com/luxonis/depthai-python/blob/main/examples/install_requirements.py)以下载所有必需的依赖项。请注意，此脚本必须在 git
上下文中运行，因此您需要先下载 [depthai-python](https://github.com/luxonis/depthai-python) 仓库，然后运行该脚本。

```bash
git clone https://github.com/luxonis/depthai-python.git
cd depthai-python/examples
python3 install_requirements.py
```

更多信息，请参考[安装指南](https://docs.luxonis.com/software/depthai/manual-install.md)。

## 源代码

#### Python

```python
#!/usr/bin/env python3
'''
NOTE: This should only be run on OAK-FFC-4P, as other OAK cameras might have different GPIO configuration!
'''
import depthai as dai
import time

# Start defining a pipeline
pipeline = dai.Pipeline()

script = pipeline.create(dai.node.Script)
script.setScript("""
    import serial
    import time

    ser = serial.Serial("/dev/ttyS0", baudrate=115200)
    i = 0
    while True:
        i += 1
        time.sleep(0.1)
        serString = f'TEST_{i}'
        ser.write(serString.encode())
""")
# Define script for output
script.setProcessor(dai.ProcessorType.LEON_CSS)

config = dai.Device.Config()
# Get argument first
GPIO = dai.BoardConfig.GPIO
config.board.gpio[15] = GPIO(GPIO.OUTPUT, GPIO.ALT_MODE_2)
config.board.gpio[16] = GPIO(GPIO.INPUT, GPIO.ALT_MODE_2)
config.board.uart[0] = dai.BoardConfig.UART()

with dai.Device(config) as device:
    device.startPipeline(pipeline)
    print("Pipeline started")
    while True:
        time.sleep(1)
```

#### C++

WIP

## Pipeline

### examples/script_uart.pipeline.json

```json
{
  "pipeline": {
    "connections": [],
    "globalProperties": {
      "calibData": null,
      "cameraTuningBlobSize": null,
      "cameraTuningBlobUri": "",
      "leonCssFrequencyHz": 700000000.0,
      "leonMssFrequencyHz": 700000000.0,
      "pipelineName": null,
      "pipelineVersion": null,
      "sippBufferSize": 18432,
      "sippDmaBufferSize": 16384,
      "xlinkChunkSize": -1
    },
    "nodes": [
      [
        0,
        {
          "id": 0,
          "ioInfo": [],
          "name": "Script",
          "properties": {
            "processor": 0,
            "scriptName": "<script>",
            "scriptUri": "asset:__script"
          }
        }
      ]
    ]
  }
}
```

### 需要帮助？

请前往 [OAKChina 官网](https://www.oakchina.cn/) 获取技术支持或解答您的任何疑问。
