简体   繁体   中英

TCL expect output

small script that should return line values but does not

#!/usr/bin/expect -f
log_user 0

set device "34:85:18:6a:52:52"

spawn gatttool -b $device -I
expect " > "
sleep 1
send -- "connect\r"
expect "Connection successful"
sleep 1
set message1 ""

expect {
    -re "Notification handle = 0x0033 value: (.+\n)" {
        set message1 ${message1}$expect_out(1,string)
        exp_continue
    }
    "^\[.+\]\[LE\]> $" {
        send -- "disconnect\r"
    }
}

puts "$message1"

the output im trying o save looks li this from normal gatt to address

mike@raspberrypi:~/Downloads $ gatttool -b 34:85:18:6a:52:52 -I 
[34:85:18:6a:52:52][LE]> connect
Attempting to connect to 34:85:18:6a:52:52
Connection successful
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 51 00 89 00 00 00 05 1d 4c 
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 4c 00 89 00 00 00 05 1d 4c 
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 fd 10 4d 00 89 00 00 00 05 1d 4c 
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 51 00 89 00 00 00 05 1d 4c 
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 50 00 89 00 00 00 05 1d 4c 
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 50 00 89 00 00 00 05 1d 4c 
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 54 00 89 00 00 00 05 1d 4c 
[34:85:18:6a:52:52][LE]> exit

(gatttool:29107): GLib-WARNING **: 11:37:04.060: Invalid file descriptor.
mike@raspberrypi:~/Downloads $ 

tried pipe'n output it returns empty file, would really just like to return a value and tried awk but for some reason i dont think it working correctly to return the values

Here's a simple example using my sexpect . You can get the idea and update your Expect script accordingly.

The script ( gatttool.sh ):

export SEXPECT_SOCKFILE=/tmp/gatttool-$$.sock

sexpect spawn -idle 300 gatttool -b 34:85:18:6a:52:52 -I
sexpect expect -exact '[LE]> '
sexpect send -cr connect

message=
pat=$'\[LE\]> |Notification handle = 0x0033 value:([ [:xdigit:]]+)[[:blank:]]*[\r\n]+'
while true; do
    sexpect expect -re "$pat" || exit 1
    out=$( sexpect expect_out -index 1 )
    [[ -n $out ]] && message=$message$out || break
done

sexpect send -cr exit
sexpect wait

echo "---- the message ----"
echo "$message"

Test result (using a fake gatttool ):

$ bash gatttool.sh
[34:85:18:6a:52:52][LE]> connect
Attempting to connect to 34:85:18:6a:52:52
Connection successful
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 51 00 89 00 00 00 05 1d 4c
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 4c 00 89 00 00 00 05 1d 4c
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 fd 10 4d 00 89 00 00 00 05 1d 4c
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 51 00 89 00 00 00 05 1d 4c
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 07 ff 10 50 00 89 00 00 00 05 1d 4c
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 50 00 89 00 00 00 05 1d 4c
Notification handle = 0x0033 value: 1e ff 02 09 03 1c 44 01 08 00 10 54 00 89 00 00 00 05 1d 4c
[34:85:18:6a:52:52][LE]> exit
---- the message ----
 1e ff 02 09 03 1c 44 01 08 00 10 51 00 89 00 00 00 05 1d 4c 1e ff 02 09 03 1c 44 01 07 ff 10 4c 00 89 00 00 00 05 1d 4c 1e ff 02 09 03 1c 44 01 07 fd 10 4d 00 89 00 00 00 05 1d 4c 1e ff 02 09 03 1c 44 01 07 ff 10 51 00 89 00 00 00 05 1d 4c 1e ff 02 09 03 1c 44 01 07 ff 10 50 00 89 00 00 00 05 1d 4c 1e ff 02 09 03 1c 44 01 08 00 10 50 00 89 00 00 00 05 1d 4c 1e ff 02 09 03 1c 44 01 08 00 10 54 00 89 00 00 00 05 1d 4c

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM