Go Down

Topic: Cheap & simple MCP2551/MCP2515 CAN BUS set up (Read 80560 times) previous topic - next topic


I understand the basic send and receive sketches, but I wish to send / receive a data structure rather than bytes.  The structure will be something like:
struct MsgType
  unsigned int NodeID;
  unsigned int NodeLoc;
  unsigned int DevType;
  byte DevID;
  byte DevStat;

MsgType  MsgData

After assigning values the data would be transmitted.  On receiving the data it would be translated back into a structure format.

All examples that I have seen on the Internet deal only with byte data.  Can a structure be transmitted and received, and can you please provide some sample code.

I am wanting to use this in a model railroad to transmit messages to operate various devices such as crossarms and servos.

Appreciate your assistance.


you fix those things with:

 typedef union {
    UCHAR array[sizeof(float)];
    float ebyte;
} E_float;

typedef union {
   UCHAR array[sizeof(int)];
   int Eint;
} E_int;

typedef union {
   UCHAR array[sizeof(UCHAR)];
   UCHAR Euchar;

in fact you need a converter visa versa to convert your structure


Hello friends,
I wanted to know how to turn on the brake lights through the canbus protocol. Please help me with this. I can use the can bus library from my arduino and mcp2515.


Hello baghernaseri. First of all: "You know that you should not do this, because it's illegal and risk to harm yourself and others!" Don't mess around with your CAR, it is not a playground.

Now, to the technical issues to solve. Due to the nature of CAN-Bus usage in cars, it is not a trivial task to produce messages which already exist. That means that most messages are repeated constantly in a fast period between 5 and 1000 ms, depending on the priority of the data it contains and the bus speed. If two nodes tries to send the same message (ID), they overlapp and produce CAN-Bus errors, which will shut down some systems sooner or later. Also the different signals inside the message will drive the receiver-module nuts and it may ignore them or shutdown itself.

The only chance to produce CAN messages at will it so put a filterdevice between the module to control and the CAN-Bus connector for it. This is some kinde of "man-in-the-middle-attack". A microcontroller must receive all signals from one interface (side) and pass them to the other unchanged. Only the requestet signales may be mangled/manipulated.

Next, i would doubt about that you cars brake lights are connected to any CAN-Bus! It is a critical system. In most cars the brake-pedal has a switch, which signals the brake to the central computer (BCM/GEM). There may also other module which signal a "brake" to the central computer like Adaptive Cruise Controls, or pedestrian protection systems. The BCM will then drive a lamp or LED with a direct wire to light. So if you don't own one of these new fancy cars, where everything is CAN connected, it would be the way i described.

Hope this helps you get things clearer.


Please do not PM me with thread based messages.  If your thoughts are worth responding,  the group should benefit from your insight.


  I'm working in a personal project with my car and I'm trying to get RPM information. For that I bought a Serial CAN-BUS Module based on MCP2551 and MCP2515 from SeeeStudio, at https://www.seeedstudio.com/Serial-CAN-BUS-Module-based-on-MCP2551-and-MCP2515-p-2924.html and I'm not getting information from my car. I'm using a teensyduino 3.1 and serial CAN Module.
  I asked for a help on seeed site but I didn't receive any help.
  My car is a Volkswagem Saveiro 2013 and on OBD-II conector doesn't have CAN-H and CAN-L cables, just K-MASS cable, and I'm catching CAN-H, CAN-L cables from instruments' panel.
  On Serial Monitor is showing that is sending information, but don't receive anything.
  Their library works with SoftwareSerial library to create aditional serial port but my teensyduino has three serial ports and I'm thinking that the problem is with their library.
  I'm connecting the module, tx on pin 7 and rx on pin 8 of teensy.
  Could you help me please?
  Thank you.

Go Up