Shell read answer from USB device

ساخت وبلاگ

Hi all,
I need to read answer from an USB device after I've sent it a command.
I can send the wanted code to /dev/ttyUSB0 using the following script:

Code:

#! /bin/bash
# launch with ./program.sh filename
# file has to be in HEX mode, exemple:
# x02x33x07
# but file contents looks like:
# 02 03 07
# init serial port for USB device
stty -F /dev/ttyUSB0 38400 cs8 parenb
# replace all occurences of space by x in file passed as argument
sed -i 's| |\x|g' "$1"
# remove empty lines in file passed as argument
sed -i '/^$/d' "$1"
# send each line to serial port, pause between each line
while read -r line
do # add x for the 1st hex code and send line to USB device echo -ne 'x'"$line" >/dev/ttyUSB0 # let's pause as we can't read answer from device sleep 0.18
done < "$1"
and the following file:

Code:

02 31 06 00 35 31 41 0a 52 45 53 45 41 55 20 52 41 49 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 a9
02 31 06 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 77
02 31 06 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 b7
02 31 06 c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 f7
The thing is that the device anwers either 0x03 for OK or 0x04 for KO after each line.
Instead of making a stupid pause (and because the file will be really bigger than this test one), I'd like to wait for the device answer before sending the next line.
Up to now, I've tried (nothing displayed)

Code:

cat -v < /dev/ttyUSB0
Tried in a 2nd terminal (nothing displayed)

Code:

#! /bin/sh
while true
do cat -v < /dev/ttyUSB0
done
Tried screen without success (nothing displayed)
I can well see the answer from the device using usbmon/vusn-analyzer or wireshark.
How to watch for this in shell ?
Thanks in advance for your thoughts.
Nicolas

feed all...
ما را در سایت feed all دنبال می کنید

برچسب : shell read answer, نویسنده : استخدام کار feedoll بازدید : 326 تاريخ : چهارشنبه 3 شهريور 1395 ساعت: 13:08