Background
I'm trying to connect to a device using UART
though a micro-USB cable and GNU screen
from Ubuntu and OSX.
On both Ubuntu and OSX I can get the most recent serial connection with the following command, which I have run just after having connected my device and turned it on.
ls -t /dev/tty* | head -n 1
On Ubuntu I get (this is actually the output of ll
and -n 3
so I can see better what's shown)
crw-rw---- 1 root dialout 166, 0 Jan 29 10:51 /dev/ttyACM0
crw-rw-rw- 1 root tty 5, 0 Jan 29 10:14 /dev/tty
crw-rw---- 1 root dialout 4, 68 Jan 21 16:19 /dev/ttyS4
and the first item is correct (i.e. I can screen /dev/ttyACM0 115200
).
On Mac, instead, I get this
crw--w---- 1 atcold tty 16, 4 29 Jan 10:56 /dev/ttys004
crw-rw-rw- 1 root wheel 19, 44 29 Jan 10:56 /dev/tty.usbmodemfd121
crw--w---- 1 atcold tty 16, 6 28 Jan 16:21 /dev/ttys006
and the first item is wrong (I cannot connect to it) whereas the working one is the second one (and I had to turn off and turn on my device twice, since the first time there was not any /dev/tty.usbmodemfd121
at all).
Question
My question is easy. How can I access automatically the correct serial connection? I'm trying to avoid to give too many troubles to the final user trying to sort this problem out automatically.
Answer
This is my final script :)
read -p "Connect the switched off board, then press <Enter>" temp
ls /dev/tty* > /tmp/old_tty
read -p "Turn on the board and press <Enter>" temp
ls /dev/tty* > /tmp/new_tty
screen $(comm -13 /tmp/old_tty /tmp/new_tty) 115200