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"
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
Code:
cat -v < /dev/ttyUSB0
Code:
#! /bin/sh
while true
do
cat -v < /dev/ttyUSB0
done
برچسب: shell read answer,
نویسنده: استخدام کار