Automatically get the port where Arduino is stuck in Python

When sending a string from Python to Arduino serially, I think that I will write it like this using pyserial.

import serial

ser = serial.Serial()
ser.baudrate = 9600
ser.port = 2 # COM3->2,COM5->4
ser.open()
ser.write("Hello")

Since ser.port changes every time you plug in and unplug the USB, check the port with the Arduino IDE, ser.port =" /dev/tty.usbmodem1451 " for Mac, and COM3 for Windows It is written as ser.port = 2, but it is troublesome because it changes every time it is inserted or removed, so such a writing method is also prepared.

import serial
import serial.tools.list_ports

ser = serial.Serial()
ser.baudrate = 9600
ser.port = list(serial.tools.list_ports.comports())[0][0]
ser.open()
ser.write("Hello")

This writing method seems to fail depending on the environment, and if you are on a Mac, you can find it by searching under / dev properly, so when I tried this writing method, it worked fine.

ser = serial.Serial()
ser.baudrate = 9600
for file in os.listdir('/dev'):
    if "tty.usbmodem" in file:
        ser.port = '/dev/'+file
        ser.open()

In the case of Windows, it is specified by a number, so you can write it like this.

ser = serial.Serial()
ser.baudrate = 9600
for i in range(100):
  try:
    ser.port = i
    ser.open()
    break
  except:
    i = i

Recommended Posts

Automatically get the port where Arduino is stuck in Python
Get the desktop path in Python
Get the script path in Python
Get the desktop path in Python
Get the class name where the method is defined in the decorator
Get the host name in Python
How is the progress? Let's get on with the boom ?? in Python
Where is the python instantiation process written?
What is "mahjong" in the Python library? ??
Get the EDINET code list in Python
[Python] Get the files in a folder with Python
Get the caller of a function in Python
What is wheezy in the Docker Python image?
About the difference between "==" and "is" in python
Get the X Window System window title in Python
How to get the files in the [Python] folder
Get the region where AWS Lambda is running
Get date in Python
Check if the string is a number in python
How to get the variable name itself in python
[Python] Get the numbers in the graph image with OCR
[python] Get the list of classes defined in the module
Get the result in dict format with Python psycopg2
Get the size (number of elements) of UnionFind in Python
Get the value selected in Selenium Python VBA pull-down
Get the URL of the HTTP redirect destination in Python
Using the LibreOffice app in Python (1) Where are the macros?
dict now keeps the storage order in python3.7, but the pprint output is sorted automatically
Get last month in python
Get your heart rate from the fitbit API in Python!
Download the file in Python
Find the difference in Python
Get the MIME type in Python and determine the file format
Get the number of specific elements in a python list
Get Terminal size in Python
[Python] Get the previous month
Explicitly get EOF in python
Get the current date and time in Python, considering the time difference
Solution if the module is installed in Python but you get an error in Jupyter notebook
Get Evernote notes in Python
Get the index of each element of the confusion matrix in Python
Get Japanese synonyms in Python
Get the Ticker Symbol for US exchange listed stocks in Python
Play a sound in Python assuming that the keyboard is a piano keyboard
If you get stuck in Cannot load mkl_intel_thread.dll in Python on Windows
Python: Update pyenv without thinking and solve the "where is Python?" Phenomenon
Automatically resize screenshots for the App Store for each screen in Python
python> Display 3 decimals in ".3f, .3f, .3f" format / Get 3 coordinate values in the range [-1: 1]
Delete a particular character in Python if it is the last
How to judge that the cross key is input in Python3
How to use the asterisk (*) in Python. Maybe this is all? ..
Hit the New Relic API in Python to get the server status
[Python] The value written in Openpyxl is displayed in exponential notation (E).
[python] Get the rank of the values in List in ascending / descending order
Get the formula in an excel file as a string in Python
What to do when the value type is ambiguous in Python?
Get the title and delivery date of Yahoo! News in Python
Get the number of readers of a treatise on Mendeley in Python
Get Leap Motion data in Python.
Difference between == and is in python
Python in the browser: Brython's recommendation