Required help to send CAN messages throgh Arduino UNO + MCP2515

Dears,
I want to send this messages through Arduino... Kindly help me to Loop all this messages as per below Sketch given

0x014 s 4 0F 30 00 00
0x22F s 7 6B 8B 34 64 44 15 D8
0x22E s 7 81 5F D7 D3 0B 11 F0
0x014 s 4 01 31 00 00
0x230 s 7 E6 3B 2F 64 40 25 D8
0x22E s 7 02 D0 D8 CB 0B 11 F0

#include <mcp_can.h>
#include <SPI.h>

MCP_CAN CAN0(10); // Set CS to pin 10
void setup()
{
Serial.begin(115200);
// 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_16MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!");
else Serial.println("Error Initializing MCP2515...");
CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
}
byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
void loop()
{
// send data: ID = 0x100, Standard CAN Frame, Data length = 8 bytes, 'data' = array of data bytes to send
byte sndStat = CAN0.sendMsgBuf(0x100, 0, 8, data);
if(sndStat == CAN_OK){
Serial.println("Message Sent Successfully!");
} else {
Serial.println("Error Sending Message...");
}
delay(100); // send data per 100ms
}

const byte data1[] = {0x04, 0x0F, 0x30, 0x00, 0x00};
CAN0.sendMsgBuf(0x014, 0, sizeof data1, data1);

const byte data2[] = {0x07, 0x6B, 0x8B, 0x34, 0x64, 0x44, 0x15, 0xD8};
CAN0.sendMsgBuf(0x22F, 0, sizeof data2, data2);

something like this may be; it assumes all messages are to be transmitted at the same rate.
(Compiles, NOT tested!)

#include <mcp_can.h>
#include <SPI.h>

MCP_CAN CAN0(10); // Set CS to pin 10

uint16_t CANids[6] = {0x014, 0x22F, 0x22E, 0x014, 0x230, 0x22E};
uint8_t dlcs[6] = {4, 7, 7, 4, 7, 7};
uint8_t data_arr[6][7] = {
                          {0x0F, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00},
                          {0x6B, 0x8B, 0x34, 0x64, 0x44, 0x15, 0xD8},
                          {0x81, 0x5F, 0xD7, 0xD3, 0x0B, 0x11, 0xF0},
                          {0x01, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00},
                          {0xE6, 0x3B, 0x2F, 0x64, 0x40, 0x25, 0xD8},
                          {0x02 , 0xD0 , 0xD8 , 0xCB , 0x0B , 0x11 , 0xF0}
                         };
uint8_t index=0;



void setup()
{
  Serial.begin(115200);
  // 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_16MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!");
  else Serial.println("Error Initializing MCP2515...");
  CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
}
void loop()
{
  // send data: CANID[index], Standard CAN Frame, Data length = dlc[index], 'data' = array of data bytes to send
  uint8_t data[7];
  memcpy (data,&data_arr[index],7);
  byte sndStat = CAN0.sendMsgBuf(CANids[index], 0, dlcs[index], data);

  if (sndStat == CAN_OK) {
     Serial.print(CANids[index], HEX);
     Serial.println(" Message Sent Successfully!");
  } else {
    Serial.println("Error Sending Message...");
  }

  index = (index+1)%6; //loops 0-5
  
  delay(100); // send data per 100ms
}

hope that helps...

19:22:08.276 -> Entering Configuration Mode Successful!
19:22:08.276 -> Setting Baudrate Successful!
19:22:08.276 -> MCP2515 Initialized Successfully!
19:22:08.276 -> Error Sending Message...
19:22:08.322 -> Error Sending Message...
19:22:08.322 -> Error Sending Message...
19:22:08.369 -> Error Sending Message...
19:22:08.415 -> Error Sending Message...
19:22:08.462 -> Error Sending Message...
19:22:08.508 -> Error Sending Message...
19:22:08.508 -> Error Sending Message...

code modified-----
#include <mcp_can.h>
#include <SPI.h>

MCP_CAN CAN0(10); // Set CS to pin 10

void setup()
{
Serial.begin(115200);

// 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_16MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!");
else Serial.println("Error Initializing MCP2515...");

CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
}

const byte data1[] = {0x04, 0x0F, 0x30, 0x00, 0x00};
const byte data2[] = {0x07, 0x6B, 0x8B, 0x34, 0x64, 0x44, 0x15, 0xD8};

void loop()
{
byte sndStat = CAN0.sendMsgBuf(0x100, 0, 5, data1);
if(sndStat == CAN_OK){
Serial.println("Message Sent Successfully!");
} else {
Serial.println("Error Sending Message...");
}
delay(10);

delay(10);

CAN0.sendMsgBuf(0x22F, 0, 8, data2);
delay(10);

requires delay between two messages upto 10ms

Based on the version by @sherzaad it looks like the first byte of the hex data is the length of data and is sent separately.

You can get the length from the first byte of the array...

const byte data1[] = {0x04, 0x0F, 0x30, 0x00, 0x00};
CAN0.sendMsgBuf(0x014, 0, data1[0], &data1[1]);

const byte data2[] = {0x07, 0x6B, 0x8B, 0x34, 0x64, 0x44, 0x15, 0xD8};
CAN0.sendMsgBuf(0x22F, 0, data2[0], &data2[1]);

or just remove the length byte from the front of the array.

const byte data1[] = {0x0F, 0x30, 0x00, 0x00};
CAN0.sendMsgBuf(0x014, 0, sizeof data1, data1);

const byte data2[] = {0x6B, 0x8B, 0x34, 0x64, 0x44, 0x15, 0xD8};
CAN0.sendMsgBuf(0x22F, 0, sizeof data2, data2);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.