[vox-tech] bash question - how to read specifed amnt of data
from a file
Mark K. Kim
vox-tech@lists.lugod.org
Wed, 8 Oct 2003 23:07:57 -0700 (PDT)
Due to data buffering issues, you should use a carriage return followed by
a newline (ASCII 13 and 10, respectively) instead of a pipe to separate
the data. (only one or the other might work, but both will surely
work...)
After that, you can just read it with the read command. Like this:
cat /dev/ttyS0 | ./myscript
and myscript can be something like:
#!/bin/bash
while true
do
read a
date=`date`
echo "$date: $a"
done
Ryan's idea using cut won't work (sorry Ryan!) because the data is
streamed.
If, for some reason, you can't change the pipe to <CR><NL>, then you need
to turn off buffering and parse the data but... you don't wanna go there.
Also, if you wanna read the data every 10 seconds, you might wanna send
the data every 10 seconds, too, since the serial port is buffered.
Otherwise you gotta flush the data before reading, and BSII might freeze
trying to send the data, and... yeah, you don't wanna go there either.
-Mark
On Wed, 8 Oct 2003, dylan wrote:
> hi-
>
> been playing around with using a basic stamp II to collect environmental
> data with very minimal setup (CdS cell and capacitor form an RC network, RC
> constant is measured by BS II)
> [http://surfdavis.no-ip.com/~dylan/light_level.p2.png]
>
> the BSII sends its data to /dev/ttyS0, and it is read in with the
> command'cat /dev/ttyS0 > tmp.light &' . data is parsed by a simple awk
> script and then sent to ploticus for a nice plot. the plot is updated by
> running a shell script that runs the awk sript and then ploticus.
>
> this works, but there is no way to associate a time with the data values. i
> was thinking that if there was some way to have a bash script read from
> /dev/ttyS0 every 10 seconds or so, save the value to a text file along with
> a date stamp it would be pretty neat.
>
> the output from the BSII looks something like this:
> light_level|light_level|light_level and so on (the delimiter is a pipe)
>
> now to the question: does anyone know how to make bash read from a file
> (i.e. the serial port) from one pipe until another pipe, and then save the
> value in between to a text file?
>
> i know this is asking a lot, but any pointers would be great!
>
> thanks a lot!
>
> dylan
>
> _______________________________________________
> vox-tech mailing list
> vox-tech@lists.lugod.org
> http://lists.lugod.org/mailman/listinfo/vox-tech
>
--
Mark K. Kim
http://www.cbreak.org/
PGP key available on the website
PGP key fingerprint: 7324 BACA 53AD E504 A76E 5167 6822 94F0 F298 5DCE