Skip to content

Question about manually parsing responses. #292

@bananaMechanic

Description

@bananaMechanic

Apologies first. I am trying to learn C++, bluetooth serial, OBD2 command and response structure all at the same time and I'm sure what I'm missing is something fundamental and simple.

I'm trying to extract transmission temperature, proving to be easy on a 2018 CAN Silverado and harder on 2001 VPW Silverado. For the 2018 Silverado, I can set header to 7E2 and
tts = (((myELM327.processPID(34, 6464, 1, 1, 1, 0) - 40) * 1.8) + 32);
works perfectly. The truck takes 221940 and returns 621940XX (with XX being the single data byte).

However on the older VPW truck, the command is actually 22194001, but it still only returns 62 19 40 XX. When I try to use processPID on that one, it seems like the processPID is getting confused by the missing 4th byte (01) in the return.
I apologize that I don't have a debug print from this, I only have access to that truck periodically.

I figured I could just use sendCommand and parse the response myself, but I'm missing something here.

The following code was written for the 2018 CAN vehicle and is essentially a strict adaptation of one of the examples:

#include "BluetoothSerial.h"
#include "ELMduino.h"

#define BT_DISCOVER_TIME 10000
esp_spp_sec_t sec_mask = ESP_SPP_SEC_NONE;
esp_spp_role_t role = ESP_SPP_ROLE_MASTER;
uint8_t address[6] = { 0x00, 0x1D, 0xA5, 0x06, 0xCC, 0x40 };  // Change this to the MAC address of your bluetooth ELM327 adapter

BluetoothSerial SerialBT;
#define ELM_PORT SerialBT
#define DEBUG_PORT Serial

ELM327 myELM327;
int nb_query_state = SEND_COMMAND;  // Set the inital query state ready to send a command

void setup() {

  DEBUG_PORT.begin(9600);
  myELM327.begin(ELM_PORT, true, 2000);
  ELM_PORT.begin("ArduHUD", true);

  if (!ELM_PORT.connect(address, sec_mask, role)) {
    Serial.println("Couldn't connect to ELM327 device.");
    while (1)
      ;
  }

  if (!myELM327.begin(ELM_PORT, true, 2000)) {
    Serial.println("ELM327 device couldn't connect to ECU.");
    while (1)
      ;
  }
  Serial.println("Connected to ELM327");
  myELM327.sendCommand_Blocking("ATSH 7E2");  // Set a custom header using ATSH command.
}

void loop() {

  if (nb_query_state == SEND_COMMAND)  // We are ready to send a new command
  {
    myELM327.sendCommand("221940");           // Send the custom PID commnad
    nb_query_state = WAITING_RESP;            // Set the query state so we are waiting for response
  } else if (nb_query_state == WAITING_RESP)  // Our query has been sent, check for a response
  {
    myELM327.get_response();  // Each time through the loop we will check again
  }
  if (myELM327.nb_rx_state == ELM_SUCCESS)  // Our response is fully received, let's get our data
  {
    int A = myELM327.payload[6];    // Parse the temp value A from the response
    Serial.println(A);              // Print the returned value
    nb_query_state = SEND_COMMAND;  // Reset the query state for the next command
    delay(5000);                    // Wait 5 seconds until we query again
  }
  else if (myELM327.nb_rx_state != ELM_GETTING_MSG) {  // If state == ELM_GETTING_MSG, response is not yet complete. Restart the loop.
    nb_query_state = SEND_COMMAND;                     // Reset the query state for the next command
    myELM327.printError();
    delay(5000);  // Wait 5 seconds until we query again
  }
}

When I run this, the debug print shows that the command is being sent properly, and the return is appropriate and correct, but when I ask it to Serial.println(A) it gives me the integer 52, and for the life of me I can't figure out why.

Clearing input serial buffer
Sending the following command/query: 221940
	Received char: 6
	Received char: 2
	Received char: 1
	Received char: 9
	Received char: 4
	Received char: 0
	Received char: 4
	Received char: 9
	Received char: \r
	Received char: \r
	Received char: >
Delimiter found.
All chars received: 62194049

52

I've tried poking through the .h and .cpp files for clues on how the payload works, but this is where my lack of knowledge shows up. Looking for some insight on where I'm going wrong.

Metadata

Metadata

Assignees

Labels

questionFurther information is requested

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions