CAN Bus Seed shield Communication

Hey guys, so CAN-BUS is a pretty new topic for me thats why i asked chatgpt to give me an code that i can then improve but it dosent work could somebody help me out pls. (that is the sender part)

#include <SPI.h>

#define CAN_ID 0x01 // die eindeutige ID des Senders
#define DELAY_TIME 500 // Zeit zwischen den Nachrichten

SPI can;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // Warte, bis eine serielle Verbindung aufgebaut ist
  }

  if (can.init(CAN_500KBPS) == 0) {
    Serial.println("CAN-Bus-Initialisierung erfolgreich.");
  } else {
    Serial.println("Fehler bei der CAN-Bus-Initialisierung.");
  }
}

void loop() {
  unsigned char data[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; // Die Daten, die gesendet werden sollen

  CANMessage message;
  message.id = CAN_ID;
  message.len = 8;
  memcpy(message.data, data, sizeof(data));

  can.sendMsgBuf(message.id, 0, message.len, message.data);
  Serial.println("Nachricht gesendet.");

  delay(DELAY_TIME);
}

Hi, @david02ktm
Why didn't you look at the examples given with the various canbus libraries?

Does the chatGPT compile and load?

What was wrong with this solution?

Tom... :smiley: :+1: :coffee: :australia:

Hey @TomGeorge, ou i didnt saw that there are example codes that come with the labraries thank for that hint.
Anyway i would like to know what wrong with it to learn from it.
The error code i get with this is:
SPI' does not name a type

Hi,
Look up the example code for CAN-BUS Shield library, it looks like it is for the seeed shield.

The seeed site even gives you the link to the library.

Tom.. :smiley: :+1: :coffee: :australia:

Hi, im using this Library.

Hi,

What library?

Tom... :smiley: :+1: :coffee: :australia:

Is the code you've shared the full program ?

If so you haven't included any library except SPI. You need to include a CanBus library.

I always use mcp_can which works great with the MCP2515/MCP2551 combo usually found on the seeed boards.

Regards,

Gareth

Hey i tried this code know but still get the same error.

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

#define CAN_CS_PIN 10  // der CS-Pin des MCP2515 auf dem CAN-Shield

MCP_CAN can(CAN_CS_PIN);

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // Warte, bis eine serielle Verbindung aufgebaut ist
  }

  if (can.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) {
    Serial.println("CAN-Bus-Initialisierung erfolgreich.");
  } else {
    Serial.println("Fehler bei der CAN-Bus-Initialisierung.");
  }
}

void loop() {
  unsigned char data[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; // Die Daten, die gesendet werden sollen

  can.sendMsgBuf(0x123, 0, 8, data);
  Serial.println("Nachricht gesendet.");

  delay(500);
}

Error:
cannot declare variable 'can' to be of abstract type 'MCP_CAN'

HI,
Try this a direct example, see if it compiles.
What IDE version are you using?
What OS?

// CAN Send Example
//

#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
}

/*********************************************************************************************************
  END FILE
*********************************************************************************************************/

Tom.... :smiley: :+1: :coffee: :australia:

Hi,
Your code compiles for me.
UNO
IDE Version 1.8.19
Windoze 10.

Tom... :smiley: :+1: :coffee: :australia:

i get this error:
cannot declare variable 'CAN0' to be of abstract type 'MCP_CAN'
with your code too.
Im using windows 10 and IDE 1.8.19

what is wrong with my pc :frowning:

Hi,
Check that the library does not need updating.

Use the library manager to check.

Tom... :smiley: :+1: :coffee: :australia:

Hi Tom, now it works had to do an update thank u so much <3

:+1: :+1: :+1: :+1: :+1:

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