Sponsored Content
Operating Systems Linux How do I capture responses from the chat command? Post 302494787 by Corona688 on Tuesday 8th of February 2011 11:26:35 AM
Old 02-08-2011
You don't have to throw the output away, you know. I just did so to make the example small... As for capturing 'all' the output of the modem, it really doesn't tell you when it ends, so chat's not doing anything you can't. I still think common utilities are preferred over something so ossified it's undocumented and/or dropped by half the UNIX world..

If you want it to show up as whole whacks of text at once you can do this:

Code:
$ cat modem.sh
#!/bin/bash
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 $!
        # Check if the modem echoed our command
        read ECHO <$TMP
        [ "$ECHO" != "$1" ] && return 1
        return 0
}

TMP="./response"

# Clear out old response
: > $TMP

# Set modem with timeout of 5/10 a second.  Things reading will read until
# data stops, wait 1/2 second, then quit.
stty -F /dev/ttyS0 9600 -echo igncr -icanon onlcr ixon min 0 time 5

# Open modem on FD 5
exec 5<>/dev/ttyS0

get_response "AT+CGMI" || echo "Bad response"
echo "Response was:"    ;       cat $TMP

get_response "AT" || echo "Bad response"
echo "Response was:"    ;       cat $TMP

exec 5<&-

$ ./modem.sh
Response was:
AT+CGMI

ERROR
Response was:
AT

OK
$

I simplified it a lot using features from ksh/zsh/bash, particularly functions and the exec 5<>/dev/ttyS0 which lets you read and write to the same FD.

---------- Post updated at 10:26 AM ---------- Previous update was at 10:00 AM ----------

this is kind of fun
Code:
#!/bin/bash

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 /dev/ttyS0 9600 -echo igncr -icanon onlcr ixon min 0 time 5

# Open modem on FD 5
exec 5<>/dev/ttyS0

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<&-

$ ./modem.sh
Response was 'ERROR'
AT+CGMI

ERROR

Response was 'OK'
AT

OK
$


Last edited by Corona688; 02-08-2011 at 12:17 PM..
This User Gave Thanks to Corona688 For This Post:
 

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

talk command to chat

Hi, Could you please advice on the following query: There are 2 users on a unix box: 1. aaaa 2. bbbb I open 2 putty sessions and login with the above 2 users. Then I type the following using the aaaa user to chat with bbbb. talk bbbb or talk bbbb@hostname Result: the screen goes... (1 Reply)
Discussion started by: miltonkeynesguy
1 Replies

2. UNIX for Dummies Questions & Answers

talk command to chat

Hi, Could you please advice on the following query: There are 2 users on a unix box: 1. aaaa 2. bbbb I open 2 putty sessions and login with the above 2 users. Then I type the following using the aaaa user to chat with bbbb. talk bbbb or talk bbbb@hostname Result: the screen goes... (4 Replies)
Discussion started by: miltonkeynesguy
4 Replies

3. UNIX for Dummies Questions & Answers

Doing a capture while another command is executing?

Basically what i'm trying to do is execute an update command and at the same time have the system do a TCPdump to file for that update traffic. So I would like to connect the two commands so that the tcpdump terminates automatically when the update finishes/fails/whatever. Right now I have... (0 Replies)
Discussion started by: MrEddy
0 Replies

4. Web Development

Can you embed Skype or any other video chat/chat program into a webpage?

Hi, I am trying to embed Skype or any other video chat/chat program into a webpage. Has anyone had success doing this? or know how? Thanks Phil (2 Replies)
Discussion started by: phil_heath
2 Replies

5. What is on Your Mind?

Very Funny and Somewhat Amazing 2006 Chat Bot Chat

Working on the badging system, Just found this old thread for 2006 and started reading it. ROTFL ... what a great discussion between forum members and our chat bot Gollum "back in the good old days"... You must check this out if you want a laugh and big smile: ... (1 Reply)
Discussion started by: Neo
1 Replies
All times are GMT -4. The time now is 01:40 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy