Trace a binary stream from a device file
You don't need to tail -f
a tty. If it's sending you EOF, or, if it is line-buffering, then you need to configure it.
stty -F/dev/ttyAPP2 raw
Now you can...
cat /dev/ttyAPP2
...as needed...
You might try...
</dev/ttyAPP2 \
dd bs=16 conv=sync | od -vtx1
...which will sync out every successful read()
from your device into 16-byte, null-padded blocks, and so will write line-buffered output (such as to your terminal) in real-time regardless of throughput, though any trailing nulls might distort your stream.
With GNU stdbuf
and a dynamically linked od
:
stdbuf -o0 od -vtx1 </dev/ttyAPP2
...would write output in real-time regardless.
You might also buffer to a temp file like...
f=$(mktemp)
exec 3<>"$f"; rm -- "$f"
while dd >&3 of=/dev/fd/1 bs=4k count=1
[ -s /dev/fd/3 ]
do od -An -vtx1 /dev/fd/3
echo
done </dev/ttyAPP2 2>/dev/null
...which, though likely not nearly as efficient as the other recommendations, might be worth considering if you wanted to delimit reads from your device by EOF. I find the technique useful sometimes when working with ttys, anyway.
It is also possible to force hexdump
to print out less bytes by using the custom print format. The example below will print every time there are 4 bytes available:
hexdump -e '4/1 "%02x " "\n"' < /dev/ttyAPP2
if multiline (9 lines) solution is ok, check below hextail.sh
script:
#!/usr/bin/env bash
#par1==buffsize, par2=xxd-columns-width, par3=sleepVal; defaults: 256 16 0
size=${1:-256}; cols=${2:-16}; cnt=0; hbuff=$(eval printf '00%.0s' {1..$size})
while true; do
hbuff=${hbuff:2}$(dd bs=1 count=1 2>/dev/null | xxd -p) #shiftLeft, add 1b
printf '\033[;H'; xxd -r -p <<<$hbuff | xxd -g1 -c$cols #cursor gotoxy 1,1
echo total bytes: $((++cnt)) ; [ -z $3 ] || sleep $3 #...and buff show.
done
it implements binary tail by storing its hex representation in bash string variable. First byte in buff deleted on appending new byte at the end.
example test commands:
1) cat /dev/urandom | ./hextail.sh 1024 32 0.2 ...with 200ms sleep
2) ./hextail.sh < /dev/ttyAPP2