以下代码中的串行连接有什么问题(在Raspberry Pi 4,Raspbian OS上运行)?



RPi上的串行连接不应该如此具有挑战性,尽管现在我面临着一些非常奇怪的

这是代码:

.
.
import serial
.
.
ser=serial.Serial(
port='/dev/ttyUSB0',
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
.
.

错误为:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 265, in open
self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
FileNotFoundError: [Errno 2] No such file or directory: '/dev/ttyUSB0'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/pi/Desktop/rtu-v4/dnp3/rtu.py", line 27, in <module>
timeout=1
File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 240, in __init__
self.open()
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 268, in open
raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
serial.serialutil.SerialException: [Errno 2] could not open port /dev/ttyUSB0: [Errno 2] No such file or directory: '/dev/ttyUSB0'

我已经签出:

https://www.raspberrypi.org/forums/viewtopic.php?t=224369

https://github.com/brendan-w/python-OBD/issues/137

并在终端中运行以下命令:sudo pip3 uninstall serialsudo pip3 install pyserial

设备是树莓派4

如有任何帮助,我们将不胜感激。Thnx。

消息"No such file or directory:'/dev/ttyUSB0'"表示您没有连接到USB0的设备。

您可以使用以下命令:

lsusb

找出哪些端口可用,然后更改您的USB名称代码以连接正确的设备。

由于您正在尝试(我认为(使用USB到串行适配器进行通信,因此可以使用dmesg获取连接到ttyUSB0的设备。在终端中键入以下命令:

dmesg|grep ttyUSB*

它将打印出连接到所有ttyUSB的所有设备。在我的情况下,结果是:

[ 5072.316991] usb 3-1: ch341-uart converter now attached to ttyUSB0

如果连接了更多的设备,结果会列出ttyUSB1/2..等

如果您断开设备,在终端中运行相同的命令将显示断开连接消息:

dmesg|grep ttyUSB*

[ 5361.353142] ch341-uart ttyUSB0: ch341-uart converter now disconnected from ttyUSB0

最新更新