rzetterberg / elmobd

A Go library for talking to cars over OBD-II
MIT License
220 stars 38 forks source link

"UNABLE TO CONNECT" is given when same command is run twice #30

Open rzetterberg opened 4 years ago

rzetterberg commented 4 years ago

Description

This issue is created from the PR #29, to separate the investigation of the root cause of the problem that the PR is trying to solve.

The following code is used to build an executable:

package main

import (
    "flag"
    "fmt"
    "github.com/rzetterberg/elmobd"
)

func main() {
    serialPath := flag.String(
        "serial",
        "/dev/ttyUSB0",
        "Path to the serial device to use",
    )

    flag.Parse()

    dev, err := elmobd.NewDevice(*serialPath, false)

    if err != nil {
        fmt.Println("Failed to create new device", err)
        return
    }

    rpm, err := dev.RunOBDCommand(elmobd.NewEngineRPM())

    if err != nil {
        fmt.Println("Failed to get rpm", err)
        return
    }

    fmt.Printf("Engine spins at %s RPMs\n", rpm.ValueAsLit())
}

The following steps is performed:

  1. ELM327-device is plugged into the computer
  2. Car ignition is turned on (engine is not started)
  3. ELM327-device is plugged into the cars OBD2-plug
  4. The executable is run and outputs:
    Engine spins at 0 RPMs
  5. The executable is run again directly and outputs:
    Failed to get rpm
    'UNABLE TO CONNECT' received, is the ignition on?

The expected result should have been:

Engine spins at 0 RPMs

Environment

samifruit514 commented 4 years ago

ELM327 device version: result of GetVersion: "Device has version OBDII to RS232 Interpreter" ELM327 device: Revesun ELM327 USB OBD2 Car make: Hyundai Car model: Accent Car production year: 2004

rzetterberg commented 4 years ago

Thanks for the additional info!

According to this page your car uses the ISO 9141 protocol.

If you make the following change to line 226 of device.go:

+   rawRes := dev.rawDevice.RunCommand("ATSP3")
-   rawRes := dev.rawDevice.RunCommand("ATSP0")

Then your ELM327-device will use protocol 3 (ISO 9141).

Can you reproduce the problem if you make this change only (and not the changes with added timeouts)? If you can, then we can drop the idea about the problem being related to automatic protocol handling.

samifruit514 commented 4 years ago

Hello there, I tried ATSP3 with the RPM example, and instead of getting a "Failed to get rpm EOF" error, I get "Engine spins at 0.000000 RPMs". What was confusing me at first is that the "Engine spins at 0.000000 RPMs" message occurs when the engine is OFF (ignition on) but also when engine is ON, but is failing. This means if I run the command, I get "Engine spins at 0.000000 RPMs" and then I wait 5-6 seconds and run the command, then I get the proper rpm.

Note that the change I made for the timeout (https://github.com/rzetterberg/elmobd/pull/29/commits/0c547782d23b774bce53253b63d76f51fbd84fce#diff-ddb58cf92a33f42e2239251c44f1f93eL84) didn't change anything for ATSP3. for ATSP3 protocol, it works well with the 5 sec ReadTimeout AND with the wait delay of 5 seconds between calls.

On the bottomline, I think ATSP3 protocol seems to mute the problem - or maybe, the ATSP0 return a wrong message? (UNABLE TO CONNECT)