OT?: break up an incoming data stream into fixed length lines? in bash?
Karl Auer
kauer at biplane.com.au
Sun Apr 8 15:04:51 UTC 2018
On Fri, 2018-04-06 at 11:04 -0700, Dave Stevens wrote:
> I'm using U16.04 to read data from a sensor. The incoming serial data
> stream has no newlines, just continuous bytes. In the datastream the
> letters B and M occur together at the start of every 24 byte
> subsequence. I'm reading the data using getserial with appropriate
> speed and parity parms. I don't see a simple way to break it up into
> lines with each line having 24 bytes. Does anyone care to suggest a
> method? I'd use a bash function if there is one.
Sounds like you need a little state machine.
You can do it in bash. Read the doco for the "read" function; it has
options for setting the number of characters to read. By default it
reads a line; to read a single char you may have to change some tty
settings - a bit of googling will find you what you need.
Your state machine would look something like:
clear buffer
state = waiting_for_B
while not EOF
get char
if state is:
waiting_for_B
if char == B
append char to buffer
state = waiting_for_M
waiting_for_M
if char == M
append char to buffer
state = collecting
else
clear buffer
state = waiting_for_B
collecting
save char in buffer
if buffer length > 23
output buffer, newline
clear buffer
state = waiting_for_B
The above discards any data not inside a 24-byte sequence starting with
"BM". You can use the same state machine on data from a file.
Regards, K.
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Karl Auer (kauer at biplane.com.au)
http://www.biplane.com.au/kauer
http://twitter.com/kauer389
GPG fingerprint: A0CD 28F0 10BE FC21 C57C 67C1 19A6 83A4 9B0B 1D75
Old fingerprint: A52E F6B9 708B 51C4 85E6 1634 0571 ADF9 3C1C 6A3A
More information about the ubuntu-users
mailing list