Can bus read multiple data from obd

I want to display some data from my car with the help of arduino. I selected 9 random data for now and I want to add more in the future.
I was able to receive data, but it does not come sequentially and quickly.I would appreciate if you share your suggestions with me on how to improve my coding.
And I have an idea to update the primary data more often for example car speed and engine speed are less priority I don't know where to start on this issue.
I would appreciate it if you could share your suggestions about how I should proceed in this regard.

My code;

#include <mcp_can.h>
#include <SPI.h>

  #define LISTEN_ID 0x7EA
  #define REPLY_ID 0x7E0
  #define FUNCTIONAL_ID 0x7DF

// CAN TX Variables
unsigned long prevTx = 0;
unsigned int invlTx = 100;

byte pidarray[9] =  {0x05,0x0B,0x0C,0x0D,0x0F,0x11,0x5C,0x5D,0x5E};
int Engine_Coolant_Temp,Intake_Pressure,Engine_RPM,Vehicle_Speed,IAT,Throttle_Position,Engine_Oil_Temp,Fuel_Injection_Timing,Engine_Fuel_Rate;

// CAN RX Variables
unsigned long rxID;
byte dlc;
byte rxBuf[8];
char msgString[128];                        // Array to store serial string

// CAN Interrupt and Chip Select Pins
#define CAN0_INT 7                              
MCP_CAN CAN0(17);                               

void setup(){

  Serial.begin(115200);
  while(!Serial);
 
  // Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
  if(CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ) == CAN_OK)
    Serial.println("MCP2515 Initialized Successfully!");
  else{
    Serial.println("Error Initializing MCP2515... Permanent failure!  Check your code & connections");
    while(1);
  }

  // Standard ID Filters
  CAN0.init_Mask(0,0x7F00000);                // Init first mask...
  CAN0.init_Filt(0,0x7DF0000);                // Init first filter...
  CAN0.init_Filt(1,0x7E10000);                // Init second filter...

  CAN0.init_Mask(1,0x7F00000);                // Init second mask...
  CAN0.init_Filt(2,0x7DF0000);                // Init third filter...
  CAN0.init_Filt(3,0x7E10000);                // Init fourth filter...
  CAN0.init_Filt(4,0x7DF0000);                // Init fifth filter...
  CAN0.init_Filt(5,0x7E10000);                // Init sixth filter...

  CAN0.setMode(MCP_NORMAL);                      // Set operation mode to normal so the MCP2515 sends acks to received data.
  
  pinMode(CAN0_INT, INPUT);                          // Configuring pin for /INT input

}

void loop(){

  if(!digitalRead(CAN0_INT)){                         // If CAN0_INT pin is low, read receive buffer
 
    CAN0.readMsgBuf(&rxID, &dlc, rxBuf);             // Get CAN data
  
    if((rxID & 0x41000000) == 0x41000000){    // Determine if message is a remote request frame.
      sprintf(msgString, " REMOTE REQUEST FRAME");
      Serial.print(msgString);
    } else {

      
if (rxBuf[2]==0x05) {
    Engine_Coolant_Temp = rxBuf[3]-40;
    Serial.print("Engine_Coolant_Temp:");
    Serial.println(Engine_Coolant_Temp);
    } 
    else if (rxBuf[2]==0x0B) {
    Intake_Pressure = rxBuf[3];
    Serial.print("Intake_Pressure:");
    Serial.println(Intake_Pressure);
    } 
    else if (rxBuf[2]==0x0C) {
    Engine_RPM = (256*rxBuf[3]+rxBuf[4])/4;
    Serial.print("Engine_RPM:");
    Serial.println(Engine_RPM);
    } 
    else if (rxBuf[2]==0x0D) {
    Vehicle_Speed = rxBuf[3];
    Serial.print("Vehicle_Speed:");
    Serial.println(Vehicle_Speed);
    }
    else if (rxBuf[2]==0x0F) {
    IAT = rxBuf[3]-40;
    Serial.print("IAT:");
    Serial.println(IAT);
    }
    else if (rxBuf[2]==0x11) {
    Throttle_Position = (100/255)*rxBuf[3];
    Serial.print("Throttle_Position:");
    Serial.println(Throttle_Position);
    }
    else if (rxBuf[2]==0x5c) {
    Engine_Oil_Temp = rxBuf[3]-40;
    Serial.print("Engine_Oil_Temp:");
    Serial.println(Engine_Oil_Temp);
    }
    else if (rxBuf[2]==0x5D) {
    Fuel_Injection_Timing   = (((256*rxBuf[3])+rxBuf[4])/128)-210;
    Serial.print("Fuel_Injection_Timing:");
    Serial.println(Fuel_Injection_Timing);
    }
    else if (rxBuf[2]==0x5E) {
    Engine_Fuel_Rate   = (((256*rxBuf[3])+rxBuf[4])/20);
    Serial.print("Engine_Fuel_Rate:");
    Serial.println(Engine_Fuel_Rate);
    }
}
}

    for (int b = 0; b < 9 ; b++)
  {
    char pid;

    if (b==0) {
                pid=pidarray[0];
    } else if (b==1) {
                pid=pidarray[1];
}
else  if (b==2) {
                pid=pidarray[2];
}
else  if (b==3) {
                pid=pidarray[3];
}
else  if (b==4) {
                pid=pidarray[4];
}
else  if (b==5) {
                pid=pidarray[5];
}
else  if (b==6) {
                pid=pidarray[6];
}
else  if (b==7) {
                pid=pidarray[7];
}
else  if (b==8) {
                pid=pidarray[8];
}
  
    char tmp[8] = {0x02, 0x01, pid, 0, 0, 0, 0, 0};
  
  if((millis() - prevTx) >= invlTx){
    prevTx = millis();
    if(CAN0.sendMsgBuf(FUNCTIONAL_ID,0, 8, tmp) == CAN_OK){
      Serial.print("Message Sent Successfully! b:"  );
      Serial.println(b);
    } else {
      Serial.println("Error Sending Message...");
      }
    }
  }
}

Serial monitor result;

Currently you are sending one request every 100 milliseconds. Can your car handle requests at a higher rate, say every 30 milliseconds?

At what intervals in milliseconds do you want to receive a vehicle speed or engine speed measurement?

hello, I want to get data as close as possible with the instrument panel.

If you are monitoring the CAN bus you will be getting it at the same time as the instrument panel.

As far as I can see, there are 2-3 different canbus libraries, with which of them can I get multiple data faster? Also, there may be delays due to my code. I want to minimize them. I'm open to your suggestions.

I wish I could help but I am not familiar with the libraries and cannot make a recommendations, maybe others can. The only thing I see that could speed it up is spend less time sending messages by making them shorter and or increase your terminal baud rate.

I did compile your code, it looks well done. You did use the same library I use, Cory Fowlers.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.