如何在 macOS 中为 pySerial 确定 Arduino 的端口号

如果我将在 Python 中使用它,我想知道如何确定连接到我的 macbook 的 Arduino nano 的端口号。


Arduino IDE 中的工具>端口


/dev/cu.usbserial-A900afrI

我的 .py 文件中有此代码


import serial


a = serial.Serial('A900afrI', baudrate=9600, timeout=1)

我想知道我应该用哪个端口替换“A9000afrI”,因为我收到一个错误:


Traceback (most recent call last):

  File "/Users/cievlh/Desktop/Python/python_env/lib/python3.7/site-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: 'A900afrI'


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "pyserial.py", line 3, in <module>

    a = serial.Serial('A900afrI', baudrate=9600, timeout=1)

  File "/Users/cievlh/Desktop/Python/python_env/lib/python3.7/site-packages/serial/serialutil.py", line 240, in __init__

    self.open()

  File "/Users/cievlh/Desktop/Python/python_env/lib/python3.7/site-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 A900afrI: [Errno 2] No such file or directory: 'A900afrI'

(python_env)


米脂
浏览 156回答 2
2回答

郎朗坤

明白了,只是根据 gre_gor 包含了整个字符串。import seriala = serial.Serial('/dev/cu.usbserial-A900afrI', baudrate=9600, timeout=1)

不负相思意

代码实际上是针对 Arduino 克隆的:我认为它们是相同的。import serial.tools.list_ports#Find USB Portdef find_port():&nbsp; #Finds which port the arduino is plugged into&nbsp; &nbsp; ports = list(serial.tools.list_ports.comports())&nbsp; &nbsp; for p in ports:&nbsp; &nbsp; &nbsp; &nbsp; if '0403' in p[2]: #unique to Osepp Uno (arduino clone)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return p[0]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python