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?


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.

Thomas