osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

OT?: break up an incoming data stream into fixed length lines? in bash?


At Sun, 8 Apr 2018 15:14:56 +0200 "Ubuntu user technical support,  not for general discussions" <ubuntu-users at lists.ubuntu.com> wrote:

> 
> On 06.04.2018 20:04, 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.
> > 
> > TIA,
> > 
> > Dave
> > 
> 
> Hello Dave
> 
> I use following bash script to read data from a serial port (USB <-> RS232):
> 
> #!/bin/bash
> # Receive remote weather data from USB-WDE1
> 
> # Set the correct interface parameters
> stty < /dev/ttyUSB0 9600 -brkint -opost -onlcr -echo
> 
> # Loop forever to read data from USB-WDE1
> socat /dev/ttyUSB0,b9600 STDOUT | \
> while read line
> do
>       if [[ "${line%%;*}" == '$1' ]] ; then
> 	#echo $line
>           # format data
>           tmp=`echo "${line#?1;1;}" | tr ';,' ':.'`
>           data=`echo "N${tmp%%0}" | sed 's/::/:U:/g' | sed 's/::/:U:/g'`
>           data=${data%%:}
> 	echo `date +%R.%S:` $line
>       fi
> done
> 
> It is a long time since I implemented this with the help of examples from various web sites. Therefore, I don't remember how the data formating is done in detail.
> Maybe this example gets you running.

This code clearly assumes data coming in line-based records (eg CRLF between 
"records" -- "while read line").  If the data stream is NOT line-based, this 
code won't work.  *Sometimes* sensor instruments send data with newlines, 
sometimes not... When not, things can become interesting to code for...  Even 
more fun when the data steam uses non-printable ASCII characters or data 
bytes.

> 
> Thomas
> 

-- 
Robert Heller             -- 978-544-6933
Deepwoods Software        -- Custom Software Services
http://www.deepsoft.com/  -- Linux Administration Services
heller at deepsoft.com       -- Webhosting Services