CAN bus implementation in real time CAR

Hi All,
I have been working with arduino UNO R3 and Sparkfun CAN shield to get the Electronics control unit responses from the real time CAR through OBD II diagnostic port in the CAR.

#include <Canbus.h>
char UserInput;
int data;
char buffer[456];  //Data will be temporarily stored to this buffer before being written to the file

//********************************Setup Loop*********************************//

void setup(){
Serial.begin(9600);
Serial.println("CAN-Bus Demo");

if(Canbus.init(CANSPEED_500))  /* Initialise MCP2515 CAN controller at the specified speed */
  {
    Serial.println("CAN Init ok");
  } else
  {
    Serial.println("Can't init CAN");
  } 
   
  delay(1000); 

Serial.println("Please choose a menu option.");
Serial.println("1.Speed");
Serial.println("2.RPM");
Serial.println("3.Throttle");
Serial.println("4.Coolant Temperature");
Serial.println("5.O2 Voltage");
Serial.println("6.MAF Sensor");

}



//********************************Main Loop*********************************//

void loop(){

while(Serial.available()){
   UserInput = Serial.read();

if (UserInput=='1'){
 Canbus.ecu_req(VEHICLE_SPEED, buffer);
 Serial.print("Vehicle Speed: ");
 Serial.println(buffer);
 delay(500);
}
else if (UserInput=='2'){
 Canbus.ecu_req(ENGINE_RPM, buffer);
 Serial.print("Engine RPM: ");
 Serial.println(buffer);
 delay(500);

}
else if (UserInput=='3'){
 Canbus.ecu_req(THROTTLE, buffer);
 Serial.print("Throttle: ");
 Serial.println(buffer);
 delay(500);

}
else if (UserInput=='4'){
 Canbus.ecu_req(ENGINE_COOLANT_TEMP, buffer);
 Serial.print("Engine Coolant Temp: ");
 Serial.println(buffer);
 delay(500);

}
else if (UserInput=='5'){
 Canbus.ecu_req(O2_VOLTAGE, buffer);
 Serial.print("O2 Voltage: ");
 Serial.println(buffer);
 delay(500);

}
else if (UserInput=='6'){
 Canbus.ecu_req(MAF_SENSOR, buffer);
 Serial.print("MAF Sensor: ");
 Serial.println(buffer);
 delay(500);

}
else
{
  Serial.println(UserInput);
  Serial.println("Not a valid input.");
  Serial.println("Please enter a valid option.");
}

}
}

I uploaded this code to the setup and monitored the serial monitor provided in the Arduino IDE.
The baud rate for serial monitoring was 9600 and the CAN initialization was done at 500kbps.
I get the output as following:

OUTPUT:

CAN-Bus Demo
CAN Init ok
Please choose a menu option
1.Speed");
2.RPM
3.Throttle
4.Coolant Temperature
5.O2 Voltage
6.MAF Sensor
when I type 1 and enter send I get
Vehicle Speed:
Again when I type 1 and enter send I get
Vechicle Speed: 12 km
Now Next If I type 2 and enter send I get
Engine RPM: 870 km
But I shoud get 870 rpm.

similarly If I change from one parameter to another the unit of it is unchanged for some time and after then it changes.
What should I change either the serial monitor baud rate or the CAN bit rate???
Or should I change anything else please help me
thank you in advance

Which Canbus library are you using? Hopefully not the crappy one that Sparkfun links to.

PaulS: Which Canbus library are you using? Hopefully not the crappy one that Sparkfun links to.

Unfortunately , I am using the same Sparkfun library.Which library do you suggest???

Unfortunately , I am using the same Sparkfun library.

You could fix their library, and send the fixed library back to them.

char CanbusClass::message_rx(unsigned char *buffer)

But the function does not return anything.

char CanbusClass::ecu_req(unsigned char pid,  char *buffer)

But the function does not return anything.

And, the function sometimes puts a space at the end of the string and sometimes doesn't.

The function appears to be putting a NULL in the buffer, but it never writes anywhere near 456 characters into the buffer.

Perhaps your issue is really that your buffer is enormously oversized.

You are also uselessly wasting SRAM by not using the F() macro to keep the data out of SRAM. Serial.println(F("CAN-Bus Demo"));

See what happens with a more reasonable buffer size and some attempts to conserve SRAM.

#include <Canbus.h>
char UserInput;
int data;
char buffer[15];  //Data will be temporarily stored to this buffer before being written to the file

//********************************Setup Loop*********************************//

void setup(){
Serial.begin(9600);
Serial.println(F("CAN-Bus Demo"));

if(Canbus.init(CANSPEED_500))  /* Initialise MCP2515 CAN controller at the specified speed */
  {
    Serial.println(F("CAN Init ok"));
  } else
  {
    Serial.println(F("Can't init CAN"));
  } 
   
  delay(1000); 

Serial.println(F("Please choose a menu option."));
Serial.println(F("1.Speed"));
Serial.println(F("2.RPM"));
Serial.println(F("3.Throttle"));
Serial.println(F("4.Coolant Temperature"));
Serial.println(F("5.O2 Voltage"));
Serial.println(F("6.MAF Sensor"));

}

I Have done the Changes which U asked to do,But I am getting the same problem
I have taken a screen shot of the output