On 12/01/2013 08:28 PM, richard kappler wrote:
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.
* What is the point of the 'sensors' dict? (also, you don't need to initialise
it as an enmpty dict)
* If you know about regexps or another matching utility, use that to decode the
input line into (key,value) pairs. This is even easier if input has a strict
format. Would tell us?
* About wrong input (incomplete data), avoid try/except, except if ever most
cases are ok (it is very costly in case of exception). Anyway, you need to
decode input, so use that to check for errors/missing stuff.
If you used a matching tool, its absence of result would directly tell about
wrong input (provided your pattern is correct! ;-) As of now, just place checks
in your decoding sequence. Possible checks places, at first sight, marked below:
line = arduino.readline().strip()
line = line.lstrip('{').rstrip('}').strip()
# check line not empty
# (actually big enough for at least {} + one key:val entry)
d = {}
# first get items in separate var and check how many:
for item in line.split(','):
item = item.strip() # not needed, for you strip key/val again
later
# first get tuple from split() and check its size is 2
key, value = item.split(':')
key = key.strip()
value = value.strip() # not needed, for int() itself strips
# maybe one try/except here for checking conversion to int:
# (else, you need to check yourself it is an int numeral)
d[key]=int(value)
Each check failure is a sign of wrong input. What do you need to do, then?
Abandon the whole round?
Denis
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor