CAN Bus error state

I'm working on a can-bus diag tool that if it detects a CAN error will turn on a red light. This is to quickly troubleshoot if there is a CAN BUS error visually.

My question is looking at the various liberties I don't see how you detect CAN errors. I have used the libraries to send and receive certain messages but never dealt with CAN Bus state.

I would like to detect if can bus is "down." If can bus is shorted to ground or vcc. If can bus is sending incorrect messages..etc...

Does the library have built in modes for CAN error states like that?

I do see in this lib line 1360 there is a line for can error but not sure if that's useful?

https://github.com/Seeed-Studio/CAN_BUS_Shield/blob/master/mcp_can.cpp

Thanks,

the EFLG register gives you 'bus off' and a 'generic received error' state among some other error flags (check the mcp2515 datasheet if you want to know more)

so strictly speaking you would not be able to differenciate between the bus errors ie whether the bus was shorted to gnd of vcc for example.

Thank you for the info.

I would not need to know the difference but rather there was an error present.

I have looked through the data sheet for the MCP2515 but am not sure how it corresponds to the actual can library.

I'm not sure how in the code you call for can state or call for a error flag. Does the library just print out the exact message from the MCP2515 chip?

Thanks again

dark1990:
Thank you for the info.

I would not need to know the difference but rather there was an error present.

I have looked through the data sheet for the MCP2515 but am not sure how it corresponds to the actual can library.

I'm not sure how in the code you call for can state or call for a error flag. Does the library just print out the exact message from the MCP2515 chip?

Thanks again

From the library:

byte MCP_CAN::checkError(void)
{
    byte eflg = mcp2515_readRegister(MCP_EFLG);
    return ((eflg & MCP_EFLG_ERRORMASK) ? CAN_CTRLERROR : CAN_OK);
}

from what I can see, it returns a 'OK' or 'Error' but not the actual register value.

but if you want to get the whole register, you could just us this line from the code:

byte eflg = mcp2515_readRegister(MCP_EFLG);