using Arduino Uno + Canbus Shield V1.2 using the library
during testing with a numberof other Can devices the methods MCP_CAN::isExtendedFrame() and MCP_CAN::isRemoteRequest() were reporting incorrect values
in particular after the first Extended frame MCP_CAN::isExtendedFrame() reported all following frames as Extended 29bit even if they were standard 11bit frames
investigation showed problems with method MCP_CAN::mcp2515_read_canMsg not setting the parameters correctly
this code appears to work
void MCP_CAN::mcp2515_read_canMsg( const byte buffer_load_addr, volatile unsigned long *id, volatile byte *ext, volatile byte *rtrBit, volatile byte *len, volatile byte *buf) /* read can msg */
{
byte tbufdata[4];
byte i;
MCP2515_SELECT();
spi_readwrite(buffer_load_addr);
// mcp2515 has auto-increment of address-pointer
for (i = 0; i < 4; i++) tbufdata[i] = spi_read();
*rtrBit=(tbufdata[3]&0x08 ? 1 : 0 ); // <<< does not work 23/1/2018
*id = (tbufdata[MCP_SIDH] << 3) + (tbufdata[MCP_SIDL] >> 5);
*ext=0; // <<< zero EXT bit 23/1/2013
if ( (tbufdata[MCP_SIDL] & MCP_TXB_EXIDE_M) == MCP_TXB_EXIDE_M )
{
/* extended id */
*id = (*id << 2) + (tbufdata[MCP_SIDL] & 0x03);
*id = (*id << 8) + tbufdata[MCP_EID8];
*id = (*id << 8) + tbufdata[MCP_EID0];
*ext = 1;
}
// *len=spi_read() & MCP_DLC_MASK; // <<< replaced with following statements
byte pMsgSize=spi_read(); // <<< read Data Length 23/1/2018
*len=pMsgSize & MCP_DLC_MASK; // <<< message length 23/1/2018
*rtrBit=(pMsgSize & MCP_RTR_MASK ? 1 : 0 ); // << get RTR bit 23/1/2018
for (i = 0; i < *len && i<CAN_MAX_CHAR_IN_MESSAGE; i++) {
buf[i] = spi_read();
}
MCP2515_UNSELECT();
}
testing with
#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 = 10;//9;
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]={0};
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
unsigned long int canId = CAN.getCanId();
char text[50]={0};
// setup standard 11 bit or extended 29 bit frame and add ID value
if(CAN.isExtendedFrame()) strcat(text,"EXT "); else strcat(text,"STD ");
sprintf(&text[strlen(text)]," ID %08lx ", canId);
Serial.print(text);
// check RTR or data frame
if(CAN.isRemoteRequest()) Serial.print(" RTR ");
else
for(int i = 0; i<8; i++) // print the data
Serial.print(buf[i], HEX);
Serial.println();
}
}
a run gives
CAN BUS Shield init ok!
STD ID 00000008 E6594DC30000
STD ID 00000008 RTR
EXT ID 000879fd E6594DC30000
EXT ID 000879fd RTR
STD ID 00000087 E6594DC30000
has anyone else found similar problems?