Python串口

4 投票
1 回答
17998 浏览
提问于 2025-04-16 06:13

我想在Python中和我的串口进行通信。我在Linux上安装了pyserial和uspp:

import serial
ser = serial.Serial('/dev/pts/1', 19200, timeout=1)
print ser.portstr       #check which port was really used
ser.write("hello")      #write a string
ser.close()             #

但是出现了以下错误:

Traceback (most recent call last):
  File "poi.py", line 5, in <module>
    ser.open()   
  File "/usr/local/lib/python2.6/dist-packages/pyserial-2.5-py2.6.egg/serial/serialposix.py", line 276, in open
    raise SerialException("could not open port %s: %s" % (self._port, msg))
serial.serialutil.SerialException: could not open port /dev/tyUSB1: [Errno 2] No such file or directory: '/dev/tyUSB1'

1 个回答

6

如果你在使用Linux系统,通常串口的名称是 /dev/tty*;你只需要把 * 替换成 s 和端口的编号,比如 /dev/ttyS1。我在使用一个通过USB串口与电脑通信的Arduino,它的名称是 /dev/ttyUSB0

撰写回答