Can bus shield id <-> content mismatch

EDIT:
Oh I think that now I see where is problem. I’m trying tp get message id before reading message. So this mean that I get id from previous message. Am I right?

Hi,

I tried to read data from my car through Can bus shield but I found problem. First I couldn’t read messages at all because of this problem but yesterday I found out what’s the matter. I’m using your example code with addition of getting message id.

[code]
INT32U canId = 0x000;
.
.
.
void loop()
{
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
{
canId = CAN.getCanId();
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf

    Serial.print("<");Serial.print(canId);Serial.print(":\t");
    for(int i = 0; i<len; i++)    // print the data
    {
        Serial.print(buf[i]);Serial.print(",\t");
    }
    Serial.println();
}

}[/code]

Everything is OK but message ID for actual message is sent in next line as you can see in this example.

. . . <1BE: 32, 31, 30, 34, 32, 31, 31, 34> -> This should be 2B6 instead of 1BE <2B6: F2, 00, 00, 00, 00, FF, 00, 00> -> This should be 217 instead of 2B6 <217: 00, 00, 00, 00, 00, 00, 11, D0> -> This should be next line message id instead of 217 . . .

That is right, you have to read the message before getCanId is loaded. Ideally, the ID should be brought out with the readMsgBuf function and branch of the library I have been working on does this.

When i use getCanId() it prints some long ID to the serial port, not the HEX number i expected.

Like this:

NEW Length=8 ID 254542800 : 0, 0, 96, 1, 0, 0, 0, 0,

Any idea please?

[code]#include <mcp_can.h>
#include <mcp_can_dfs.h>

// demo: CAN-BUS Shield, receive data with interrupt mode
// when in interrupt mode, the data coming can’t be too fast, must >20ms, or else you can use check mode
// loovee, 2014-6-13

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

MCP_CAN CAN(10); // Set CS to pin 10

unsigned char Flag_Recv = 0;
unsigned char len = 0;
//unsigned long ID = 0;
unsigned long ID = 0x000;
unsigned char buf[8];
char str[20];

void setup()
{
Serial.begin(115200);

START_INIT:

if(CAN_OK == CAN.begin(CAN_125KBPS))                   // init can bus : baudrate = 500k
{
    Serial.println("CAN BUS Shield init ok!");
}
else
{
    Serial.println("CAN BUS Shield init fail");
    Serial.println("Init CAN BUS Shield again");
    delay(100);
    goto START_INIT;
}

attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt

}

void MCP2515_ISR()
{
Flag_Recv = 1;
}

void loop()
{
if(Flag_Recv) { // check if get data

Flag_Recv = 0;                // clear flag

// iterate over all pending messages
// If either the bus is saturated or the MCU is busy,
// both RX buffers may be in use and reading a single
// message does not clear the IRQ conditon.
while (CAN_MSGAVAIL == CAN.checkReceive()) {
  // read data,  len: data length, buf: data buf
  
  
  CAN.readMsgBuf(&len, buf);
  ID=CAN.getCanId();
  Serial.print("NEW  ");
  Serial.print("Length=");
  Serial.print(len,DEC);
  Serial.print("  ID ");
  Serial.print(ID);
  Serial.print(" : ");
       // print the data
  for(int i = 0; i<len; i++) {
Serial.print(buf[i]);
    Serial.print(",\t");
  }
  Serial.println();
} 

}
}[/code]