Arduino serial communication and uploading program to a specific arduino

How to fetch the com port or any id I can use to upload specific program to a specific arduino?
My project is a head mouse for which I am using two arduino boards . I want to upload a specific program to a specific arduino. How can I do it? And if I need to use a pid or com port id about which the official arduino forum people suggested, how can I retrieve this id? I'm using an Arduino micro and a pro micro if it helps any.

What PC operating system are you using?

On Linux if I am careful to connect my Arduinos in a specific order I can be sure which one is on ttyACM0 and which is on ttyACM1

I have written a short Python program to compile and upload my code using the Ardunio IDE command line. At the top of my .ino files I have some comments like this which tell Python which serial port to use

// python-build-start
// action, verify
// board, arduino:avr:uno
// port, /dev/ttyACM0
// ide, 1.8.6
// python-build-end

...R

The information that you're looking for is the USB VID and PID of the USB device after you've reset the board. They are unique for each device model.

You can find them in Windows device manager (or the linux/mac equivalents). I suspect that you can also find it the the boards.txt file, but I'm not behind a PC at the moment to check.

You will have to explain how you intend to use the information and which OS you're using.

sterretje:
They are unique for each device model.

I think that means that two Unos (for example) would report the same values?

...R

Robin2:
I think that means that two Unos (for example) would report the same values?

...R

Actually I am using an Arduino Pro Micro to get the cursor movement data and an Arduino Pro to get and send data from the Electrohouse voice recognition module V3.1. I am using Windows 10 64 bit for the operating system. I want to retrieve the USB VID or PID to use it in my code to upload it to a specifice arduino such as the .hex file of the "a.ino" to the micro and "b.ino" to the pro micro one at a time. This project I am doing is a client project. Thus, I can't make the user to connect the arduino to specific usb ports to connect the boards in a specific order. Because the arduino boards and the other components connected to it will be hidden inside a box or something for abstraction and the user won't know which cable connects to a specific arduino.

Robin2:
I think that means that two Unos (for example) would report the same values?

...R

To my knowledge, yes.

Nahian_Alindo:
This project I am doing is a client project. Thus, I can't make the user to connect the arduino to specific usb ports to connect the boards in a specific order. Because the arduino boards and the other components connected to it will be hidden inside a box or something for abstraction and the user won't know which cable connects to a specific arduino.

I think that will require a third "computer" inside the device that can receive the new code from the user and route it to the appropriate Arduino. A RaspberryPi should be able to do that nicely - but there are probably other options.

...R

It probably can be easier (no need to use VID and PID). Below example using powershell to find the serial ports, filter for a Leonardo and reset the Arduino; tested on Windows 8. I did not add the upload part

# regular expression pattern to detect specific device
$reSerial = "(Arduino Leonardo) \((COM\d+)\)"
$reBootloader = "(Arduino Leonardo bootloader) \((COM\d+)\)"

# get serial ports
$devices = (gwmi -query "select * from win32_pnpentity" | where {$_.Name -match "COM\d+"}).name

$port = "N/A"
$arduino = "N/A"

# loop through ports
foreach ($dev in $devices)
{
  # and find arduino serial port
  if($dev -match $reSerial)
  {
    $arduino = $Matches[1]
    $port = $Matches[2]
    break;
  }
}

Write-Host $port
Write-Host $arduino

# open and close port at 1200 baud to force a reset
$serial= new-Object System.IO.Ports.SerialPort $port,1200,None,8,one
$serial.open()
$serial.close()

# wait a little
Start-Sleep -Seconds 2

# get serial ports
$devices = (gwmi -query "select * from win32_pnpentity" | where {$_.Name -match "COM\d+"}).name

$port = "N/A"
$arduino = "N/A"

# loop through ports
foreach ($dev in $devices)
{
  # and find arduino serial port
  if($dev -match $reBootloader)
  {
    $arduino = $Matches[1]
    $port = $Matches[2]
    break;
  }
}

Write-Host $port
Write-Host $arduino

# upload here

Output

COM28
Arduino Leonardo
COM29
Arduino Leonardo bootloader

Adjust (and harden) to your needs :wink:

PS
Please be aware that your clients might not necessarily use Windows :slight_smile: