Help with Flexcan OBD reading

Hi everyone!

I’ve building a custom dashboard for my car and as part of it I’m using a Teensy 3.2 to read OBD information from my car (well an OBD simulator at the moment!)

I was using a very simple library to interface my sketch with Flexcan, but I am trying to remove the requirement for the library and bring the functionality directly into my sketch. I have stripped things right back and as far as I can tell the code is the same (maybe I’m missing something obvious), but where the old sketch read OBD information no problem, the new sketch does not.

I’ve run some serial prints and it seems that a message is getting out, but nothing is coming back to even start to interpret, so the error must be around the message transmission.

Here’s the new sketch I’m using:

#include <Arduino.h>
#include <FlexCAN.h>


/* Details from http://en.wikipedia.org/wiki/OBD-II_PIDs */
#define MODE1               0x01        //Show current data
#define MODE2               0x02        //Show freeze frame data
#define MODE3               0x03        //Show stored Diagnostic Trouble Codes
#define MODE4               0x04        //Clear Diagnostic Trouble Codes and stored values

#define PID_RPM             0x0C

#define MODE1_RESPONSE      0x41
#define PID_REQUEST         0x7DF
#define PID_REPLY           0x7E8

char PID_SOUGHT = 0x0C;
static CAN_message_t can_MsgRx, can_MsgTx;

void setup(void)
{
  Can0.begin(500000);
}


void loop(void)
{

  //---------PID REQUESTS
  CAN_Request(MODE1,PID_RPM);
  
  //---------PID RECEIVES
  int ENGINE_RPM = CAN_Receive(PID_RPM);

}

void CAN_Request(char CAN_MODE, char PID_SOUGHT) {
  can_MsgTx.buf[0] = 0x02;
  can_MsgTx.buf[1] = 0x01; //CAN_MODE;
  can_MsgTx.buf[2] = 0x0C; //PID_SOUGHT;  //PID TO CALL HERE
  can_MsgTx.buf[3] = 0;
  can_MsgTx.buf[4] = 0;
  can_MsgTx.buf[5] = 0;
  can_MsgTx.buf[6] = 0;
  can_MsgTx.buf[7] = 0;
  can_MsgTx.len = 8;
  //can_MsgTx.ext = 0;
  can_MsgTx.flags.extended = 0;
  can_MsgTx.flags.remote = 0;
  can_MsgTx.id = 0x7DF; //PID_REQUEST;
  //  can_MsgTx.timeout = 500;
  Can0.write(can_MsgTx);
}

char CAN_Receive(char PID_SOUGHT){
    while (Can0.available())
  {
    Can0.read(can_MsgRx);
    Serial.println(can_MsgRx.buf[1]);
  
    if ((can_MsgRx.id == PID_REPLY) && (can_MsgRx.buf[2] == PID_SOUGHT))
    {
      switch (can_MsgRx.buf[2])
      {
        case PID_RPM:              //   ((A*256)+B)/4    [RPM]
          return  ((can_MsgRx.buf[3] * 256) + can_MsgRx.buf[4]);
          break;
      }
      return 1;
    }
  } // while
}

I’ll attach the old libraries I was using (that worked ok) this is how they used to be called from the sketch:

N_ECU_R.init(500000);

if (N_ECU_R.request(ENGINE_RPM, &engine_data) == 1) if (engine_data != RPM) RPM = engine_data;

N_ECU_R.cpp (4.51 KB)

N_ECU_R.h (1.81 KB)

Bumping :slight_smile: