Hello,
I have a question regarding CAN with RPi, which seems to be a simple question but I cannot find a answer after googling a lot. I hope someone here can give me a hint.
Setup:
I am using a RPi with a CAN-SPI adapter using MCP2515 and MCP2551 to communicate with a device using CANopen protocol. I am programming in C and using SocketCAN for sending and receiving CAN frames to/from the CAN bus.
Problem:
The CAN bus randomly dies (quite often): CAN message cannot be sent to the bus either in C by write() or in the shell by 'cansend'; CAN message cannot be received either in C by read() or in shell by 'candump'.
However, when this happens, the CAN bus can be easily reinitialized by 'sudo ifconfig can0 down && sudo ifconfig can0 up'.
Questuins:
1. How can I detect the CAN bus is dead in C? Besides the symptoms, is there a flag I can check to detect the problem? E.g. how can I check the current length in the socket's TX buffer (if it is not 0 say 10 ms after write(), the bus is dead)?
2. How can I know the CAN bus is dead in the shell from the output of 'sudo IP -s -d link show can0' or 'ifconfig can0'?
3. How can I reinitialize the CAN bus in C to have the same effect as performing 'sudo ifconfig can0 down/up' in shell?