Reputation: 45
I am trying to write a Bash script to receive a file of known number of Bytes (we are talking about 1MB) of binary data from a serial device connected to my embedded device. These bytes must then be saved to a file for later ops.
I've tried something like (stty raw; cat > blob.bin) < /dev/ttyS0
but I would like cat
to stop when it reaches the number of Bytes I am expecting, as the script needs to progress on other functions when the file is complete.
The data flow will be started from the external device and it will run continuously until the the end of the binary file from the external device.
Working on Linux Buster, unfortunately I cannot use Python or other programming languages.
Thanks!
Upvotes: 0
Views: 2123
Reputation: 45
Thanks to the comment of @meuh, I was able to write a working script using dd
:
dd ibs=1 count=$PLBYTE iflag=count_bytes if=/dev/ttyS0 of=/.../dump.bin
using dd
operands count and iflag, (counting the received bytes and reading 1 byte/block) with $PLBYTE
the number of expected bytes.
The script now works as expected.
Make sure to set stty in noncanonical mode (-icanon) otherwise data over 4096 bytes will be truncated and dd
will not receive the expected amount of bytes.
Upvotes: 1