Search code examples
pythonarduinopyserialmacos-mojaveusbserial

How to determine port number of Arduino for pySerial in macOS


I would like to know how to determine the port number of the Arduino nano that is connected to my macbook if I will use it in Python.

Tools>Port in Arduino IDE

/dev/cu.usbserial-A900afrI

I have this code in my .py file

import serial

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

I want to know what port should I replace 'A9000afrI' with because I get an error which is this:

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)

Solution

  • Got it, just included the whole string according to gre_gor.

    import serial
    
    a = serial.Serial('/dev/cu.usbserial-A900afrI', baudrate=9600, timeout=1)