Go Down

Topic: Cheap & simple MCP2551/MCP2515 CAN BUS set up (Read 68845 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.


Go Up