I'm writing a little script right now which sends some data to the Arduino through a bash shell script. The command that sends the data looks like this:

alias led_green="echo -e -n '\x10' > /dev/ttyACM1"

As you can see, I have /dev/ttyACM1 hard coded into the program. This is problematic, because if I change USB ports the value will change as well, and my script won't work. I'm wonder if there's a way to programatically tell which port the arduino is connected to so the script will always run as long as there's an arduino connected.


I would suggest switching from alias to a function , which uses a loop to go through a list of the device files.

Assuming there's no other devices , you can use a for loop to determine which device exists.

$ for i in $(seq 0 99) ; do                                                    
>    [ -e "/dev/ttyACM$i"  ] && break                                            
> done

If you echo the value of $i after the loop exits, that will be the value of the found device .

However , if for instance you have /dev/ttyACM0 and /dev/ttyACM1 devices there and they run at different baud rate you can loop through them and test for stty -a -F /dev/ttyACM$i | grep $BAUD_RATE

If both devices run on the same baud rate , well . . . there's not a lot to do - have to use manual way.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.