Hello!
I’m trying to use an arduino with the “CAN-BUS Shield V2” from seeeduino to read the output of a Mateksys “CAN-L4-BM” sensor. I am able to read the frames coming from the battery monitor. And while the transfer ID is incrementing, the data does not seem to change.
I am using the Seeed_Arduino_CAN library made for the shield, so I can’t take credit for most of the code, but the program I have been using looks like this:
// demo: CAN-BUS Shield, receive data with check mode
// send data coming to fast, such as less than 10ms, you can use this way
// loovee, 2014-6-13
#include <SPI.h>
#define CAN_2515
// #define CAN_2518FD
// Set SPI CS Pin according to your hardware
#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD)
// For Wio Terminal w/ MCP2518FD RPi Hat:
// Channel 0 SPI_CS Pin: BCM 8
// Channel 1 SPI_CS Pin: BCM 7
// Interupt Pin: BCM25
const int SPI_CS_PIN = BCM8;
const int CAN_INT_PIN = BCM25;
#else
// For Arduino MCP2515 Hat:
// 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 = 9;
const int CAN_INT_PIN = 2;
#endif
#ifdef CAN_2518FD
#include "mcp2518fd_can.h"
mcp2518fd CAN(SPI_CS_PIN); // Set CS pin
#endif
#ifdef CAN_2515
#include "mcp2515_can.h"
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
#endif
const int LED = 8;
boolean ledON = 1;
void setup() {
SERIAL_PORT_MONITOR.begin(115200);
pinMode(LED, OUTPUT);
while (CAN_OK != CAN.begin(CAN_1000KBPS)) { // init can bus : baudrate = 500k
SERIAL_PORT_MONITOR.println("CAN init fail, retry...");
delay(100);
}
SERIAL_PORT_MONITOR.println("CAN init ok!");
}
void loop() {
unsigned char len = 0;
unsigned char buf[8];
if (CAN_MSGAVAIL == CAN.checkReceive()) { // check if data coming
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
unsigned long canId = CAN.getCanId();
SERIAL_PORT_MONITOR.println("-----------------------------");
SERIAL_PORT_MONITOR.print("get data from ID: 0x");
SERIAL_PORT_MONITOR.println(canId, HEX);
//------------------------------Start of the part I have modified------------------------------------------
//print data bytes 0 to 6
for (int i = 0; i < 7; i++) {
SERIAL_PORT_MONITOR.print("Byte ");
SERIAL_PORT_MONITOR.print(i);
SERIAL_PORT_MONITOR.print(": ");
SERIAL_PORT_MONITOR.print(buf[i]);
SERIAL_PORT_MONITOR.print("\t");
}
//print the tail byte info
SERIAL_PORT_MONITOR.print("Start of transfer bit: ");
SERIAL_PORT_MONITOR.print((buf[7] & 0x80) >> 7);
SERIAL_PORT_MONITOR.print("\t");
SERIAL_PORT_MONITOR.print("End of transfer bit: ");
SERIAL_PORT_MONITOR.print((buf[7] & 0x40) >> 6);
SERIAL_PORT_MONITOR.print("\t");
SERIAL_PORT_MONITOR.print("Toggle bit: ");
SERIAL_PORT_MONITOR.print((buf[7] & 0x20) >> 5);
SERIAL_PORT_MONITOR.print("\t");
SERIAL_PORT_MONITOR.print("Transfer ID bits: ");
SERIAL_PORT_MONITOR.print(buf[7] & 0x1F);
SERIAL_PORT_MONITOR.print("\t");
//--------------------------------End of the part I have modified------------------------------------------
SERIAL_PORT_MONITOR.println();
}
}
//END FILE
And I am able to get the frame but the data stays the same, as shown below:
I looked in the ArduPilot source code, and from what I was able to tell, the data to be received covers more than 7 bytes (maybe I’m looking at something unrelated), which tells me that I might need a multi frame transfer, yet both the start- and end of transfer bits are 1.
I’m hoping someone can help by telling me if that is the problem, if I need to request the data somehow, or if its its something completely different.
Thank you!