Little script with minicom

Hi all,
i must do a little script that send AT command at ttyUSB3..
for example:

Start Minicom:
Set serial port ttyUSB3
AT+CREG?
if response != OK then do something
...other AT command (open a socket tcp/ip with at comman)
Send a file .txt for example.
exit minicom.

I saw on the network command "expect" but I have not undarstand how to launch minicom and give it commands to set the right port ..

thanks for help

You'd be better off using stty than trying to script keypresses in an interactive program.

I wrote this to deal with a modem once. Probably not the best in the world but it worked and should do for simple commands.

#!/bin/bash

MODEM="/dev/ttyS3"

function get_response
{
        local ECHO
        # cat will read the response, then die on timeout
        cat <&5 >$TMP &
        echo "$1" >&5
        # wait for cat to die
        wait $!

        exec 6<$TMP
        read ECHO <&6
        if [ "$ECHO" != "$1" ]
        then
                exec 6<&-
                return 1
        fi

        read ECHO <&6
        read RESPONSE <&6
        exec 6<&-
        return 0
}

TMP="./response"

# Clear out old response
: > $TMP

# Set modem with timeout of 5/10 a second
stty -F "$MODEM" 9600 -echo igncr -icanon onlcr ixon min 0 time 5

# Open modem on FD 5
exec 5<>"$MODEM"

get_response "AT+CGMI" || echo "Bad response"
echo "Response was '${RESPONSE}'"       ;       cat $TMP

echo

get_response "AT" || echo "Bad response"
echo "Response was '${RESPONSE}'"       ;       cat $TMP

exec 5<&-

9600 might not be what your modem -- if it is a modem -- defaults to.

and how send a file..

I open a socket tcp/ip with at command and after in Hyper terminal on windows i click Trasfer->File and send file but here how do this?
thanks

You will have to use something like kermit or zmodem (sz) to send files.

What OS are you using?
Most do not have these programs installed by default, so you will probably need to install (e.g. on linux you can do yum install lrzsz for zmodem).

I don't think he means literally uploading a binary file. I think he just means putting the contents of a text file into the modem.

cat < file > /dev/ttyS3
2 Likes

I use minicom. but is possibile TOdo a script that automatically Run minicom. Set port ttyusb3 and send at command or send a file at server and after disconnect and close minicom. is possible?

It's very difficult to script interactive things. It looks easy because it's easy for a human to use one, but making a program watch for things and respond appropriately is full of pitfalls. If you can avoid it that would be better.

@enaud
We had a lot of trouble understanding your previous post and recommended that you contact the software authors (when we eventually found out which product called minicom you were posting about).
http://www.unix.com/shell-programming-scripting/180257-script-command.html

However, in general if you can type commands at the command line you can usually compose an expect script.

What are you trying to do?

and How can I do?

---------- Post updated at 06:26 AM ---------- Previous update was at 03:15 AM ----------

Corona,
i have an error with your script:

giuseppe@giuseppe-K53SV:~/Scrivania$ ./scriptmodem2
stty: /dev/ttyUSB3: impossibile eseguire tutte le operazioni richieste
Response was ''
AT+CGMI

Response was ''
AT
giuseppe@giuseppe-K53SV:~/Scrivania$

I have change
MODEM="/dev/ttyUSB3"
and
stty -F "$MODEM" 115200 -echo igncr -icanon onlcr ixon min 0 time 5

---------- Post updated at 06:47 AM ---------- Previous update was at 06:26 AM ----------

I need port ttyUSB3 115200 8N1
how et this in stty?
thanks

I don't have a modem now (left it in Antarctica believe it or not), so it is hard to test/script this for you, but Corona is certainly right that you would not use minicom.

PPPD comes with 'chat' which is a good way to do the initial parts of opening connection. I would use that. Again, untested:

exec 3<&1 >"$modem" <"$modem"
stty 115200 clocal ...etc...
if chat -v -s 'AT+CREG OK ATopencmd CONNECT'; then
   echo "error setup modem" >&2
   exit 1
fi
cat file
echo "done" >&3

I can't read that error message so don't know why it's not working, is translation possible?

Corona:

giuseppe@giuseppe-K53SV:~/Scrivania$   ./scriptmodem2
stty: /dev/ttyUSB3: impossible to perform all tasks required
Response was ''
AT+CGMI

Response was ''
AT
giuseppe@giuseppe-K53SV:~/Scrivania$

mmmmm

I think that stty is setted badly

(The error messages seem to be in Italian).

yes :):b:

---------- Post updated at 12:09 PM ---------- Previous update was at 11:46 AM ----------

I have doing a script :

#! /bin/sh

cat /dev/ttyUSB3 /home/giuseppe/Scrivania/imsi.txt &
sleep 1
# stty ispeed 115200 ospeed 115200 -echo not work!
# echo AT > /dev/ttyUSB3 not work!
chat -V -s '' 'AT+creg?' '' > /dev/ttyUSB3 < /dev/ttyUSB3
sleep 2
chat -V -s '' 'AT' '' > /dev/ttyUSB3 < /dev/ttyUSB3
sleep 2
chat -V -s '' 'AT!PADSETUP=1,2,82.91.28.127,0,9940,0,1' '' > /dev/ttyUSB3 < /dev/ttyUSB3
sleep 2
chat -V -s '' 'AT+CGDCONT=1,"IP","web.omnitel.it"' '' > /dev/ttyUSB3 < /dev/ttyUSB3
sleep 2
chat -V -s '' 'AT!PADCONN=1' '' > /dev/ttyUSB3 < /dev/ttyUSB3
sleep 10
killall cat
exit 0

but i not have a control! you can help for merge your script with mine?
Sorry but i not undstand this string:
exec 3<&1 >"$modem" <"$modem"

you can explain me, thank!
thanks!

This is a script you already had? Did it ever work? :slight_smile:

What do you mean?

You see you do "</dev/ttyUSB3 >/dev/ttyUSB3" after each chat. By using exec, we basically permanently redirect. First though we copy the current stdout to 3 so we can use it later to print messages. So in this way, the modem is not open/closed between programs, which I found may cause problems with some things ...

Your script doesn't use chat to it's ability. It doesn't check response. To avoid quoting issues, place this in a separate file (ie: omnitel.chat)

ABORT    'NO CARRIER'
ABORT    ERROR
''       AT+CREG?
OK       'AT!PADSETUP=1,2,82.91.28.127,0,9940,0,1'
OK       'AT+CGDCONT=1,"IP","web.omnitel.it"'
OK       'AT!PADCONN=1'
CONNECT  '\c'

Then your shell code would be easier

modem=/dev/ttyUSB3
file=/home/giuseppe/Scrivania/imsi.txt
chat_script=/home/giuseppe/Scrivania/omnitel.chat

exec 3<&1 >"$modem" <"$modem"
stty 115200 clocal ...etc...
if chat -v -s -f "${chat_script}"; then
    echo "error setup modem" >&2
    exit 1
fi
cat "$file"
echo "done" >&3

My script work.. tomorrow i test your.
what is in your script stty etc... ? I need 115200 8n1

I've never encountered the options not already set for 8N1, usually just putting the speed is enough and maybe setting flow control. "stty 115200 raw" seems to set it up OK. I connected my cell phone and tried the script and it work.

Ok.. I have this error with your script

root@ubuntu:~/Desktop/SCRIPTMODEM# sh modem2.sh
stty: /dev/ttyUSB3: unable to perform all requested operations
abort on (NO CARRIER)
abort on (ERROR)
send (AT+CREG?^M)
expect (OK)
^M
+CREG: 0,1^M
^M
OK

-- got it

send (AT!PADSETUP=1,2,82.91.28.127,0,9940,0,1^M)
expect (OK)
^M
^M
OK

-- got it

send (AT+CGDCONT=1,"IP","web.omnitel.it"^M)
expect (OK)
^M
^M
OK

-- got it

send (AT!PADCONN=1^M)
expect (CONNECT)
^M
^M
OK^M
^M
CONNECT

-- got it

send ()
error setup modem

but i need a script like this
fore example:

IF not exist /dev/ttyUSB3 echo "MODEM NOT CONNECTED" and exit
ELSE (exist /dev/ttyUSB3) {
AT+CREG?
if ouput is different from +creg: 0,1 then echo "MODEM NOT REGISTRED" and redo at+creg
AT!PADCONN=1
if output is different from CONNECTED 1,0 then "IMPOSSIBLE OPEN A SOCKET" and redo at!PADCONN=1
}

how i can do?

PLS THANKS

learn shell scripting...

also i had an error, this line needs negated with a ! before command:

if ! chat -v -s -f "${chat_script}"; then

You can test if /dev/ttyUSB3 exists with [ -e "$modem" ]
You can use individual chat commands like you did before, in an if condition.

send_expect() {
    chat -v -f 'ABORT ERROR' '' "$@" ''
}

die() {
    echo "$@" >&2
    exit 1
}

if ! [ -e "$modem" ]; then
    die 'Modem not connected'
fi

if ! send_expect 'AT+CREG?' '+CREG: 0,1'; then
    die 'Modem not registered'
fi

if ! send_expect 'AT!PADCONN=1' 'CONNECT'; then
    die 'Unable to open socket'
fi

Thanks but i not undstand where put this function.
This is my new script but not work
i have an error on stty:standard input: unable to perform all requested operations

#! bin/sh

modem=/dev/ttyUSB3
file=/home/giuseppe/Desktop/SCRIPTMODEM/imsi.txt
chat_script=/home/giuseppe/Desktop/SCRIPTMODEM/omnitel.chat

exec 3<&1 >"$modem" <"$modem"

stty 115200 clocal
if ! chat -v -s -f "${chat_script}"; then
    echo "error setup modem" >&2
    exit 1
fi

die() {
    echo "$@" >&2
    exit 1
}
send_expect() {
    chat -v -f 'ABORT ERROR' '' "$@" ''
}

if ! [ -e "$modem" ]; then
    die 'Modem not connected'
fi

if ! send_expect 'AT+CREG?' '+CREG: 0,1'; then
    die 'Modem not registered'
fi

if ! send_expect 'AT!PADCONN=1' 'CONNECT'; then
    die 'Unable to open socket'
fi

cat"$file"
echo "done" >&3

you can write complete script pls?
thanks

---------- Post updated at 10:33 AM ---------- Previous update was at 08:23 AM ----------

i have do a new script following you:

#! /bin/bash
exec 2>error

cat /dev/ttyUSB3 /home/giuseppe/Desktop/SCRIPTMODEM/imsi.txt &
sleep 1

# CONTROLLO MODEM CONNESSO
if ! [ -e /dev/ttyUSB3 ]; then
    echo 'Modem non connesso'
    exit 0
fi

if ! chat -V -f 'ABORT ERROR' '' "$@" '' 'AT' 'OK' > /dev/ttyUSB3 < /dev/ttyUSB3; then
    echo "$@" 'ERRORE AT 1'
fi

sleep 2
killall cat
exit 0

but i view always ERRORE AT1.

:eek: