I'm writing a simple script that reads line from a serial line and choose in which file save the string in function of the first char of the line.
My input is like this:
C{"value":0.0,"setpoint":0.0,"isAuto":0}\n
B{"value":0.0,"setpoint":0.0,"isAuto":0}\n
C{"value":0.0,"setpoint":0.0,"isAuto":0}\n
D{"value":0.0,"setpoint":0.0,"isAuto":0}\n
...
And here the script I'm writing:
#!/bin/bash
stty -F /dev/ttyS1 115200 -echo
echo "Receiver is listening..."
while read -r line < /dev/ttyS1; do
echo $line
target=${line:0:1}
echo $target
if [ "$target" = "C" ]; then
echo ${line#?} > /tmp/file1
elif [ "$target" = "D" ]; then
echo ${line#?} > /tmp/file2
elif [ "$target" = "T" ]; then
echo ${line#?} > /tmp/file3
elif [ "$target" = "A" ]; then
echo ${line#?} > /tmp/file4
fi
done
but with the input above I see the following:
C{"value":0.0,"setpoint":0.0,"isAuto":0}
C
C{"value":0.0,"setpoint":0.0,"isAuto":0}
C
uto":0}
u
C{"value":0.0,"setpoint":0.0,"isAuto":0}
C
}
}
C{"value":0.0,"setpoint":0.0,"isAuto":0}
C
uto":0}
u
C{"value":0.0,"setpoint":0.0,"isAuto":0}
C
uto":0}
u
C{"value":0.0,"setpoint":0.0,"isAuto":0}
C
:0}
:
C{"value":0.0,"setpoint":0.0,"isAuto":0}
C
":0}
Instead sending only one type of message, say "D", the output is correct:
D{"value":0.0,"setpoint":0.0,"isAuto":0}
D
D{"value":0.0,"setpoint":0.0,"isAuto":0}
D
D{"value":0.0,"setpoint":0.0,"isAuto":0}
D
D{"value":0.0,"setpoint":0.0,"isAuto":0}
D
D{"value":0.0,"setpoint":0.0,"isAuto":0}
D
There is something obviously wrong in my script?