OT?: break up an incoming data stream into fixed length lines? in bash?
Peter Silva
peter at bsqt.homeip.net
Sun Apr 8 14:25:24 UTC 2018
This is pretty straightforward stuff. What is needed is a complete specification
of the protocol that is used over the serial line. Then you need to implement
the protocol. This sounds like someone's homework assignment.
stuff I've written to do this sort of thing...
Here's some code that implements a GUI to talk to a Z-80 over a serial link.
It's in python2:
https://github.com/petersilva/Bi-Directional-Machining-Facility
Python is likely a very good approach, but you can use any language you want.
This other tool is in debian, drobo-utils.sf.net. This is another
python app that is
communicating with an ARM processor over SCSI protocol inside a USB
link. In this case, the usb is transparent because other linux drivers
make it present a SCSI application driver (work with /dev/sdX). In
any event, this stuff is fairly straightforward if you know the
protocol you need to use.
If you don't have a protocol specification, then it is orders of
magnitude more work, because you end up reverse engineering, and the
debugging sucks.
On Sun, Apr 8, 2018 at 9:38 AM, Robert Heller <heller at deepsoft.com> wrote:
> 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
>
>
> --
> ubuntu-users mailing list
> ubuntu-users at lists.ubuntu.com
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
More information about the ubuntu-users
mailing list