Hi,
I've got a question about buffer overloads with canbus using the Sparkfun MCP2515_CAN module, with library GitHub - Seeed-Studio/Seeed_Arduino_CAN: Seeed Arduino CAN-BUS library - MCP2518FD&MCP2515&MCP2551
I would like to use the CAN protocol, I readed that it's very stable and has a good error handling. But is the buffer overload handled?
For the test, I setted up 2 arduino's, one is sending data to the other.
My question is: How can i be sure if the data is received right?
For the test i'm sending a message with a frequence of 100ms, and read it out at the other side with 300ms. the receiver has a buffer of 2 messages, so i expected a error would occur at the sending site, that the message is'nt delivered well, because the buffer is full. But this doesn't happen.
When the receiver is checked with CAN.checkError(), the error num 5 is received (i expect this is buffer full)
Is the only way to check the receive status, to send a OK signal back to the sender?
this would increase the amount of data sended over the bus.
Can anyone help me with this question?
Thanks!
code of the Receiver
#include <SPI.h>
#include "mcp_can.h"
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 53;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
}
void loop()
{
unsigned char len = 0;
unsigned char buf[8];
delay(300);
if (CAN.checkError()) {
Serial.println(CAN.checkError());
}
if (CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
unsigned int canId = CAN.getCanId();
Serial.println("-----------------------------");
Serial.print("Get data from ID: ");
Serial.println(canId, HEX);
for(int i = 0; i<len; i++) // print the data
{
Serial.print(buf[i], HEX);
Serial.print("\t");
}
Serial.println();
}
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/
code of the master
// demo: CAN-BUS Shield, send data
#include <mcp_can.h>
#include <SPI.h>
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 10;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
}
unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};
void loop()
{
// send data: id = 0x00, standrad frame, data len = 8, stmp: data buf
Serial.println(CAN.checkError());
Serial.println( CAN.sendMsgBuf(0x02, 1, 8, stmp));
delay(100); // send data per 100ms
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/