Hello, i m using mcp2515 with Arduino Uno R3 board. My mcp is connected after volkswagen pq35 gateway drive train can bus and by sketch only have can send (with delay) and can read. I have a problem with vcds car scanner (error when dtcs reading, ecu identification problems and gateway entering timeout, program freezes), in one word - is conflict. That problem can't resolve when i remove the read function, only resolve when i remove send function. Send function delayed by 300-400 ms. I found some info for ack, but i dont understanding what it is help for this.
Send function:
#include <mcp_can.h>
#include <SPI.h>
void sendPID(unsigned char __pid) {
unsigned char tmp[8] = {0x02, 0x01, __pid, 0, 0, 0, 0, 0};
byte sndStat = CAN0.sendMsgBuf(0x7E0, 0, 8, tmp);
if (sndStat == CAN_OK) {
Serial.print("PID sent: 0x");
Serial.println(__pid, HEX);
}
else {
Serial.println("Error Sending Message...");
}
}
Read function:
char* receivePID(unsigned char __pid, int * response) {
if (!digitalRead(CAN0_INT)) { // If CAN0_INT pin is low, read receive buffer
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
sprintf(msgString, "Standard ID: 0x%.3lX, DLC: %1d, Data: ", rxId, len);
Serial.print(msgString);
for (byte i = 0; i < len; i++) {
sprintf(msgString, " 0x%.2X", rxBuf[i]);
Serial.print(msgString);
}
Serial.println("");
switch (__pid) {
case PID_ENGINE_RPM:
if(rxBuf[2] == PID_ENGINE_RPM){
uint16_t rpm;
rpm = ((256 * rxBuf[3]) + rxBuf[4]) / 4;
* response = rpm;
Serial.print("Engine Speed (rpm): ");
Serial.println(rpm, DEC);
}
break;
return (char*) "OK";
}
return (char*) "ERR:READ";
Can initialization function:
void setupCanService() {
Serial.begin(115200);
if (CAN0.begin(MCP_NORMAL, CAN_500KBPS, MCP_8MHZ) == CAN_OK) {
// can try MCP_STDEXT
Serial.println("MCP2515 Initialized Successfully!");
}
else {
Serial.println("Error Initializing MCP2515...");
while (1);
}
//initialise mask and filter to allow only receipt of 0x7xx CAN IDs
CAN0.init_Mask(0,0,0x07FF0000); // check all 11 address bits for RXB0
CAN0.init_Filt(0,0,0x07E80000); // Allow 07E8 through RXB0.
CAN0.init_Filt(1,0,0x07E90000); // Allow 07E9 through RXB0
CAN0.init_Mask(1,0,0x07FF0000); // check all 11 address bits for RXB1
CAN0.init_Filt(2,0,0x07EA0000); // Allow 07EA through RXB1
CAN0.init_Filt(3,0,0x07EB0000); // Allow 07EB through RXB1
CAN0.init_Filt(4,0,0x07EC0000); // Allow 07EC through RXB1
CAN0.init_Filt(5,0,0x07ED0000); // Allow 07ED through RXB1
CAN0.setMode(MCP_NORMAL);
pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input
Serial.println("Sending and Receiving OBD-II_PIDs Example...");
}