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 ..
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.
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 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.
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
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$
This is a script you already had? Did it ever work?
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'
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.
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
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
}
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