Parsing a serial stream too slowly
Jon Clements
joncle at googlemail.com
Mon Jan 23 17:00:25 EST 2012
On Jan 23, 9:48 pm, "M.Pekala" <mcdpek... at gmail.com> wrote:
> Hello, I am having some trouble with a serial stream on a project I am
> working on. I have an external board that is attached to a set of
> sensors. The board polls the sensors, filters them, formats the
> values, and sends the formatted values over a serial bus. The serial
> stream comes out like $A1234$$B-10$$C987$, where "$A.*$" is a sensor
> value, "$B.*$" is a sensor value, "$C.*$" is a sensor value, ect...
>
> When one sensor is running my python script grabs the data just fine,
> removes the formatting, and throws it into a text control box. However
> when 3 or more sensors are running, I get output like the following:
>
> Sensor 1: 373
> Sensor 2: 112$$M-160$G373
> Sensor 3: 763$$A892$
>
> I am fairly certain this means that my code is running too slow to
> catch all the '$' markers. Below is the snippet of code I believe is
> the cause of this problem...
>
> def OnSerialRead(self, event):
> text = event.data
> self.sensorabuffer = self.sensorabuffer + text
> self.sensorbbuffer = self.sensorbbuffer + text
> self.sensorcbuffer = self.sensorcbuffer + text
>
> if sensoraenable:
> sensorresult = re.search(r'\$A.*\$.*', self.sensorabuffer )
> if sensorresult:
> s = sensorresult.group(0)
> s = s[2:-1]
> if self.sensor_enable_chkbox.GetValue():
> self.SensorAValue = s
> self.sensorabuffer = ''
>
> if sensorbenable:
> sensorresult = re.search(r'\$A.*\$.*', self.sensorbenable)
> if sensorresult:
> s = sensorresult.group(0)
> s = s[2:-1]
> if self.sensor_enable_chkbox.GetValue():
> self.SensorBValue = s
> self.sensorbenable= ''
>
> if sensorcenable:
> sensorresult = re.search(r'\$A.*\$.*', self.sensorcenable)
> if sensorresult:
> s = sensorresult.group(0)
> s = s[2:-1]
> if self.sensor_enable_chkbox.GetValue():
> self.SensorCValue = s
> self.sensorcenable= ''
>
> self.DisplaySensorReadings()
>
> I think that regex is too slow for this operation, but I'm uncertain
> of another method in python that could be faster. A little help would
> be appreciated.
You sure that's your code? Your re.search()'s are all the same.
More information about the Python-list
mailing list