Hello, i want to send CAN data from one arduino to another but i cant, im a “begginer” in arduino, the last (and first) thing i did in arduino was to move a servo with a proximity sensor.
Im using two Keyestudio UNO R3 and Keyestudio Can Bus Shield, so far i was able to read cars, trucks, etc but when i try to make the arduinos “talk” to each other i cannot. Just in case, i do put the arduinos in the same can speed, and i connect them directly with CAN H and CAN L, also tried the DB9, and when i find more info about this they set the MHZ of the shield and i cant see that on mine.
To read the data im using a slightly modified example from the library arduino-mcp2515-master and its this one:
#include <SPI.h>
#include <mcp2515.h>
struct can_frame canMsg;
MCP2515 mcp2515(10);
void setup() {
Serial.begin(9600);
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS);
mcp2515.setNormalMode();
Serial.println("------- CAN Read ----------");
Serial.println("ID DLC DATA");
}
void loop() {
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
Serial.print(canMsg.can_id, HEX); // print ID
Serial.print(" ");
Serial.print(canMsg.can_dlc, HEX); // print DLC
Serial.print(" ");
for (int i = 0; i < canMsg.can_dlc; i++) { //
if (canMsg.data[i] < 0x10) { //
Serial.print('0'); //
} //
Serial.print(canMsg.data[i], HEX);
Serial.print(" ");
}
Serial.println();
}
}
As i said it works fine but im uploading it in case it could be its fault, so moving next to the sender arduino i have tried many different libraries and they didn’t work, also tried using a usb - db9 to send the data and i got another error ( i was using python to do it but it said that’s a readonly bus, same with python → arduino).
For the different libraries i’ve used are: the ones from corey, mc neil, seeduino and some other examples made in base of those (i’ve also used more libraries but don’t know the name of the authors).
What happens when i send the data?, well the arduino seems to send the data ok but the receptor doesn’t display anything on the serial and i think i may need to change a bit of the sender code but dont know where to start. here i’ll put a code that i used and didnt “work”.
#include <CAN.h>
#include <SPI.h> // required to resolve #define conflicts
// Define our CAN speed (bitrate).
#define bitrate CAN_BPS_500K
// data counter just to show a dynamic change in data messages
uint32_t extended_counter = 0;
uint32_t standard_counter = 0;
void setup()
{
Serial.begin(115200); // Initialize Serial communications with computer to use serial monitor
//Set CAN speed. Note: Speed is now 500kbit/s so adjust your CAN monitor
CAN.begin(bitrate);
delay(4000); // Delay added just so we can have time to open up Serial Monitor and CAN bus monitor. It can be removed later...
// Output will be formatted as a CSV file, for capture and analysis
Serial.println(F("millis(),standard_counter,extended_counter"));
}
// Create a function to load and send an extended frame message
void extendedMessage()
{
CAN_Frame extended_message; // Create message object to use CAN message structure
// There are at least two ways to put data into the message; memcpy() and individual arrays
uint8_t message_data[8] = { 0 };
extended_message.id = 0x02DACBF1; // Random Extended Message ID
extended_message.valid = true;
extended_message.rtr = 0;
extended_message.extended = CAN_EXTENDED_FRAME;
extended_message.length = 8; // Data length
// counter in the first 4 bytes, timing in the last 4 bytes
uint32_t timehack = millis();
// Load the data into a local array and then use memcpy() to transfer to CAN_Frame
message_data[0] = (extended_counter >> 24);
message_data[1] = (extended_counter >> 16);
message_data[2] = (extended_counter >> 8);
message_data[3] = (extended_counter & 0xF);
message_data[4] = (timehack >> 24);
message_data[5] = (timehack >> 16);
message_data[6] = (timehack >> 8);
message_data[7] = (timehack & 0xF);
memcpy(extended_message.data, message_data, sizeof(extended_message.data));
CAN.write(extended_message); // Load message and send
extended_counter++; // increase count
}
// Create a function to load and send a standard frame message
void standardMessage()
{
CAN_Frame standard_message; // Create message object to use CAN message structure
standard_message.id = 0x555; // Random Standard Message ID
standard_message.valid = true;
standard_message.rtr = 0;
standard_message.extended = CAN_STANDARD_FRAME;
standard_message.length = 8; // Data length
// counter in the first 4 bytes, timing in the last 4 bytes
uint32_t timehack = millis();
// Load the data directly into CAN_Frame
standard_message.data[0] = (standard_counter >> 24);
standard_message.data[1] = (standard_counter >> 16);
standard_message.data[2] = (standard_counter >> 8);
standard_message.data[3] = (standard_counter & 0xF);
standard_message.data[4] = (timehack >> 24);
standard_message.data[5] = (timehack >> 16);
standard_message.data[6] = (timehack >> 8);
standard_message.data[7] = (timehack & 0xF);
CAN.write(standard_message); // Load message and send
standard_counter++; // increase count
}
// Finally arduino loop to execute above functions with a 250ms delay
void loop()
{
standardMessage();
delay(250);
extendedMessage();
delay(250);
Serial.print(millis());
Serial.print(',');
Serial.print(standard_counter);
Serial.print(',');
Serial.println(extended_counter); // adds a line
}