Skip to content

Commit dbfd379

Browse files
interfectalistair23
authored andcommitted
Only treat "OK" as a response terminator when appropriate
Right now __read() will stop reading at "OK" even if a ">" is still coming. This can cause the ">" to be seen as the response to the next command, which confuses the initialization sequence, since the initialization sequence expects a very specific set of responses to its commands. This changes __read() so that by default it only treats ">" as the response terminator. When we issue the "ATLP" command to enter low-power mode, we will use "OK" as the response terminator instead, since that's the only time we don't expect to see a prompt. This should fix #226 and should also fix #227.
1 parent a08424d commit dbfd379

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

obd/elm327.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ class ELM327:
5555
ecus()
5656
"""
5757

58+
# chevron (ELM prompt character)
5859
ELM_PROMPT = b'>'
60+
# an 'OK' which indicates we are entering low power state
5961
ELM_LP_ACTIVE = b'OK'
6062

6163
_SUPPORTED_PROTOCOLS = {
@@ -391,7 +393,7 @@ def low_power(self):
391393
logger.info("cannot enter low power when unconnected")
392394
return None
393395

394-
lines = self.__send(b"ATLP", delay=1)
396+
lines = self.__send(b"ATLP", delay=1, end_marker=self.ELM_LP_ACTIVE)
395397

396398
if 'OK' in lines:
397399
logger.debug("Successfully entered low power mode")
@@ -466,13 +468,14 @@ def send_and_parse(self, cmd):
466468
messages = self.__protocol(lines)
467469
return messages
468470

469-
def __send(self, cmd, delay=None):
471+
def __send(self, cmd, delay=None, end_marker=ELM_PROMPT):
470472
"""
471473
unprotected send() function
472474
473475
will __write() the given string, no questions asked.
474476
returns result of __read() (a list of line strings)
475-
after an optional delay.
477+
after an optional delay, until the end marker (by
478+
default, the prompt) is seen
476479
"""
477480
self.__write(cmd)
478481

@@ -482,13 +485,13 @@ def __send(self, cmd, delay=None):
482485
time.sleep(delay)
483486
delayed += delay
484487

485-
r = self.__read()
488+
r = self.__read(end_marker=end_marker)
486489
while delayed < 1.0 and len(r) <= 0:
487490
d = 0.1
488491
logger.debug("no response; wait: %f seconds" % d)
489492
time.sleep(d)
490493
delayed += d
491-
r = self.__read()
494+
r = self.__read(end_marker=end_marker)
492495
return r
493496

494497
def __write(self, cmd):
@@ -512,11 +515,12 @@ def __write(self, cmd):
512515
else:
513516
logger.info("cannot perform __write() when unconnected")
514517

515-
def __read(self):
518+
def __read(self, end_marker=ELM_PROMPT):
516519
"""
517520
"low-level" read function
518521
519-
accumulates characters until the prompt character is seen
522+
accumulates characters until the end marker (by
523+
default, the prompt character) is seen
520524
returns a list of [/r/n] delimited strings
521525
"""
522526
if not self.__port:
@@ -543,9 +547,8 @@ def __read(self):
543547

544548
buffer.extend(data)
545549

546-
# end on chevron (ELM prompt character) or an 'OK' which
547-
# indicates we are entering low power state
548-
if self.ELM_PROMPT in buffer or self.ELM_LP_ACTIVE in buffer:
550+
# end on specified end-marker sequence
551+
if end_marker in buffer:
549552
break
550553

551554
# log, and remove the "bytearray( ... )" part

0 commit comments

Comments
 (0)