[Tutor] truncated dictionary return
richard kappler
richkappler at gmail.com
Sun Dec 1 20:28:26 CET 2013
I have a script that reads sensor values gathered by an Arduino board from
serial as a dictionary, said values to later be used in the AI for Nav &
Control. Here's the script:
#!/usr/bin/python
def sensorRead():
import serial
from time import sleep
sensors = {}
sensors = dict.fromkeys('Sonar1 Sonar2 Sonar3 Sonar4 Dewpoint
Temperature Humidity Light'.split())
arduino = serial.Serial('/dev/ttyACM0', 9600)
sleep(1)
line = arduino.readline().strip()
line = line.lstrip('{').rstrip('}').strip()
d = {}
for item in line.split(','):
item = item.strip()
key, value = item.split(':')
key = key.strip()
value = value.strip()
d[key]=int(value)
return d
I hope that comes through okay, I copied it from the text file so
indentation and such should be fine, if not let me know.
The script works great with one exception. I understand the problem, I'm
just not sure how to address it. The problem is:
The Arduino runs on a constant loop, it reads each sensor, sends the key
and the value to the serial bus in format for python to read it as a
dictionary, lather, rinse, repeat.
Python querries the bus when told. Usually the python script gets the full
dictionary (all 8 values with keys, brackets etc) but sometimes it doesn't.
Sometimes it only gets the last few values, sometimes it gets nothing or
misses a bracket and throws an error. This makes sense. They are not in
sync.
What I need to figure out how to do is have the python script wait until
the next round of values as signified by the opening bracket "{" or check
that it has all 8 values and if not retry or.... something.
Would this be an if/else? try? exception?
I've not yet delved into any of these in my quest to learn python except
if/else and that doesn't feel right for this, so I'm at a loss as to how to
proceed.
regards, Richard
--
*Mater tua criceta fuit, et pater tuo redoluit bacarum sambucus*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131201/2f1469a7/attachment.html>
More information about the Tutor
mailing list