# ROS2 与 Arduino 串口读写挂起怎么办

> 原文链接：https://robotics.hellobike.com/pitfalls/2021-07-23-robotics-se-22438
> 频道：pitfalls | 更新日期：2021-07-23 | 标签：auto, stackexchange, arduino, python, serial, communication
> 核心摘要：问题因回调中反复打开串口触发 Arduino 自动复位；复用串口连接或禁用自动复位可解决。

# ROS2 与 Arduino 串口读写挂起怎么办

::: v-pre
> 本文由 AI 根据公开内容整理，可能有误，请以原文为准：[When reading serial data between Arduino and a node, it hangs. How can I properly send and read data?](https://robotics.stackexchange.com/questions/22438/when-reading-serial-data-between-arduino-and-a-node-it-hangs-how-can-i-properl)（Robotics Stack Exchange #22438）

**状态**：已解决

## 问题

ROS2 节点与 Arduino 串口通信时读写挂起；`executeCallback` 中每次执行 `serial.Serial(port="/dev/ttyUSB0")`，可能导致 Arduino 因建立串口连接自动复位，写入时不在线而读不到响应。

## 环境

Ubuntu 21.04 x64，Linux 5.8.0-59-generic，ROS2 Foxy，Python serial，老款 Arduino Nano（legacy bootloader）。

## 解决办法

1. 不要在每次 `executeCallback` 里重新创建串口连接；在节点初始化时建立 `serial.Serial`，把连接句柄保存为成员变量，或作为回调参数传入复用。
2. 或禁用 Arduino 的串口连接自动复位：在 `5V` 与 `reset` 之间接约 `120 ohm` 上拉电阻，需恢复自动复位时拔掉；不要低于 `110 ohm` 或高于 `124 ohm`，且不要在连接 ISP 编程器时这样做。

原问题作者：rasa911216；采纳答案作者：Chuck。原内容依 CC BY-SA 4.0 授权，本页摘要同样依 CC BY-SA 4.0 授权。
:::

## 原始参考来源 (Sources)

- [Robotics Stack Exchange #22438：When reading serial data between Arduino and a node, it hangs. How can I properly send and read data?](https://robotics.stackexchange.com/questions/22438/when-reading-serial-data-between-arduino-and-a-node-it-hangs-how-can-i-properl)