Myk
Myk

Reputation: 279

How to find tty of USB modem on raspberry pi?

I want send AT commands to USB modem connected to raspberry pi. I have found some helps mentioning that USB modem is usually connected as ttyACM or ttyUSB device, but I have no ttyACM or ttyUSB device. I have devices from tty1 to tty63, but I do not know how to identify which one is used by the modem.

I have tried to find the device by storing devices list with the modem unconnected and then compare device list after the modem is pluged in:

ls /dev/ > dev_list_1.txt
(plug the modem in)
ls /dev/ | diff --suppress-common-lines -y - dev_list_.txt

returns to me:

bsg                               <
sda                               <
sg0 

I have tried to connect the modem with cu tool:

sudo cu -l sda
sudo cu -l sg0

but both returns:

cu: open (/dev/sg0): Permission denied
cu: sg0: Line in use

So I tried also use minicom and configure serial communication to /dev/sg0 or /dev/sda but it does not work either.

So I think I need to find right tty device used by the modem to be able to communicate with it. But how to find it?

Upvotes: 2

Views: 3178

Answers (2)

Orsiris de Jong
Orsiris de Jong

Reputation: 3016

I'm using picocom >=3.0 to identify which tty accepts AT commands for USB modems (tested on Sierra EM7455).

My script looks like

#!/usr/bin/env bash

for tty in $(ls /dev/ttyUSB*); do
    echo "Checking $tty"
    picocom -qrX -b 9600 $tty
    sleep 1
    result=$(echo "AT&F" | picocom -qrix 1000 $tty)
    if [ "$result" = "AT&F" ]; then
        echo "Found AT compatible modem at $tty"
    else
        echo "No AT compatible modem at $tty"
    fi
done

Of course you can adapt this by replacing /dev/ttyUSB* with whatever fits your modem.

Upvotes: 0

hlovdal
hlovdal

Reputation: 28238

You can look for /sys/class/tty/*/device entries and ignore all the links that points to serial8250 since those are not USB devices.

With shell script:

for device in /sys/class/tty/*/device
do
    case $(readlink $device) in
        *serial8250)    # e.g. ../../../serial8250
            ;;
        *)              # e.g. ../../../1-3:3.1
            echo $device | rev | cut -d/ -f2 | rev
            ;;
    esac;
done | sed 's@^@/dev/@'

which produces

/dev/ttyACM0
/dev/ttyACM1
/dev/ttyS0

with my phone connected. You might get more than the usb devices (e.g. ttyS0) but at least this should give you all the usb serial devices the kernel know about (and if the device does not populate /sys/class/tty/ it almost certainly is not a serial device).

This is based on the list_ports function logic in the libserialport library.

Upvotes: 0

Related Questions