Parsing data from pyserial, an (inefficient) solution

Lone Wolf lone_wolf at ureach.com
Sun Dec 3 21:19:00 EST 2006


I want to thank everybody who tried to help me, and also to post
my solution, even though I don’t think it is a very good one.

Many of you correctly guessed that there was an “\r” included
with the packet from the CUMcam, and you were correct. The
actual format of the packet is:     M xxx xxx xxx xxx xxx xxx
xxx xxx\r. Unfortunately, splitting the packet using \r wouldn’t
help because the format of the data stream that I get with the
components variable (after I split the reading file according to
“M”) generally doesn’t include a complete packet at first. For
example, I get:  [xxx xxx xxx\r, yyy yyy yyy yyy yyy yyy yyy
yyy/r,  zzz zzz zzz]          Therefore, data from before the
first \r  (which I have shown as xxx) generally is incomplete
and I need to go on to the second packet. 

Also, for those of you who suggested some kind of delay before
reading the serial port, you were right. The first packet from
the CMUcam is always a null. 

Anyway, here is my code:

################################################################

# This program reads a serial port hookup up to a CMUcam version
1.
# It tracks the middle of a green object and provides a
confidence estimate

import serial

ser=serial.Serial('com1',baudrate=115200, bytesize=8,
parity='N', stopbits=1,xonxoff=0, timeout=1)

ser.write("TC 016 240 100 240 016 240\r\n")  #This line orders
the CMUcam to track green
reading = ser.read(40)  # CMUcam's first data packet is null, so
this line gets it out of the way

for i in range(0,100,1):
	reading = ser.read(40)
	components = reading.split("M")
	components = components[1]
	if len(components) > 23:  # If shorter than 24 it won't have
enough data for a full packet
		subcomponents = components.split()
		mx = int(subcomponents[0])
		my = int(subcomponents[1])
		confidence = int(subcomponents[7])
		print mx, my, confidence 
ser.close


The really sad thing is that I get a perfectly constructed
packet from the reading variable, and that gets butchered when I
try to slice it up to pick out individual elements. Since
pyserial doesn’t do anything to rearrange the data, then the
CMUcam must do the heavy lifting of extracting a perfect packet
from the data stream. It’s a real shame I couldn’t use it
because the program would be more efficient. FWIW, this code
will analyze 2-3 frames per second on my computer, which is
enough for my purposes.

In case you couldn’t tell from the questions/code, I am a total
beginner, and I really appreciate this list. All I needed was a
hand, not a handout. Wolves are willing to hunt for their
supper.


________________________________________________
Get your own "800" number
Voicemail, fax, email, and a lot more
http://www.ureach.com/reg/tag



More information about the Python-list mailing list