Some data loss during communication via car's CAN BUS

Hi guys,

I am very new to Arduino and this is my very first topic here. I am currently working on car's CAN BUS (500,000bps) communication in order to read and translate the CAN message real time. The UNO together with MCP2515 are connected to the car OBD2 port in order to sniff all supported PID data and the available output are now printed out and it looks promising enough (attached picture). However, I experienced frequent data loss, even though there is the delay of 5 seconds to read the next loop of data. I did try searching but never get any closer to the proper solution. Is this situation is called either Race Condition or data overflow? or I have to take a look at buffer size of transmitted data?

All source file and sketch are referred to GitHub - sandeepmistry/arduino-OBD2: An Arduino library for reading OBD-II data from your car over CAN bus.

Here is the sketch I used (libraries\OBD2-0.0.1\examples\OBD2_01_SupportedPIDs);

#include <CAN.h> // the OBD2 library depends on the CAN library
#include <OBD2.h>

void setup() {
  Serial.begin(9600);
  while (!Serial);

  Serial.println(F("OBD2 data printer"));

  while (true) {
    Serial.print(F("Attempting to connect to OBD2 CAN bus ... "));

    if (!OBD2.begin()) {
      Serial.println(F("failed!"));

      delay(1000);
    } else {
      Serial.println(F("success"));
      break;
    }
  }

  Serial.println();
  Serial.print("VIN = ");
  Serial.println(OBD2.vinRead());
  Serial.print("ECU Name = ");
  Serial.println(OBD2.ecuNameRead());
  Serial.println();
}

void loop() {
  // loop through PIDs 0 to 95, reading and printing the values
  for (int pid = 0; pid < 96; pid++) {
    processPid(pid);
  }
  Serial.println();

  // wait 5 seconds before next run
  delay(5000);
}

void processPid(int pid) {
  if (!OBD2.pidSupported(pid)) {
    // PID not supported, continue to next one ...
    return;
  }

  // print PID name
  Serial.print(OBD2.pidName(pid));
  Serial.print(F(" = "));

  if (OBD2.pidValueRaw(pid)) {
    // read the raw PID value
    unsigned long pidRawValue = OBD2.pidReadRaw(pid);

    Serial.print(F("0x"));
    Serial.print(pidRawValue, HEX);
  } else {
    // read the PID value
    float pidValue = OBD2.pidRead(pid);

    if (isnan(pidValue)) {
      Serial.print("error");
    } else {
      // print value with units

      Serial.print(pidValue);
      Serial.print(F(" "));
      Serial.print(OBD2.pidUnits(pid));
    }
  }

  Serial.println();
}

what do you see if you run the SupportedPIDs sketch?

is OBD2_02_KeyStats.ino running without reporting any error?

Hi J-M-L,

I attached the printout of sketch SupportPIDs. This sketch show only the name of supported PID on my car which seem to be correct.
16:49:14.386 -> PIDs supported [01 - 20]
16:49:14.419 -> Monitor status since DTCs cleared
16:49:14.453 -> Fuel system status
16:49:14.453 -> Calculated engine load
16:49:14.486 -> Engine coolant temperature
16:49:14.520 -> Short term fuel trim — Bank 1
16:49:14.554 -> Intake manifold absolute pressure
16:49:14.589 -> Engine RPM
16:49:14.623 -> Vehicle speed
16:49:14.623 -> Timing advance
16:49:14.623 -> Intake air temperature
16:49:14.657 -> MAF air flow rate
16:49:14.690 -> Throttle position
16:49:14.690 -> Oxygen sensors present (in 2 banks)
16:49:14.758 -> Oxygen Sensor 2 - Short term fuel trim
16:49:14.791 -> Run time since engine start
16:49:14.826 -> PIDs supported [21 - 40]
16:49:14.860 -> Distance traveled with malfunction indicator lamp (MIL) on
16:49:14.895 -> Commanded evaporative purge
16:49:14.928 -> Warm-ups since codes cleared
16:49:14.962 -> Distance traveled since codes cleared
16:49:14.997 -> Absolute Barometric Pressure
16:49:15.032 -> Oxygen Sensor 1 - Fuel–Air Equivalence Ratio
16:49:15.102 -> PIDs supported [41 - 60]
16:49:15.135 -> Control module voltage
16:49:15.135 -> Absolute load value
16:49:15.170 -> Fuel–Air commanded equivalence ratio
16:49:15.204 -> Relative throttle position
16:49:15.239 -> Absolute throttle position B
16:49:15.272 -> Absolute throttle position D
16:49:15.305 -> Absolute throttle position E
16:49:15.340 -> Commanded throttle actuator

And another attachment is the printout of sketch KeyStats . This sketch printout both names and values and there is still present of frequent error message and occur randomly among the shown PIDs.
16:41:42.960 -> Calculated engine load = 32.16 %
16:41:42.994 -> Engine coolant temperature = 82.00 °C
16:41:42.994 -> Engine RPM = 770.00 rpm
16:41:43.060 -> Vehicle speed = 0.00 km/h
16:41:43.128 -> Intake air temperature = 46.00 °C
16:41:43.197 -> MAF air flow rate = 2.93 grams/sec
16:41:43.267 -> Throttle position = 15.29 %
16:41:43.301 -> Run time since engine start = 255.00 seconds
16:41:43.371 -> Fuel Tank Level Input = error
16:41:43.405 -> Absolute Barometric Pressure = 100.00 kPa
16:41:43.439 -> Absolute load value = 25.49 %
16:41:43.507 -> Relative throttle position = 4.71 %

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