Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19300

Re: Parsing a serial stream too slowly

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!feeder3.cambriumusenet.nl!feed.tweaknews.nl!194.134.4.91.MISMATCH!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <nicholas.dokos@hp.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.047
X-Spam-Evidence '*H*': 0.91; '*S*': 0.00; 'value,': 0.04; 'removes': 0.07; 'snippet': 0.07; 'python': 0.08; 'operation,': 0.09; 'variables.': 0.09; 'output': 0.10; 'def': 0.13; 'cc:addr:python- list': 0.15; 'already.': 0.16; 'bus.': 0.16; 'code?': 0.16; 'event):': 0.16; 'nick': 0.16; 'polls': 0.16; 'throws': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'modified': 0.18; 'jan': 0.19; 'pointed': 0.21; 'header:In-Reply-To:1': 0.22; 'fine,': 0.23; 'string': 0.24; 'code': 0.25; 'right.': 0.25; "i'm": 0.27; 'somebody': 0.27; 'script': 0.28; 'example': 0.28; 'sends': 0.28; 'cc:addr:python.org': 0.29; 'second': 0.29; 'fairly': 0.30; 'closer': 0.30; 'differently': 0.30; 'values': 0.32; 'initial': 0.32; 'there': 0.33; 'match': 0.33; 'too': 0.34; 'rather': 0.34; 'certain': 0.34; 'searches': 0.34; 'probably': 0.35; 'running': 0.35; 'trouble': 0.35; 'project': 0.35; 'external': 0.35; 'skip:" 20': 0.35; 'cc:2**1': 0.36; 'variables': 0.36; 'skip:" 10': 0.36; 'but': 0.37; 'hello,': 0.37; 'another': 0.37; 'could': 0.37; 'some': 0.38; 'problems': 0.38; 'third': 0.38; 'think': 0.38; 'should': 0.38; 'e.g.': 0.38; 'same.': 0.38; 'why': 0.39; 'appreciated.': 0.39; 'help': 0.39; 'subject:: ': 0.39; 'data': 0.40; 'more': 0.61; 'your': 0.61; 'header:Received:6': 0.61; 'below': 0.63; 'skip:1 10': 0.63; 'believe': 0.65; 'cause': 0.66; 'serial': 0.71; 'reply-to:no real name:2**0': 0.72; 'stream': 0.77; 'header:Reply-to:1': 0.84; 'lose': 0.84; 'consumed': 0.84; 'grabs': 0.84; 'jon': 0.84; 'problem...': 0.84; 'running,': 0.84; 'subject:stream': 0.84; 'want:': 0.84; 'uncertain': 0.91; 'presumably': 0.93
To "M.Pekala" <mcdpekala@gmail.com>
From Nick Dokos <nicholas.dokos@hp.com>
Subject Re: Parsing a serial stream too slowly
In-Reply-To Message from "M.Pekala" <mcdpekala@gmail.com> of "Mon\, 23 Jan 2012 14\:03\:25 PST." <b16c3dd9-e7c3-49ba-906c-c1c6999252b0@t7g2000vbg.googlegroups.com>
References <d250b98e-6409-474b-ba56-146e5b490e01@l1g2000vbc.googlegroups.com> <760df574-f13d-46e5-93e7-6cc51de5510a@s18g2000vby.googlegroups.com> <b16c3dd9-e7c3-49ba-906c-c1c6999252b0@t7g2000vbg.googlegroups.com>
Organization HPCS
X-Mailer MH-E 8.3.1; nmh 1.3; GNU Emacs 24.0.92
MIME-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding quoted-printable
Date Mon, 23 Jan 2012 18:28:24 -0500
Sender nicholas.dokos@hp.com
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To nicholas.dokos@hp.com
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4998.1327361819.27778.python-list@python.org> (permalink)
Lines 138
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1327361820 news.xs4all.nl 6890 [2001:888:2000:d::a6]:56988
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19300

Show key headers only | View raw


M.Pekala <mcdpekala@gmail.com> wrote:

> On Jan 23, 5:00 pm, Jon Clements <jon...@googlemail.com> wrote:
> > On Jan 23, 9:48 pm, "M.Pekala" <mcdpek...@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.
> 
> Whoops you are right. the search for the second should be re.search(r'\
> $B.*\$.*', self.sensorbbuffer ), for the third re.search(r'\$C.*\$.*',
> self.sensorcbuffer )
> 

The regex is probably still wrong: r'\$A.*\$.*' will e.g. match all of
your initial example "$A1234$$B-10$$C987$", so s will lose the initial
and final '$' and end up as "1234$$B-10$$C987" - I doubt that's what you
want:

>>> sensor_result = "$A123$$B456$$C789$$A456$"
>>> r = re.search(r'\$A.*\$.*', sensor_result)
>>> s = r.group(0)
>>> s = s[2:-1]
>>> s
'123$$B456$$C789$$A456'

Is this perhaps closer to what you want?

>>> r = re.search(r'\$A[^$]+\$', sensor_result)
>>> r.group(0)
'$A123$'
>>> 

I'm sure there are more problems too - e.g. why are there three buffers?
If they all start empty and get modified the same way, they will all
contain the same string - are they modified differently in the part of
the program you have not shown? They will presumably need to be trimmed
appropriately to indicate which part has been consumed already. And, as
somebody pointed out already, the searches should probably be against
the *buffer* variables rather than the *enable* variables.

Nick

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Parsing a serial stream too slowly "M.Pekala" <mcdpekala@gmail.com> - 2012-01-23 13:48 -0800
  Re: Parsing a serial stream too slowly Jon Clements <joncle@googlemail.com> - 2012-01-23 14:00 -0800
    Re: Parsing a serial stream too slowly "M.Pekala" <mcdpekala@gmail.com> - 2012-01-23 14:03 -0800
      Re: Parsing a serial stream too slowly Nick Dokos <nicholas.dokos@hp.com> - 2012-01-23 18:28 -0500
  Re: Parsing a serial stream too slowly Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-01-24 00:13 +0100
    Re: Parsing a serial stream too slowly Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-01-24 14:04 +0100
  Re: Parsing a serial stream too slowly Cameron Simpson <cs@zip.com.au> - 2012-01-24 10:49 +1100
    Re: Parsing a serial stream too slowly "M.Pekala" <mcdpekala@gmail.com> - 2012-01-23 17:07 -0800
    Re: Parsing a serial stream too slowly Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-01-24 05:08 +0000
      Re: Parsing a serial stream too slowly Cameron Simpson <cs@zip.com.au> - 2012-01-24 19:23 +1100
  Re: Parsing a serial stream too slowly Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-01-24 11:15 +0100

csiph-web