Arduino CAN BUS monitoring tool

General discussion about interfacing with the CAN bus
Post Reply
latonita
What's hacking?
Posts: 15
Joined: 2015 Apr 15 06:58

Arduino CAN BUS monitoring tool

Post by latonita » 2015 May 18 14:36

Guys,

Just want to share my tool to sniff CAN bus using Arduino + CAN BUS Shield. Link on github: https://github.com/latonita/arduino-canbus-monitor. This is an implementation of CAN232 serial protocol. Use it with software that understands it.

First, I started to use CAN_BUS_Shield library to read data and that was okay. Then I realized I need a software to visualize the data - and I used excel. But then I understood I need to interact with the bus and I decided to do it in some standard way - and just made an implementation of serial can bus protocol initially proposed by Lawicel and their CAN232/CANUSB. There a many tools which can work with this protocol. In my studies I use free tool called CANHacker v.2.00.01 (I found working downloadable link on this page: http://www.canhack.net/viewtopic.php?f= ... ddeaec14b1)

Step 1. Get my arduino-canbus-monitor. Build and upload to arduino. (I tested this on Arduino Uno)
Step 2. Download CANHacker or another analyzer tool which support serial can232 protocol. Choose a proper COM port.
Step 3. Have fun! Monitor your can bus, save traces, send messages, do what your analyzer/visualizer software can.

This arduino-canbus-protocol tool is free - everybody is welcome to fork and enhance it. If you like the tool or have a question/proposal - just drop me a line here or on github.

TheMatrix
How the heck did I end up here?
Posts: 10
Joined: 2006 Feb 15 22:13

Re: Arduino CAN BUS monitoring tool

Post by TheMatrix » 2015 Dec 20 09:05

Seem very useful, Thanks for implementing this communication protocol. I will try it in my car.
I am also using Excel since years to sort my CAN frames. I was looking for a better inexpensive tool. Thanks for your work, and many thanks for Sharing.
Scrolling !!!!

User avatar
linuxkidd
Site Admin
Posts: 344
Joined: 2005 Jul 22 15:48
Location: Anywhere, USA
Contact:

Re: Arduino CAN BUS monitoring tool

Post by linuxkidd » 2015 Dec 21 00:57

latonita wrote:Guys,

Just want to share my tool to sniff CAN bus using Arduino + CAN BUS Shield. Link on github: https://github.com/latonita/arduino-canbus-monitor. This is an implementation of CAN232 serial protocol. Use it with software that understands it.

First, I started to use CAN_BUS_Shield library to read data and that was okay. Then I realized I need a software to visualize the data - and I used excel. But then I understood I need to interact with the bus and I decided to do it in some standard way - and just made an implementation of serial can bus protocol initially proposed by Lawicel and their CAN232/CANUSB. There a many tools which can work with this protocol. In my studies I use free tool called CANHacker v.2.00.01 (I found working downloadable link on this page: http://www.canhack.net/viewtopic.php?f= ... ddeaec14b1)

Step 1. Get my arduino-canbus-monitor. Build and upload to arduino. (I tested this on Arduino Uno)
Step 2. Download CANHacker or another analyzer tool which support serial can232 protocol. Choose a proper COM port.
Step 3. Have fun! Monitor your can bus, save traces, send messages, do what your analyzer/visualizer software can.

This arduino-canbus-protocol tool is free - everybody is welcome to fork and enhance it. If you like the tool or have a question/proposal - just drop me a line here or on github.
Hey Iatonita,
I'm using the Seeedstudio CAN-shield for my sniffing and prototyping.. I have however just picked up 3 of low cost SPI bus CAN adapters from amazon (link)


Wondering if you could help me determine the proper CNF register values to get 250kbps on an 8mhz clocked MCP2515 with the same timings as these provide for 16mhz:
#define MCP_16MHz_250kBPS_CFG1 (0x41)
#define MCP_16MHz_250kBPS_CFG2 (0xF1)
#define MCP_16MHz_250kBPS_CFG3 (0x85)

I've tried the bit timing calculator here (https://www.kvaser.com/support/calculat ... alculator/), but while I can sniff traffic using the first few settings, it causes comms issues with other nodes on the bus whereas the ones listed above for the 16mhz clocked chip do not.

Thanks to anyone who can help!
LK
If you can read this, the light is still red.

User avatar
linuxkidd
Site Admin
Posts: 344
Joined: 2005 Jul 22 15:48
Location: Anywhere, USA
Contact:

Re: Arduino CAN BUS monitoring tool

Post by linuxkidd » 2015 Dec 21 12:29

I seem to have determined the answer... Found in the MCP2515 datasheet, the full details of each config bit set by the CNF1|2|3 values.

At this time, the following seems to operate quite well:
#define MCP_8MHz_250kBPS_CFG1 (0x40)
#define MCP_8MHz_250kBPS_CFG2 (0xF1)
#define MCP_8MHz_250kBPS_CFG3 (0x85)

All of the params configured use multiples of a value called Tq, which is calculated from the 6 LSBs of the CNF1 ( aka Baud Rate Prescaler or BRP ). Tq is calculated as: 2 x (BRP + 1)/Fosc
Where Fosc = Frequency of the Oscillator.

The good values for 16Mhz have the BRP set to 1, thus:
2 x ( 1 + 1 ) / 16Mhz = Tq of 0.00000025

Since the clock on the linked CAN SPI adapter is 1/2 that of the CAN-Shield, I reduced the BRP to 0:
2 x ( 0 + 1 ) / 8Mhz = Tq of 0.00000025

YEAH! Stable operations on my bus.. :D

fwiw, I'm using this to hack the RV-C (Recreational Vehicle CAN bus) on my 2016 Allegro Bus 45OP Motorhome.. :) The good news is that RV-C spec is open and quite well documented. Which is a damn good thing, since they use extended CanID, and overload it with the command, priority, data byte count and source address. Would have been difficult to figure that out.

LK
If you can read this, the light is still red.

tandm87
How the heck did I end up here?
Posts: 2
Joined: 2016 Feb 01 22:12

Re: Arduino CAN BUS monitoring tool

Post by tandm87 » 2017 Aug 28 00:05

Nothing happens,

i have a test with Arduino Nano v3 with MCP 2515 CAN Interface & OBD2.
i have to connect Arduino Nano v3 with MCP 2515 CAN and connect MCP 2515 CAN Interface with OBD2 (on my car is Chevrolet Captiva 2008) by H-CAN to Pin 1, L-CAN to Pin4.

MCP2515 module : Image

Wiring Diagram: Image

and i use library MCP-can-lib-master : https://github.com/coryjfowler/MCP_CAN_lib

My code:

Code: Select all

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

long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128];                        // Array to store serial string

#define CAN0_INT 2                              // Set INT to pin 2
MCP_CAN CAN0(10);                               // Set CS to pin 10


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

 if (CAN0.begin(MCP_ANY, CAN_33K3BPS, MCP_16MHZ) == CAN_OK)
   Serial.println("MCP2515 Initialized Successfully!");
 else {
   while (true) {
     Serial.println("Error Initializing MCP2515...");
     delay(1000);
   }
 }
 CAN0.setMode(MCP_NORMAL);
 pinMode(CAN0_INT, INPUT);
 Serial.println("MCP2515 Library Receive Example...");
}

void loop()
{
 if (!digitalRead(CAN0_INT))                    
 {
   CAN0.readMsgBuf(&rxId, &len, rxBuf);      // Read data: len = data length, buf = data byte(s)

   if ((rxId & 0x80000000) == 0x80000000)
     sprintf(msgString, "Extended ID: 0x%.8lX  DLC: %1d  Data:", (rxId & 0x1FFFFFFF), len);
   else
     sprintf(msgString, "Standard ID: 0x%.3lX       DLC: %1d  Data:", rxId, len);

   Serial.print(msgString);

   if ((rxId & 0x40000000) == 0x40000000) {
     sprintf(msgString, " REMOTE REQUEST FRAME");
     Serial.print(msgString);
   } else {
     for (byte i = 0; i < len; i++) {
       sprintf(msgString, " 0x%.2X", rxBuf[i]);
       Serial.print(msgString);
     }
   }

   Serial.println();
 }


 if(CAN0.checkError() == CAN_CTRLERROR){
   Serial.print("Error register value: ");
   byte tempErr = CAN0.getError() & MCP_EFLG_ERRORMASK; // We are only interested in errors, not warnings.
   Serial.println(tempErr, BIN);
   
   Serial.print("Transmit error counter register value: ");
   tempErr = CAN0.errorCountTX();
   Serial.println(tempErr, DEC);
   
   Serial.print("Receive error counter register value: ");
   tempErr = CAN0.errorCountRX();
   Serial.println(tempErr, DEC);
 }
}
=============
And i have a problem when run, MCP2515 Initialized Successfully, but allways return length = 0 with ID = 0x000.
and with error check, serial return :

Code: Select all

Error register value: 1000
Transmit error counter register value:
Receive error counter register value: 128.
=> please help me what's wrong?

I had tried with canhack library, but nothing happens.
Please guide me how to check MCP2515 module.

User avatar
linuxkidd
Site Admin
Posts: 344
Joined: 2005 Jul 22 15:48
Location: Anywhere, USA
Contact:

Re: Arduino CAN BUS monitoring tool

Post by linuxkidd » 2017 Aug 28 12:22

Hello tandm87,
The board you're using is 8mhz clock, but you're trying to initialize it as 16mhz clock. See my adjustments above to modify the MCP library to allow 8mhz clock usage.

Thanks,
LK
If you can read this, the light is still red.

tandm87
How the heck did I end up here?
Posts: 2
Joined: 2016 Feb 01 22:12

Re: Arduino CAN BUS monitoring tool

Post by tandm87 » 2017 Aug 30 10:57

Thank @linuxkidd, let me try, thank again.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest