Getting python to see arduino programming on the samdD51 grand centeral board? - SOLVED

Hello, I don't know if this belongs in programming or commandline. I'm trying to get python to talk to the arduino to detect which port it is on. I searched online and found this

import serial.tools.list_ports

def find_arduino_port():
    ports = serial.tools.list_ports.comports()
    for port, desc, hwid in ports:
        if "Arduino" in desc:
            return port
    return None

port = find_arduino_port()

if port:
    print("Arduino found on port:", port)
else:
    print("No Arduino found")

I installed all the modules pyserial and serial. It all installed correctly with no errors. When I go to run the script I get (No Arduino found). on the arduino side I did add the serial and serial print for arduino here. No matter what I do I can not get it to see an arduino. I'm not all to familiar with python yet. I need help to detect which port it is on. Someone please help me?

on the arduino side I just have a basic serial script.

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Serial is working.");
}

void loop() {
  // put your main code here, to run repeatedly:

}

Joseph

I ran another script

import serial
import time

# Replace 'COM3' with your Arduino's port
arduino_port = "COM3"
baud_rate = 9600  # Match the baud rate set in the Arduino sketch

try:
    ser = serial.Serial(arduino_port, baud_rate, timeout=1)
    time.sleep(2)  # Wait for Arduino to reset
    print("Connected to Arduino")
    ser.close()
except Exception as e:
    print(f"Error: {e}")

And I now got.

PS C:\Users\josep\OneDrive\Desktop\screeny> python detect.py
Connected to Arduino

I just can't get it to detect. i can get it to connect with no problem.

Are you writing these scripts yourself, or are you just trying to run what ChatGPT gives you?

If you're writing them yourself, you should try looking at the output of comports and see what it says and maybe that will help you figure out why your thing isn't working.

If you're just trying copying and pasting ChatGPT output, you really need to learn how to program first, before you try doing stuff like this. Your current approach is not going to set you up for long term success.

no chatgpt. It is what I just searched from google. I'm not a python coder. It is examples I found in google. I never used chatgpt.

try listing the attached serial devices, e.g.

import serial
from serial.tools import list_ports

if __name__ == "__main__":
    for port in list_ports.comports():
        if "USB" in port.hwid:
            print(f"Name: {port.name}")
            print(f"Description: {port.description}")
            print(f"Location: {port.location}")
            print(f"Product: {port.product}")
            print(f"Manufacturer: {port.manufacturer}")
            print(f"ID: {port.pid}\n")

on a Raspberry PI displays

raspberrypi:~ $ python3 ListCOMports.py
Name: ttyUSB6
Description: CP2102 USB to UART Bridge Controller - CP2102 USB to UART Bridge Controller
Location: 1-1.2.4
Product: CP2102 USB to UART Bridge Controller
Manufacturer: Silicon Labs
ID: 60000

Name: ttyUSB5
Description: CP2102 USB to UART Bridge Controller - CP2102 USB to UART Bridge Controller
Location: 1-1.2.5.7
Product: CP2102 USB to UART Bridge Controller
Manufacturer: Silicon Labs
ID: 60000

Name: ttyUSB3
Description: USB2.0-Ser!
Location: 1-1.2.7
Product: USB2.0-Ser!
Manufacturer: None
ID: 29987

Name: ttyUSB4
Description: USB Serial
Location: 1-1.2.6
Product: USB Serial
Manufacturer: None
ID: 29987

Name: ttyUSB2
Description: USB Serial
Location: 1-1.2.5.6
Product: USB Serial
Manufacturer: None
ID: 29987

Name: ttyUSB1
Description: CP2102 USB to UART Bridge Controller - CP2102 USB to UART Bridge Controller
Location: 1-1.2.5.5
Product: CP2102 USB to UART Bridge Controller
Manufacturer: Silicon Labs
ID: 60000

Name: ttyUSB0
Description: USB Serial
Location: 1-1.4
Product: USB Serial
Manufacturer: None
ID: 29987

Name: ttyACM1
Description: Feather RP2040 CAN - Board CDC
Location: 1-1.2.3:1.0
Product: Feather RP2040 CAN
Manufacturer: Adafruit
ID: 33071

Name: ttyACM0
Description: USB Single Serial
Location: 1-1.5:1.0
Product: USB Single Serial
Manufacturer: None
ID: 21971

Windows should show a similar list displaying COM ports

That does work it did detect the port.

Name: COM3
Description: USB Serial Device (COM3)
Location: 1-1:x.0
Product: None
Manufacturer: Microsoft
ID: 32800

I ran your original Python script while connected to an Arduino Leonardo, and it works properly.

The board you are using is not an official Arduino board, so the description may not contain the keyword Arduino.

You can try printing out "desc" in the "for port" loop to see what the board description may actually be.

@MrY Yes my board is the samdD51 grand centeral board from adafruit.

Does the "desc" string contain Arduino? If not, that is why your code returns "No Arduino found".

@MrY Got you. Now I see it. With your code How did it know there was a somewhat arduino clone or aftermarket board on there with arduino coding?

if the port description cannot identify the board I find the simplest way is to scan thru the list of serial ports prompting each one for a response , e.g. with a '?'
a board would be programmed to respond to '?' with an identifier, e.g. "ESP32 canbus tester"
if no response is received within a set time or an invalid response is received move onto the next port

@horace Is there a way to change it so it can identify that it is an arduino or another name Or put something in to identify it?

I modified your code to:

def find_arduino_port():
    ports = serial.tools.list_ports.comports()
    for port, desc, hwid in ports:
        # if "Arduino" in desc:
        #     return port
        print(f'port: {port} desc: {desc} hwid: {hwid}')
    return None


find_arduino_port()


Here is the output for the Leonardo:

port: /dev/ttyACM0 desc: Arduino Leonardo hwid: USB VID:PID=2341:8036 LOCATION=5-2:1.0

You may use the VID:PID value to find a unique board type.

There is no way that I know of to modify the data being returned by a board.

@MrY I got this:

port: COM8 desc: Standard Serial over Bluetooth link (COM8) hwid: BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_VID&000105D6_PID&000A\7&17F6F26C&0&F0BE25618A58_C00000000
port: COM3 desc: USB Serial Device (COM3) hwid: USB VID:PID=239A:8020 SER=2D16A11D5337543239202020FF103E0C LOCATION=1-1:x.0
port: COM7 desc: Standard Serial over Bluetooth link (COM7) hwid: BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&0000\7&17F6F26C&0&000000000000_00000006

So, I am assuming that the board is connected to COM3.
To find the board, you must see if 239A:8020 is contained within the hwid string.

Here is some sample code:

import serial.tools.list_ports


def find_arduino_port():
    ports = serial.tools.list_ports.comports()
    for port, desc, hwid in ports:
        # if "Arduino" in desc:
        #     return port
        # print(f'port: {port} desc: {desc} hwid: {hwid}')
        if '239A:8020' in hwid:
            print("found the board")
    return None


find_arduino_port()

If you need to find a specific instance of the board, you would qualify against the PID:VID and the serial number.

What i need to be able to do is figure out what port it is on and connect to it. Problem is I have many of these boards with all the same processor.

Edit: So do I just search for 239A:8020. Wondering if all the boards have has pid vid like that?

When you say you have many of these boards, do you mean multiple samd51 grand central boards or multiple boards of different types?

The VID:PID is a vendor and product ID assigned uniquely to each USB board type.

If you plug the number into this web page, it will identify the board.

If you have multiple boards of the same type plugged in, you must qualify each board found against its VID:PID and its serial number. You would then use the port to know which board to access.

Yes multiple boards the same boards all with the same micro processor.

All the boards will have the same VID:PID. If you just want to find the USB ports, retrieve the port attribute. However, if you want to find a specific board, you will need to know its serial number. You would then search for the serial number in the hwid and use its port value to know which comport it is connected to.

I am assuming that Adafruit assigns a unique serial number to each board. That is simple to determine. Plug in a couple of boards, and print out the full hwid for each.