Read CAN message from a machine

Hello guys,

I'm trying to read a CAN message with Arduino, this message is sent by a machine.

When I prepare a message to send from an Arduino and prepare a second Arduino to read that message, ok I have no doubts.

But how do I read a message sent by a machine?

I already know the message I read using the "can analyzer", the ID is: 18FFEE8D, the DLC is 8.

How do I configure Arduino to read this message when it is sent by the machine?

can you help me?

you send ID and receive 8 bytes

like this?

i need to read the message the machine sends and not send a message to the machine.

Hi,
What is the machine?
What CanBus module are you using?
What speed is the CanBus?

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

Hello,

I don't know exactly which machine it is, but I can guarantee that it sends the message in the attached image:

ID

I'm using Arduino Nano + MCP2515

If I were to read a message sent by another arduino, I would use the code below:

#include <SPI.h>
#include <mcp2515.h>

struct can_frame canMsg;
MCP2515 mcp2515(10);


void setup() {
  Serial.begin(115200);
  
  mcp2515.reset();
  mcp2515.setBitrate(CAN_125KBPS);
  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++)  {  // print the data
      Serial.print(canMsg.data[i],HEX);
      Serial.print(" ");
    }

    Serial.println();      
  }
}

The speed is 250 kbps

Hi,

What do you mean by that?

What do you expect your project to do?

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

mcp2515.setBitrate(CAN_250KBPS); 

?

I think it doesn't matter which machine it is, what matters to me is that it sends this CAN message and I need to read it...

But that's ok, the machine is a "Caterpillar cat 320" and I need to read this CAN message to convert it to RS232...

So my problem is: How do I read this message?

if you do a web search for arduino canbus you will get get plenty of links

1 Like

Hi Horace, you're right....

And the code that I posted here is one of those many examples and it works very well, but on a bus between Arduinos, because I prepare the "Arduino 2" to receive the message sent by the "Arduino 1", that is, I configure the 2 devices. ...

In my case, I need to read the message that comes from the machine (Caterpila CAT320), if it were as simple as the examples available, I wouldn't need a CANanalyzer to read the messages from the machine, I would simply plug the Arduino and that's it.

But I already managed to move forward, I discovered the messages sent by (as posted above), now I need to know what I need to change in the code below so that the Arduino receives this message....

#include <SPI.h>
#include <mcp2515.h>

struct can_frame canMsg;
MCP2515 mcp2515(10);


void setup() {
  Serial.begin(115200);
  
  mcp2515.reset();
  mcp2515.setBitrate(CAN_250KBPS);
  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++)  {  // print the data
      Serial.print(canMsg.data[i],HEX);
      Serial.print(" ");
    }

    Serial.println();      
  }
}

if the canbus is connected correctly and the can speed is correct you should see messages on the serial monitor consisting of ID, message length then the data (up to 8 bytes)
do you see anything - if nothing try changing the CAN speed ?
when debugging CAN I find a USB CAN dongle which plugs into a PC useful for chacking network operation and debugging
can you give us a schematic of your wiring?

Hi,

Have you Googled;

caterpillar canbus protocol

Do you know the PIDs that you want to monitor?

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

Hi,
If the PIDs are ODB2 then this may help.
Google;

arduino reading canbus PIDs

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

Hi guys, I managed to solve my problem.

To read messages on the CAN network of a machine, just use the command below:

 if (mcp2515.checkReceive())
    {
        mcp2515.readMessage(&canMsg1);

And if the ID is already known, just filter as below:

 if (canMsg1.can_id == 0x80FFEA28)

And we can still filter by the data as below:

if (canMsg1.data[i] == 0x01)

In the end, this is my complete code:

#include <Arduino.h>
#include <SPI.h>
#include <mcp2515.h>
#include <SoftwareSerial.h>

SoftwareSerial serial_tel (7, 8); //Define RX/TX por Software

struct can_frame canMsg1;

MCP2515 mcp2515(10);

void setup() 
{

  Serial.begin(9600);
  serial_tel.begin(9600);

  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS);
  mcp2515.setNormalMode();

  Serial.println("------- SERIAL DISPONIVEL --------");
}

void loop() 
{
    if (mcp2515.checkReceive())
    {
        mcp2515.readMessage(&canMsg1);

        if (canMsg1.can_id == 0x80FFEA28)
          {
            int i = 0;

            if (canMsg1.data[i] == 0x01)
            {
              Serial.println("AT+GTDAT=9875,2,,PEDESTRE PERIGO,0,,,,FFFF$");              
              serial_tel.println("AT+GTDAT=9875,2,,PEDESTRE PERIGO ,0,,,,FFFF$");
              delay(10000);
            }

            if (canMsg1.data[i] == 0x02)
            {
              Serial.println("AT+GTDAT=9875,2,,PEDESTRE CUIDADO ,0,,,,FFFF$");
              serial_tel.println("AT+GTDAT=9875,2,,PEDESTRE CUIDADO ,0,,,,FFFF$");
              delay(10000);
            }      
    }
  }
}

This is my Caterpilla....

Caterpillar 320??????????????????????

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