I have a scale that continuously send data via serial port, 2 times per second. The only reliable way on reading this data happened to be cat
command. The following works:
cat /dev/ttyUSB0
But the problem with cat
is that, as it doesn't receive an EOF
, it continues retrieving data. I've also tried head
, read
and tail
.
head -1 /dev/ttyUSB0 | strings
works 'almost' everytime, but now and then shows old data, and only re-running cat command fixes it (?). The string after pipe retrieves only printable data.
read line < /dev/ttyUSB0 | echo $line
now and then retrieves data, but most of the time only shows an empty line.
tail -1 < /dev/ttyUSB0
just freezes, waiting for EOF
, maybe?
My issue is that i need to write a bash
script that can be called and "read" /dev/ttyUSB0
and retrieve data. I'm going to use head
. But I'm wondering, as cat
never fails, if there's a way to capture only one line of cat
output and then stop it.
Note: the port configuration is 9600 baud, 1 start bit, 8 data bits, no parity, 2 stop bits. To set the port properly this is the command:
sudo stty -F /dev/ttyUSB0 9600 -parity cs8 cstopb
dd
? It might work (with suitable options).head
command may be getting slightly old data due to a buffer, and you are reading the top of it. When you triedtail
, it was with other arguments. Try repeating thehead
command options withtail
cat
command withstdbuf -i 0 -o 0
, i.e.stdbuf -i 0 -o 0 head -1 /dev/ttyUSB0 | strings
.