I'm trying to read serial input(/dev/tty*) character by character. I need this to parse the data following the corresponding protocol. My current method is as follows:
while read -rs -n 1 c; do
echo Read a char:
echo ${c}
done < "${SERIAL_PORT}"
This works, except for the fact that the data is binary and some bytes are \x00, which is not allowed as a character in a variable. My aim is to first convert the byte into a hex string(two characters) which I will then store in the variable. This is where I get stuck. What is a good way to do this? I would prefer to stay posix compliant.
od
does this. Note that you're already not POSIX compliant since you're using other options than just-r
toread
.pyserial
via Python script. Or at a very leastscreen
application. Those tools are designed exactly for the job. Shell scripts - not so suitable for low level stuff like thatc=$(dd bs=1 count=1 < ... | od ... | awk ...)
but it might take so long that some data would be missed..