Hi guys.
I'm new in arduino, coding and all this things, so i hope my problem is simple, BUT I`ve looked over many topics here and haven't find 1 similar problem :(.
I have combination of CAN Shield "Niren" with MCP2515 and arduino nano. Here is the wiring diagram: link
Also i have a properly working CanHacker https://canhacker.com device with CarBUSAnalyzer installed on my PC, that helps me with testing.
Now I m trying to recieve messages from CAN and send some there, and there's no problem with sending, as you could see further, but i can recieve no messages with uncorrupted data (ID, DLC, data bytes) in it.
I've tried two libraries that works with MCP2515:
-
Autowp arduino-mcp2515 lib
Using this lib gives me no incoming ID from the message (id=1), but gives some DLC and data (but data is incorrect).
Modified code from the example:
#include <SPI.h>
#include <mcp2515.h>
struct can_frame canMsg;
MCP2515 mcp2515(9);
void setup() {
Serial.begin(9600);
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
mcp2515.setNormalMode();
pinMode(2, INPUT);
Serial.println("------- CAN Read ----------");
struct can_frame frame;//initial test frame
frame.can_id = 0x17330500 | CAN_EFF_FLAG;
frame.can_dlc = 3;
frame.data[0] = 0x20;
frame.data[1] = 0x5F;
frame.data[2] = 0x02;
Serial.println("Sending test message: 0x17330500, 3, 20 5F 02");
mcp2515.sendMessage(MCP2515::TXB1, &frame);
}
void loop() {
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
Serial.print("Arduino recieved the message with: Id ");
Serial.print(canMsg.can_id, HEX); // print ID
Serial.print(", DLC ");
Serial.print(canMsg.can_dlc, HEX); // print DLC
Serial.print(", data ");
for (int i = 0; i<canMsg.can_dlc; i++) { // print the data
Serial.print(canMsg.data[i],HEX);
Serial.print(" ");
}
Serial.println();
}
}
Log from the serial after sending one message from the canhacker:
------- CAN Read ----------
Sending test message: 0x17330500, 3, 20 5F 02
Arduino recieved the message with: Id 1, DLC 3, data 0 4F FF
and that`s what I see in CarBUSAnalyzer:
Out message (that one which Arduino recieves) is in the bottom, In message is on the top of the screen.
This lib gives me some incorrect ID, but 0 DLC and no data at all...
Code:
// CAN Receive Example
//
#include <mcp_can.h>
#include <SPI.h>
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128]; // Array to store serial string
#define CAN0_INT 2 // Set INT to pin 2
MCP_CAN CAN0(9); // Set CS to pin 9
void setup() {
Serial.begin(9600);
// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
if (CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK)
Serial.println("MCP2515 Initialized Successfully!");
else
Serial.println("Error Initializing MCP2515...");
CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data.
pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input
Serial.println("MCP2515 Library Receive Example...");
Serial.println();
}
unsigned long exampleId = 0x17330110 | 0x80000000UL;
byte exampleData[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
void loop() {
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)
if ((rxId & 0x80000000) == 0x80000000) // Determine if ID is standard (11 bits) or extended (29 bits)
sprintf(msgString, "Arduino recieved the message with: Extended ID 0x%.8lX DLC %1d", (rxId & 0x1FFFFFFF), len);
else
sprintf(msgString, "Standard ID: 0x%.3lX ,DLC: %1d Data:", rxId, len);
Serial.println(msgString);
Serial.print("Arduino sending a message with: ID = 0x17330100, Extended CAN Frame, Data length = 8 bytes, 'data' = 00 01 02 03 04 05 06 07");
byte sndStat = CAN0.sendMsgBuf(0x17330100, 1, 8, exampleData);
if (sndStat == CAN_OK) {
Serial.println("Message Sent Successfully!");
}
Serial.println();
}
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/
Serial log after sending 3 messages from CanHacker:
Arduino recieved the message with: Extended ID 0x00130010 DLC 0
Arduino sending a message with: ID = 0x17330100, Extended CAN Frame, Data length = 8 bytes, 'data' = 00 01 02 03 04 05 06 07
Message Sent Successfully!
Arduino recieved the message with: Extended ID 0x1FF30010 DLC 0
Arduino sending a message with: ID = 0x17330100, Extended CAN Frame, Data length = 8 bytes, 'data' = 00 01 02 03 04 05 06 07
Message Sent Successfully!
Arduino recieved the message with: Extended ID 0x1FF30010 DLC 0
Arduino sending a message with: ID = 0x17330100, Extended CAN Frame, Data length = 8 bytes, 'data' = 00 01 02 03 04 05 06 07
and that`s what I see in CarBUSAnalyzer:
note that I sent three identical messages, but the id of the first message received by the arduino is different from the others.
Please help me guys, what am i doing wrong? I`ve doublechecked my devices, there are no problems and no errors with soldering.