"Wolfgang Maier" <wolfgang.ma...@biologie.uni-freiburg.de> wrote in message news:loom.20131201t230651-...@post.gmane.org... > richard kappler <richkappler <at> gmail.com> writes: > >> >> 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 >> >> 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. >> > There should be no sync issue here. The readline method should read from > the > serial port until it reaches an EOL character, then return the whole line > (i.e., your sleep(1) should be removed since readline() already waits for > input). > From what you're describing, the real issue seems to be on the side of the > sender. Are you sure, it terminates each line with \n as it should? Where > is > that code coming from? > Best, > Wolfgang > > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > I also suspect sleep doesn't work. Two better options would be: 1. read/loop until line terminator, 2. Use serial signals, i.e., RTS/DTS if possible.
Paul Simon _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor