Skip to content

Synchronisation issue with ELM327 #226

@ddowling

Description

@ddowling

I was having problems getting the python-obd library to reliably sync with a cheap ELM327 clone. I believe the problem is due to some code added to elm327.py to support low power mode.
Specifically the line line elm327.py:548

      # end on chevron (ELM prompt character) or an 'OK' which
      # indicates we are entering low power state
      if self.ELM_PROMPT in buffer or self.ELM_LP_ACTIVE in buffer:
          break

During start up the following exchange happens with the ELM327:

[obd.elm327] Initializing ELM327: PORT=/dev/ttyUSB0 BAUD=auto PROTOCOL=auto
[obd.elm327] Response from baud 38400: b'?\r\r>'
[obd.elm327] Choosing baud 38400
[obd.elm327] write: b'ATZ\r'
[obd.elm327] wait: 1 seconds
[obd.elm327] read: b'\xfc\r\rELM327 v1.5\r\r>'
[obd.elm327] write: b'ATE0\r'
[obd.elm327] read: b'ATE0\rOK'
[obd.elm327] write: b'ATH1\r'
[obd.elm327] read: b'>'
[obd.elm327] closing port
[obd.elm327] write: b'ATZ\r'
[obd.elm327] ATH1 did not return 'OK', or echoing is still ON

Note how after ATE0 is sent to the ELM327 we receive back the echoed command and then an OK. This OK matches the ELM_LP_ACTIVE clause and the command returns before reading the next line containing the chevron prompt. When the ATH1 command is sent to the ELM327 the chevron prompt is then read and the code assumes this command has failed.

The fix is to always attempt to read up to the chevron prompt and to not bail out early if we read OK in the response stream.
If I change line 548 to the following the adapter connects reliably everytime:

    if self.ELM_PROMPT in buffer:
                break
[obd.elm327] Initializing ELM327: PORT=/dev/ttyUSB0 BAUD=auto PROTOCOL=auto
[obd.elm327] Response from baud 38400: b'\x7f\x7f\r?\r\r>'
[obd.elm327] Choosing baud 38400
[obd.elm327] write: b'ATZ\r'
[obd.elm327] wait: 1 seconds
[obd.elm327] read: b'ATZ\r\xfc\r\rELM327 v1.5\r\r>'
[obd.elm327] write: b'ATE0\r'
[obd.elm327] read: b'ATE0\rOK\r\r>'
[obd.elm327] write: b'ATH1\r'
[obd.elm327] read: b'OK\r\r>'
[obd.elm327] write: b'ATL0\r'
[obd.elm327] read: b'OK\r\r>'
[obd.elm327] write: b'AT RV\r'
[obd.elm327] read: b'0.0V\r\r>'
[obd.elm327] OBD2 socket disconnected

I am not sure how my change will impact the low power mode as I am not using this but I am happy to test out an alternate fix if required.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions