[Tutor] making use of input
Alan Gauld
alan.gauld at btinternet.com
Mon Nov 5 02:39:31 CET 2012
On 04/11/12 20:52, richard kappler wrote:
> The serial out from the Arduino sends in some form (depending on how I
> format it in the Arduino code) Sonar1, Sonar2, Sonar3, Sonar4,
> Temperature, Humidity, Dewpoint, and Light.
>
> I can get these into Python in a continuous read by doing:
>
> import serial
> arduino = serial.Serial('/dev/ttyACM0', 9600)
> while 1:
> arduino.readline()
In general its a bad idea to read a byte stream from the serial port
using readline().
readline() is looking for an end of line pattern and the nature of
serial comms is such that its a risky strategy to assume that bytes
don't get corrupted or even to make assumptions about the format of the
data coming in. Using read() with or without a length value is usually
safer. Which you opt for will depend on how continuous your data stream
is. If its bursty a simple read() is probably better. But if you can
guarantee the newlines will be there and corruption will be minimal
(slow speed, short distance) then readline will work.
However, if it is sporadic data then you might be better off looking at
select() which will wait for data to arrive before waking up and
processing it. Slightly more complex to use but there is a Howto on the
python site.
> This little bit of code gives me data in the form of 'Sensor1:120\r\n'
> one line for each sensor in a continuous loop.
> I can also get it to python in the following form {Sensor1:120,
> Sensor2:89, etc...}
Who is producing the data?
I'm assuming it is you from your description?
If so you can make it look like anything you choose.
It is just a string of bytes after all.
Your job at the Python end is to interpret(parse) that stream into
meaningful data.
> Right. Like a dictionary. but a continuous read, in other words, python
> prints a new dictionary with the same keys but different data each time
> the Arduino loops which is what Arduino's do.
I thought Arduinos were rather more intelligent than simple loops. Can't
they issue interrupts too? But I've never used one so am not sure.
> I got only part of it, eg something like {,Sonar4:120,temperature:71,
> etc on to the end of the sensor list} so python just captured whatever
> was scrolling through serial at the moment
You lost me there. Can you show real data?
> What I NEED to do is have each parameter incoming to python through
> serial be assigned to a name and be available within the python program
Why?
Its much easier to add it to a collection and access it
via indexes or dictionary keys. It saves you having to
write self modifying code to cater for when names you've
never seen before arrive...
> The use of dictionary form was kind of a whim, and within limits I have
> not yet found, I can format the output pretty much any way I'd like from
> the Arduino, including just sending the numbers.
Its just a data stream, of course you can format it any way ypu like.
You could even send the raw binary values. Or you could create XML
structures, or JSON, or csv, or any one of the other formats that python
has module support for. Or you can invent your own format and write a
bespoke parser(but I wouldn't recommend it!)
> I'm pretty happy with the accomplishments of the day, but am at a bit of
> a loss as to how to proceed next. I hope that all made sense. I might be
> suffering a bit from "forest for trees" syndrome.
Next is to parse out the data from the stream and assign it to a data
structure for processing. Those decisions will be some of the most
important in your project as they will determine how easy the rest of
the processing will be to write! So consider how you would like it to
look to make your future code easier. Some extra effort in parsing will
make the rest of the job much easier!
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list