stanleyhuangyc / ArduinoOBD

OBD-II library and sketches for Arduino
http://freematics.com
986 stars 518 forks source link

How to check state of OBD connection? #52

Open benjaminBrownlee opened 6 years ago

benjaminBrownlee commented 6 years ago

In the code there is the following function to fetch the state of the OBD:

// get connection state
virtual OBD_STATES getState() { 
      return m_state; 
}

These states also have there own variable type defined in the library:

// states
typedef enum {
    OBD_DISCONNECTED = 0,
    OBD_CONNECTING = 1,
    OBD_CONNECTED = 2,
    OBD_FAILED = 3
} OBD_STATES;

I actually have two questions here. First, how can I get the state of the OBD into a usable format? I have tried casting and comparing, as shown below, with little sucess:

boolean isConnected = obd.getState() == OBD_CONNECTED;
int state = (int) obd.getState();

Also, what is the point of making a function that returns a useless variable type? There are no built in functions to these variables that provide any utility to the user, and as far as I can see, they are fairly inconvenient (or even impossible) to get in a format that can interact with other code.

benjaminBrownlee commented 6 years ago

Never mind, I figured it out.

Serial.write("ODB State: ");
  OBD_STATES state = obd.getState();
  if(state == OBD_DISCONNECTED) Serial.write("Disconnected");
  if(state == OBD_CONNECTING) Serial.write("Connecting");
  if(state == OBD_CONNECTED) Serial.write("Connected");
  if(state == OBD_FAILED) Serial.write("Failed");
  Serial.write("\n");