Hi All,
I'm new to this forum but relatively familiar with the Arduino platform.
Currently, I'm working on a CAN-bus reader with the CAN-bus shield from Seeed Studio (V1.2).
I've managed to build a CAN Reader which will listen to the bus and display all messages to the screen.
While building a reader which will only read the PIDs from Mode 1, through sending a message to 0x7DF first, I'm only able to read up to 6 different PIDs.
Attached you may find the code I'm currently working with. Examining one PID gives the correct result, examining six PIDs it is also working, however, more fault data will appear on the serial monitor.
When using the complete 2D array it doesn't work anymore..
I did set a mask and filter to only receive messages from CAN ID 0x7E8 as can be read from ISO 15765-4, standard identifier.
In case something in my code is not clear please ask and I will try to explain.
You're help is greatly appreciated !
// CAN test code
#include <mcp_can.h>
#include <SPI.h>
#define CAN_ID 0x7DF
#define PID_ENG_COOL 0x05
// Interrupt pin
#define INTERRUPT 2
// Pin for SPI communication between CAN-shield and Arduino
const int SPI_CS_PIN = 9;
// Pin Setting
MCP_CAN readCAN(SPI_CS_PIN);
// Variables
unsigned char length = 0;
unsigned char data[8];
//byte dataBuffer[8] = {0x02, 0x01, PID_ENG_COOL, 0, 0, 0, 0, 0}; // Used to retrieve Coolant temperature
byte dataBuffer[8]; // Used icm. list of PIDs
char listPID[] = {0x05, 0x0C, 0x0D, 0x0F, 0x33, 0x51}; // Max number of PIDs which can be read
/*char listPID[][2] = {{0x00, 0x04}, {0x01, 0x04}, {0x02, 0x02}, {0x03, 0x02}, {0x04, 0x01}, {0x05, 0x01}, {0x06, 0x01}, {0x07, 0x01}, {0x08, 0x01}, {0x09, 0x01}, {0x0A, 0x01}, {0x0B, 0x01}, {0x0C, 0x02}, {0x0D, 0x01}, {0x0E, 0x01}, {0x0F, 0x01},
{0x10, 0x02}, {0x11, 0x01}, {0x12, 0x01}, {0x13, 0x01}, {0x14, 0x02}, {0x15, 0x02}, {0x16, 0x02}, {0x17, 0x02}, {0x18, 0x02}, {0x19, 0x02}, {0x1A, 0x02}, {0x1B, 0x02}, {0x1C, 0x02}, {0x1D, 0x01}, {0x1E, 0x01}, {0x1F, 0x02},
{0x20, 0x04}, {0x21, 0x02}, {0x22, 0x02}, {0x23, 0x02}, {0x24, 0x04}, {0x25, 0x04}, {0x26, 0x04}, {0x27, 0x04}, {0x28, 0x04}, {0x29, 0x04}, {0x2A, 0x04}, {0x2B, 0x04}, {0x2C, 0x01}, {0x2D, 0x01}, {0x2E, 0x01}, {0x2F, 0x01},
{0x30, 0x01}, {0x31, 0x02}, {0x32, 0x02}, {0x33, 0x01}, {0x34, 0x04}, {0x35, 0x04}, {0x36, 0x04}, {0x37, 0x04}, {0x38, 0x04}, {0x39, 0x04}, {0x3A, 0x04}, {0x3B, 0x04}, {0x3C, 0x02}, {0x3D, 0x02}, {0x3E, 0x02}, {0x3F, 0x02},
{0x40, 0x04}, {0x41, 0x04}, {0x42, 0x02}, {0x43, 0x02}, {0x44, 0x02}, {0x45, 0x01}, {0x46, 0x01}, {0x47, 0x01}, {0x48, 0x01}, {0x49, 0x01}, {0x4A, 0x01}, {0x4B, 0x01}, {0x4C, 0x01}, {0x4D, 0x02}, {0x4E, 0x02}, {0x4F, 0x04},
{0x50, 0x04}, {0x51, 0x01}, {0x52, 0x01}, {0x53, 0x02}, {0x54, 0x02}, {0x55, 0x02}, {0x56, 0x02}, {0x57, 0x02}, {0x58, 0x02}, {0x59, 0x02}, {0x5A, 0x01}, {0x5B, 0x01}, {0x5C, 0x01}, {0x5D, 0x02}, {0x5E, 0x02}, {0x5F, 0x01},
{0x60, 0x04}, {0x61, 0x01}, {0x62, 0x01}, {0x63, 0x02}};*/
void setup()
{
pinMode(INTERRUPT, INPUT);
Serial.begin(115200);
initializeCAN();
setMaskandFilter();
}
void loop()
{
for (int j = 0; j < sizeof(listPID); j++){
// dataBuffer[0] = listPID[j][1];
dataBuffer[0] = 0x02;
dataBuffer[1] = 0x01;
// dataBuffer[2] = listPID[j][0];
dataBuffer[2] = listPID[j];
dataBuffer[3] = 0x55;
dataBuffer[4] = 0x55;
dataBuffer[5] = 0x55;
dataBuffer[6] = 0x55;
dataBuffer[7] = 0x55;
// Send request to access Mode 1
readCAN.sendMsgBuf(CAN_ID, 0, 8, dataBuffer);
delay(200);
// Receive response from Mode 1
if(!digitalRead(INTERRUPT))
{
readCAN.readMsgBuf(&length, data); // read data, len: data length, buf: data buf
for(int i = 0; i < length; i++) // print the data
{
Serial.print(data[i]);
if (i < length - 1){
Serial.print(",");
}
}
Serial.println();
}
}
}
void initializeCAN() {
while (CAN_OK != readCAN.begin(CAN_500KBPS))
{
delay(1000);
}
}
void setMaskandFilter(){
readCAN.init_Mask(0, 0, 0x7FC);
readCAN.init_Mask(1, 0, 0x7FC);
readCAN.init_Filt(0, 0, 0x7E8);
readCAN.init_Filt(1, 0, 0x7E8);
readCAN.init_Filt(2, 0, 0x7E8);
readCAN.init_Filt(3, 0, 0x7E8);
readCAN.init_Filt(4, 0, 0x7E8);
readCAN.init_Filt(5, 0, 0x7E8);
delay(10);
}