Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
share|improve this question

2 Answers 2

up vote 0 down vote accepted

A solution could be to dectect the active serial connection before and after plugging the micro-usb, and then connect to the one present only after the micro-usb is plugged. This can be done for example with an sh script

#!/bin/sh
PATH_SUFFIX="/dev/tty"

read -p "Be sure the mini usb adapter is unplugged then press ENTER" temp
BEFORE=`ls -1 ${PATH_SUFFIX}* `
read -p "Now plug device and then press ENTER" temp
# sleep 2 seconds to be sure the filedevise is created
sleep 2
AFTER=`ls -1 ${PATH_SUFFIX}*`

OLD_IFS=$IFS

IFS='
'

for file in $AFTER ; do
    if case ${BEFORE} in *"${file}"*) false;; *) true;; esac; then
        FOUND=$file
    fi
done

IFS=$OLD_IFS

#Now do whatever with FOUND device
echo $FOUND
share|improve this answer
    
LOL, I cannot believe I had /dev/ttys004 also before connecting my device (since its time stamp, I was sure it was generated by connecting my device)! Do you have any suggestion (sh code snippet) about detecting the new active serial connection? –  Atcold Jan 29 at 21:05
    
@Atcold I added a sample sh script –  Alepac Jan 30 at 16:15
    
A-W-E-S-O-M-E-!-!-! –  Atcold Feb 3 at 19:39

You might want to look at setting up a udev rule to create a symlink with a specific name for this serial device.

Assuming you can identify the device by vendor or serial number.

This should help:

http://noctis.de/ramblings/linux/49-howto-fixed-name-for-a-udev-device.html

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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