0

Every time I plug in an Arduino Uno on my computer, I need to change the code (Python, pyfirmata library) to connect to a different "COM{n}" port. I would like to be able to have a piece of code that will always be able to connect to an Arduino when it is connected via USB. The piece of code that I use to establish connection to the Arduino is as follows:

self.board = pyfirmata.Arduino("COM4")

The parameter of the function is what always changes if I change Arduino. It could change to "COM7" for example, randomly. Is there a solution to this, so I can always connect to the Arduino, and not have to change the COM port all the time manually?

Daniel Holler
  • 235
  • 1
  • 5
  • 14
  • 2
    Does this answer your question? [Python to automatically select serial ports (for Arduino)](https://stackoverflow.com/questions/24214643/python-to-automatically-select-serial-ports-for-arduino) – gre_gor Jun 09 '20 at 08:49
  • 1
    Assigning USB Serial ports to `COM` names is done by the operating system. Rather check there, why the previously assigned name is still blocked (?) when replugging the same Arduino? – datafiddler Jun 09 '20 at 11:05
  • Did my answer help you? Accept it – Viewed Jun 10 '20 at 08:16

1 Answers1

1

Module pyfirmata has method get_the_board() in util.py (link)

Helper function to get the one and only board connected to the computer running this. It assumes a normal arduino layout, but this can be overriden by passing a different layout dict as the layout parameter. base_dir and identifier are overridable as well. It will raise an IOError if it can't find a board, on a serial, or if it finds more than one.

In addition, see post with implemented methods by other users.

Viewed
  • 1,159
  • 3
  • 16
  • 43