# [SOLVED] Communicating with /dev/ttyUSB0

## Jenk

Apologies if this is the incorrect forum.

I've got a serial GPRS modem on /dev/ttyUSB0. It works great with wvdial, hardware is fine etc. etc. but as the systems we are going to be rolling out will be using multiple modems, each with a different telephone network, each modem will have differing username/password combinations amongst other parameters. 

Now, the sharp witted of you may also note they are USB modems converted to serial devices - this is where the problem is sprung. If, for example, a device dies, or is disconnected for whatever reason - the other modems will "shift" along the device chain, so /dev/ttyUSB1 will become /dev/ttyUSB0 - which when used with wvdial will not work - incorrect username/password.

So - I've been tasked with finding out how to interrogate the modem into fessing up which network it will be connecting to. (i.e. which chip ID does the sim within hold) but I can't find anything anywhere that is going to let me run a series of terminal (using bash, btw) commands to do said interrogation.

Does anyone know how I can communicate with this modem?

Many thanks.Last edited by Jenk on Mon Nov 19, 2007 5:21 pm; edited 1 time in total

----------

## Inte

Give net-dialup/minicom a try.

----------

## Jenk

Hi, thanks, that did help - however, what I was hoping to achieve is something like:

```
root@somehost # echo "AT+CIMI" > /dev/ttyUSB0
```

and retrive the value of the IMSI number from that.

----------

## Inte

You can do this. Just open two terminals, execute cat /dev/ttyUSB0 at the first for monitoring and echo -e "command" > /dev/ttyUSB0 at the second one.

Maybe you've to play with the -n option and \n (echo -ne "command\n" > /dev/ttyUSB0 for example). It's months ago I configured my UMTS card this way.

----------

## Jenk

I've tried this, what I'm sending:

```
# echo -en "AT+IMSI\n" > /dev/ttyUSB0

```

and what I'm receiving (in a second terminal - cat is run before I send):

```
# cat /dev/ttyUSB0

AT+IMSI

```

note the echo of the command, but no reply.

The other difficulty is that ultimately, I will be using this in a script which will only be creating symlinks to the device(s) based upon which provider the IMSI produces; so that another application can ascertain which wvdial configuration to use for dialing. e.g. IMSI's begining with 23410 use the provider O2, so a symlink for them will be like: 

```
ln -s /dev/ttyUSBx /dev/modems/o2x
```

 whilst the IMSI's begining with 23413 are t-mobile, so they will end up with: 

```
ln -s /dev/ttyUSBx /dev/modems/tmobilex
```

 where x is the sequential device number.

I've reached marginal success using cu; e.g.: 

```
# cu -l /dev/ttyUSB0

AT+CIMI

23410xxxxxxxxxxxxx

OK

~.

Disconnecting

#
```

Though I have to manually type the AT+CIMI and ~. lines, I can't see how to pipe these into cu?

----------

## Jenk

Problem resolved;

Here is an example of my script. Note I send \r instead of \n.  :Smile: 

```
#!/bin/bash

for i in $( ls /dev/ttyUSB* )

do

        exec 5<$i

        echo -en "AT+CIMI\r" > $i

        echo ' ' > $i

        while 0<&5 read line

        do

                if [ ${line:0:5} = '23410' ]

                then

                        echo "Device ${i} is provided by O2"

                        break

                fi

                if [ ${line:0:5} = '23430' ]

                then

                        echo "Device ${i} is provided by T-Mobile"

                        break

                fi

        done

done
```

----------

