Code Monkey home page Code Monkey logo

arduino_can_bus_mcp2515's People

Contributors

arachnid avatar coryjfowler avatar reeedstudio avatar sidney2007 avatar stephen1874 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

arduino_can_bus_mcp2515's Issues

Second Mask and Filters Not Working

I am pulling CAN messages off a vehicle based off their CAN ID. I am able to get the first mask to work which corresponds to the first 2 filters. The issue is with the second mask and second set of filters (3-6). Is there something in the library I need to change to enable both masks?

I have attached code for reference.

Thank you.

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

// ************************ Section Below for LCD ********************************
//********************************************************************************
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR 0x27 // Define I2C Address where the PCF8574A is
// Address can be changed by soldering A0, A1, or A2
// Default is 0x27

// map the pin configuration of LCD backpack for the LiquidCrystal class
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7

LiquidCrystal_I2C lcd(I2C_ADDR,
En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin,
BACKLIGHT_PIN, POSITIVE);

// ************************* End LCD Setup *****************************************
//**********************************************************************************

MCP_CAN CAN0(10); // Set CS to pin 10

// ***************** Variable declarations *********************************
double Special1;
double Special2;

float RWHL_Spd; // average of rears engine Veh_V_ActlEng sb 48, len 16, res 0.01 offset = 0 kph 0x202 EngVehicleSpThrottle2

// Trail Control Section

float TrlCtlSetSpd;
int BangBit; // Tom: message 0x76, start bit 32, length 1 forma name BrkCtrFnd_B_Stat
int lsmcstate;
int MSR_Status; // active = 1
int rtt;
int RPMStatus;
int TrailCtlStat = 0;
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128]; // Array to store serial string

void setup()

{
//******************** Section for LCD Initialization ****************************

lcd.begin(20,4); // 20 columns by 4 rows on display

lcd.setBacklight(HIGH); // Turn on backlight, LOW for off

// *********************** Misc Set up *******************************************
pinMode(8,OUTPUT);//Setup Pin 8 for and LED for BangBit:center found by Brakes

// *******************************************************************************
Serial.begin(115200);
if(CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.print("MCP2515 Init Okay!!\r\n");
else Serial.print("MCP2515 Init Failed!!\r\n");
pinMode(2, INPUT); // Setting pin 2 for /INT input

CAN0.init_Mask(0,0,0x07FF0000); // Init first mask...
CAN0.init_Filt(0,0,0x04150000); // Init first filter...
CAN0.init_Filt(1,0,0x02040000); // Init second filter...

CAN0.init_Mask(1,0,0x07FF0000); // Init second mask...
CAN0.init_Filt(2,0,0x02020000); // Init first filter...
CAN0.init_Filt(3,0,0x02030000); // Init second filter...
CAN0.init_Filt(4,0,0x02050000); // Init third filter...
CAN0.init_Filt(5,0,0x00760000); // Init fouth filter...

CAN0.setMode(MCP_NORMAL);

lcd.clear();
lcd.setCursor(0,0);
lcd.print("Trail Control");
lcd.setCursor(0,1);
lcd.print("Tom Box CAN Display");
lcd.setCursor(0,2);
lcd.print("Version V12");
lcd.setCursor(0,3);
lcd.print("Powered by Spartans!");
delay(2000);
lcd.clear();

}

void loop()

{
if(!digitalRead(2)) // If pin 2 is low, read receive buffer
{
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
// Serial.print("ID: ");
// Serial.print(rxId, HEX);
// Serial.print(" Data: ");
// for(int i = 0; i<len; i++) // Print each byte of the data
// {
// if(rxBuf[i] < 0x10) // If data byte is less than 0x10, add a leading zero
// {
// Serial.print("0");
// }
// Serial.print(rxBuf[i], HEX);
// Serial.print(" ");
// }
// Serial.println();

unsigned int canId = (rxId);
float CAN_Msg=canId;

// *******************************Trail Control *************************************************
// message Brake 0x415 only on HS2 Pins 3 and 11

if (CAN_Msg == 0x415)// LsmcBrkDecel_D_Stat SB=32, length = 3, SED 1110 0000
{
lsmcstate = rxBuf[4]>>5;
}
if (CAN_Msg == 0x41E) // BrakeSysFeatures_3 TrailCtl_D_Stat SB=48 Len = 3 HS2 only
{
Special1 = rxBuf[6]>>4;
TrailCtlStat = Special1;//0 = off, 1 = enabled, 2 = active,
}

// if (CAN_Msg == 0x43A) // // SB=51 len 3 on HS1, This MUST be commented out if connected to HS2
// {
// Special1 = rxbuf[6]>>2 & 0x7;
// TrailCtlStat = Special1;//0 = off, 1 = enabled, 2 = active,
// }
if (CAN_Msg == 0x204)// EngAout_N_Actl SB27, length = 13
{
RPMStatus = ((((rxBuf[3] & 0x1F) * 256) + rxBuf[4]) * 2); //select what bytes you want to read
}

//Find Center****************

if (CAN_Msg == 0x76) //msg 0x76 is BrakeSnData_2 "BangBit" BrkCtrFnd_B_Stat, position=32, len=1 bits0-63
{
BangBit = (rxBuf[4] & 0x80)>>7;//uses bitwise AND not boolean AND which is &&, bit shift moves the bit from position 7 to 0

     // LED is ON until Center is Found, it is too bright to leave on the entire drive cycle at night
      if (BangBit == 1)
        {
          lcd.setCursor(0,1);
          lcd.print("          ");
          lcd.setCursor(0,1);
          lcd.print("Found");
        }
       else
        { 
          lcd.setCursor(0,1);
          lcd.print("          ");
          lcd.setCursor(0,1);
          lcd.print("Not Found");
        } 
    }

//******************************End Find Center

// ******************** get Trail Control Set Speed and Display ******************************

if (CAN_Msg == 0x202) // EngVehicleSpThrottle2_HS1
// Veh_V_ActlEng sb 48, len 16, res 0.01 offset = 0 kph
// Veh_V_RqCcSet SB=39 length = 9 res = 0.5
// 0-7,8-15,16-23,24-31,32-39,40-47,48-55,56-63
{
// Get Trail Control Set Speed
Special1 = (rxBuf[4] & 0x01); // grab bit 39
Special2 = rxBuf[5];
TrlCtlSetSpd = round((Special1256 + Special2) * 0.5 * 0.621371); // kph to mph 0.621
TrlCtlSetSpd = ((Special1
256 + Special2) * 0.5 * 0.621371); // kph to mph 0.621
}

//*********************** End Trail Control Set Speed******************************************

// *********** DEBUG *****************PRINTS OUT RAW CAN MESSAGE BYTES *************************
Serial.print("Get data from ID: ");
Serial.println(canId, HEX);
for(int i = 0; i<len; i++) // print the data
{
Serial.print(rxBuf[i], HEX);
Serial.print("\t"); // tab
}

// ****************** END DEBUG*****************************************************************

//LOGIC FOR RTT determination*****************

if (MSR_Status == 0 && TrailCtlStat == 0) // if either is set don't dsiplay off, X1 uses MSR_Status, M1 uses TrailCtlStat
{
rtt = 0; //Display "OFF"
}

if ((lsmcstate == 3 && TrlCtlSetSpd == 0) || (TrailCtlStat == 1))// enabled not active ** left of the OR X1, right of the OR M1
{
rtt = 2; // display "ENABLED - NOT ACTIVE"
}

if ((lsmcstate < 4 && lsmcstate > 0 && TrlCtlSetSpd > 0) || (TrailCtlStat == 2 && RWHL_Spd < 33)) //active ** left of the OR X1, right of the OR M1
{
rtt = 1; //display "ENABLED - ACTIVE"
}

if ((lsmcstate > 3 && MSR_Status == 1) || (TrailCtlStat == 2 && RWHL_Spd >= 33)) //over threshold ** left of the OR X1, right of the OR M1
{
rtt = 3; //display "Overspeed"
}

//END LOGIC FOR RTT DETERMINATION******

//Determine Vehicle speed****

Special1 = rxBuf[6];
Special2 = rxBuf[7];
RWHL_Spd = (( Special1 *256) + Special2) / 100; //kph

//End determine vehicle speed**

//*********************** Trail Control Set Speed *********************************

lcd.setCursor(0,0);
lcd.print("Set Speed");
lcd.setCursor(11,0);
lcd.print(" ");
lcd.setCursor(0,3);
lcd.print(" ");
lcd.setCursor(11,0);
lcd.print(TrlCtlSetSpd,0);
lcd.setCursor(0,3);
lcd.print(RPMStatus);

// ***************** Trail Control Status ***************************

lcd.setCursor(0,2);
if (rtt == 0)
{
lcd.print("OFF ");
}

if (rtt == 1)
  {
    lcd.print("ACTIVE              ");
  }
    
if (rtt == 2)
  {
    lcd.print("ENABLED - NOT ACTIVE");
  }
           
if (rtt ==3)
{
    lcd.print("OVERSPEED              ");
}

// debug items
//lcd.setCursor(0,3);
//lcd.print(Special1);
//lcd.setCursor(10,3);
//lcd.print(TrailCtlStat);
//lcd.setCursor(0,3);
//lcd.print(TrailCtlStat);
//lcd.setCursor(10,3);
//lcd.print(rtt);
//lcd.setCursor(0,1);
//lcd.print(lsmcstate);

}
}

`

Error Sending Messages...

Hi, I am having a trouble ... as if am using CAN shield in Loopback mode then I am receiving the msgs transmitted which confirms that all my connections are fine :

Enter Configuration mode successful!
Setting Baudrate Successful!
MCP2515 Initialized Successfully!
MCP2515 Library Loopback Example...
Message Sent Successfully!
Extended ID: 0x00000100 DLC: 8 Data: 0xAA 0x55 0x01 0x10 0xFF 0x12 0x34 0x56
Message Sent Successfully!
Extended ID: 0x00000100 DLC: 8 Data: 0xAA 0x55 0x01 0x10 0xFF 0x12 0x34 0x56
Message Sent Successfully!
Extended ID: 0x00000100 DLC: 8 Data: 0xAA 0x55 0x01 0x10 0xFF 0x12 0x34 0x56
Message Sent Successfully!
Extended ID: 0x00000100 DLC: 8 Data: 0xAA 0x55 0x01 0x10 0xFF 0x12 0x34 0x56
Message Sent Successfully!
Extended ID: 0x00000100 DLC: 8 Data: 0xAA 0x55 0x01 0x10 0xFF 0x12 0x34 0x56
Message Sent Successfully!
....

But I am getting :

Enter Configuration mode successful!
Setting Baudrate Successful!
MCP2515 Initialized Successfully!
Error Sending Message...
Error Sending Message...
Error Sending Message...
Error Sending Message...
Error Sending Message...
Error Sending Message...

if using in 'Normal mode' when i tried to send data 'using CAN_send' sketch.... i am using CoryJFowler's library for can shield..... Can anyone please help what wrong with it????????

ext not initialized in sendMsgBuf

Leaving this here as there is no Issues tab on Cory Fowlers fork, but it is relating to that v1.5 fork.

Line 1070 initializes rtr, but not ext, often leaving ext unequal 0 unintended.
Loopback example returned extended frames. Suggest to change that line to

INT8U ext = 0, rtr = 0;

Thank you for the great work.

mcp_can.h no such file or directory error

I've been working on a project using an Arduino Uno with success but am trying to switch over to a Mega as I need more inputs.

I'm unable to establish communication on the Mega so I've been trying to use this library however when I compile I get the following error.

multiMotorPosCtrl:10:10: fatal error: mcp_can.h: No such file or directory
#include <mcp_can.h>
^~~~~~~~~~~
compilation terminated.
exit status 1
mcp_can.h: No such file or directory

Anyone have advice?

My code is available in the below link:
https://drive.google.com/file/d/1GyWThPyuqSBLzfnIKNLt5sGKc8tEOWh4/view?usp=sharing

Drive(control) Motor With MCP2515

I have a motor with a "can-H can-L" connection. I want to drive(control) this engine with MCP2515 (step by step if possible). How can I do it? Do you know any documentation on this subject?

Filtering one discret ID

Hello,
I do not know how to recieve only messages with the ID 522 (dec). Can you help me, how to set filter and mask?

255 = 0xFF = 11111111 is it the mask? I do not know.....
522 = 1000001010 (maybe it is the filter? but which filter?)???

I know that I have to use
init_Mask(INT8U num, INT8U ext, INT32U ulData);
init_Filt(INT8U num, INT8U ext, INT32U ulData);

But what I have to set to num, ext and uIData??????

Speed selection is incorrect.

I took my "non-working" into work and tested it with my Kvaser and Vector hardware.

Setting 500 kbps on the Arduino is actually transmitting at 250 kbps.

On both an Arduino Uno R3 with SeeedStudio CAN Shield v1.0 and an Arduino Nano w/standalone SPI CAN board

spelling error in repo name

It's a trivial issue, and I'm hoping easily corrected: the repo name should have a d in the word Ar_d_uino

error compiling for ESP32 dev module

Error compiling for ESP32 board. ESP32 Arduino core 2.0.6. Windows 11 64bit. Arduino IDE 1.8.19

Error massage from Arduino:

C:\Users\CredoSense Desktop 2\Documents\Arduino\libraries\Aruino_CAN_BUS_MCP2515-master\mcp_can.cpp: In member function 'byte MCP_CAN::setMsg(long unsigned int, byte, byte, byte, byte*)':

C:\Users\CredoSense Desktop 2\Documents\Arduino\libraries\Aruino_CAN_BUS_MCP2515-master\mcp_can.cpp:751:47: error: no matching function for call to 'min(byte&, int)'

 dta_len     = min(len, MAX_CHAR_IN_MESSAGE);

                                           ^

In file included from c:\users\credosense desktop 2\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch5\xtensa-esp32-elf\include\c++\8.4.0\algorithm:62,

             from C:\Users\CredoSense Desktop 2\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.6\cores\esp32/Arduino.h:162,

             from C:\Users\CredoSense Desktop 2\Documents\Arduino\libraries\Aruino_CAN_BUS_MCP2515-master\mcp_can_dfs.h:27,

             from C:\Users\CredoSense Desktop 2\Documents\Arduino\libraries\Aruino_CAN_BUS_MCP2515-master\mcp_can.h:27,

             from C:\Users\CredoSense Desktop 2\Documents\Arduino\libraries\Aruino_CAN_BUS_MCP2515-master\mcp_can.cpp:47:

c:\users\credosense desktop 2\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch5\xtensa-esp32-elf\include\c++\8.4.0\bits\stl_algo.h:3456:5: note: candidate: 'template<class _Tp, class _Compare> _Tp std::min(std::initializer_list<_Tp>, _Compare)'

 min(initializer_list<_Tp> __l, _Compare __comp)

 ^~~

c:\users\credosense desktop 2\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch5\xtensa-esp32-elf\include\c++\8.4.0\bits\stl_algo.h:3456:5: note: template argument deduction/substitution failed:

C:\Users\CredoSense Desktop 2\Documents\Arduino\libraries\Aruino_CAN_BUS_MCP2515-master\mcp_can.cpp:751:47: note: mismatched types 'std::initializer_list<_Tp>' and 'unsigned char'

 dta_len     = min(len, MAX_CHAR_IN_MESSAGE);

                                           ^

In file included from c:\users\credosense desktop 2\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch5\xtensa-esp32-elf\include\c++\8.4.0\algorithm:62,

             from C:\Users\CredoSense Desktop 2\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.6\cores\esp32/Arduino.h:162,

             from C:\Users\CredoSense Desktop 2\Documents\Arduino\libraries\Aruino_CAN_BUS_MCP2515-master\mcp_can_dfs.h:27,

             from C:\Users\CredoSense Desktop 2\Documents\Arduino\libraries\Aruino_CAN_BUS_MCP2515-master\mcp_can.h:27,

             from C:\Users\CredoSense Desktop 2\Documents\Arduino\libraries\Aruino_CAN_BUS_MCP2515-master\mcp_can.cpp:47:

c:\users\credosense desktop 2\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch5\xtensa-esp32-elf\include\c++\8.4.0\bits\stl_algo.h:3450:5: note: candidate: 'template _Tp std::min(std::initializer_list<_Tp>)'

 min(initializer_list<_Tp> __l)

 ^~~

c:\users\credosense desktop 2\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch5\xtensa-esp32-elf\include\c++\8.4.0\bits\stl_algo.h:3450:5: note: template argument deduction/substitution failed:

C:\Users\CredoSense Desktop 2\Documents\Arduino\libraries\Aruino_CAN_BUS_MCP2515-master\mcp_can.cpp:751:47: note: mismatched types 'std::initializer_list<_Tp>' and 'unsigned char'

 dta_len     = min(len, MAX_CHAR_IN_MESSAGE);

                                           ^

In file included from c:\users\credosense desktop 2\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch5\xtensa-esp32-elf\include\c++\8.4.0\algorithm:61,

             from C:\Users\CredoSense Desktop 2\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.6\cores\esp32/Arduino.h:162,

             from C:\Users\CredoSense Desktop 2\Documents\Arduino\libraries\Aruino_CAN_BUS_MCP2515-master\mcp_can_dfs.h:27,

             from C:\Users\CredoSense Desktop 2\Documents\Arduino\libraries\Aruino_CAN_BUS_MCP2515-master\mcp_can.h:27,

             from C:\Users\CredoSense Desktop 2\Documents\Arduino\libraries\Aruino_CAN_BUS_MCP2515-master\mcp_can.cpp:47:

c:\users\credosense desktop 2\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch5\xtensa-esp32-elf\include\c++\8.4.0\bits\stl_algobase.h:243:5: note: candidate: 'template<class _Tp, class _Compare> const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'

 min(const _Tp& __a, const _Tp& __b, _Compare __comp)

 ^~~

c:\users\credosense desktop 2\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch5\xtensa-esp32-elf\include\c++\8.4.0\bits\stl_algobase.h:243:5: note: template argument deduction/substitution failed:

C:\Users\CredoSense Desktop 2\Documents\Arduino\libraries\Aruino_CAN_BUS_MCP2515-master\mcp_can.cpp:751:47: note: deduced conflicting types for parameter 'const _Tp' ('unsigned char' and 'int')

 dta_len     = min(len, MAX_CHAR_IN_MESSAGE);

                                           ^

In file included from c:\users\credosense desktop 2\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch5\xtensa-esp32-elf\include\c++\8.4.0\algorithm:61,

             from C:\Users\CredoSense Desktop 2\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.6\cores\esp32/Arduino.h:162,

             from C:\Users\CredoSense Desktop 2\Documents\Arduino\libraries\Aruino_CAN_BUS_MCP2515-master\mcp_can_dfs.h:27,

             from C:\Users\CredoSense Desktop 2\Documents\Arduino\libraries\Aruino_CAN_BUS_MCP2515-master\mcp_can.h:27,

             from C:\Users\CredoSense Desktop 2\Documents\Arduino\libraries\Aruino_CAN_BUS_MCP2515-master\mcp_can.cpp:47:

c:\users\credosense desktop 2\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch5\xtensa-esp32-elf\include\c++\8.4.0\bits\stl_algobase.h:195:5: note: candidate: 'template const _Tp& std::min(const _Tp&, const _Tp&)'

 min(const _Tp& __a, const _Tp& __b)

 ^~~

c:\users\credosense desktop 2\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch5\xtensa-esp32-elf\include\c++\8.4.0\bits\stl_algobase.h:195:5: note: template argument deduction/substitution failed:

C:\Users\CredoSense Desktop 2\Documents\Arduino\libraries\Aruino_CAN_BUS_MCP2515-master\mcp_can.cpp:751:47: note: deduced conflicting types for parameter 'const _Tp' ('unsigned char' and 'int')

 dta_len     = min(len, MAX_CHAR_IN_MESSAGE);

                                           ^

Using library SPI at version 2.0.0 in folder: C:\Users\CredoSense Desktop 2\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.6\libraries\SPI

Using library Aruino_CAN_BUS_MCP2515-master at version 1.0.0 in folder: C:\Users\CredoSense Desktop 2\Documents\Arduino\libraries\Aruino_CAN_BUS_MCP2515-master

exit status 1

Error compiling for board ESP32 Dev Module.

Filters and Masks

hi, i am new to github so i am not quite sure this is the best way to comunicate. My question is,
can you make a exapmle where you would use fliters and masks for CAN massages. For example, you have two arduino's
comunicating via CAN bus, one is only sending messages (messages with extended ID's) and the other is supposed to
read only messages with specific ID's.

Thanks

Setting Filters and Masks

I have been playing around with setting Masks and Filters. It took me a while to work out how to make it work correctly, but there is an omission in the code for setting the standard mask and filter.
The 'standard' frame mask and filter can be used to filter on ID and also the first 2 data bytes. Unfortunately the code in mcp_can.cpp doesn't allow for filtering on the data bytes, only on the ID. Here is the current code.

void MCP_CAN::mcp2515_write_id( const INT8U mcp_addr, const INT8U ext, const INT32U id )
{
    uint16_t canid;
    INT8U tbufdata[4];

    canid = (uint16_t)(id & 0x0FFFF);

    if ( ext == 1)
    {
        tbufdata[MCP_EID0] = (INT8U) (canid & 0xFF);
        tbufdata[MCP_EID8] = (INT8U) (canid >> 8);
        canid = (uint16_t)(id >> 16);
        tbufdata[MCP_SIDL] = (INT8U) (canid & 0x03);
        tbufdata[MCP_SIDL] += (INT8U) ((canid & 0x1C) << 3);
        tbufdata[MCP_SIDL] |= MCP_TXB_EXIDE_M;
        tbufdata[MCP_SIDH] = (INT8U) (canid >> 5 );
    }
    else
    {
        tbufdata[MCP_SIDH] = (INT8U) (canid >> 3 );
        tbufdata[MCP_SIDL] = (INT8U) ((canid & 0x07 ) << 5);
        tbufdata[MCP_EID0] = 0;
        tbufdata[MCP_EID8] = 0;
    }
    mcp2515_setRegisterS( mcp_addr, tbufdata, 4 );
}

I have gotten around this by modifying the code and adding ext=2. Here is my modified code

void MCP_CAN::mcp2515_write_id( const INT8U mcp_addr, const INT8U ext, const INT32U id )
{
    uint16_t canid;
    INT8U tbufdata[4];

    canid = (uint16_t)(id & 0x0FFFF);

    if ( ext == 1) 
    {
        tbufdata[MCP_EID0] = (INT8U) (canid & 0xFF);
        tbufdata[MCP_EID8] = (INT8U) (canid >> 8);
        canid = (uint16_t)(id >> 16);
        tbufdata[MCP_SIDL] = (INT8U) (canid & 0x03);
        tbufdata[MCP_SIDL] += (INT8U) ((canid & 0x1C) << 3);
        tbufdata[MCP_SIDL] |= MCP_TXB_EXIDE_M;
        tbufdata[MCP_SIDH] = (INT8U) (canid >> 5 );
    }
    else if ( ext == 2) 
    {
        tbufdata[MCP_EID0] = (INT8U) (canid & 0xFF);
        tbufdata[MCP_EID8] = (INT8U) (canid >> 8);   
        canid = (uint16_t)(id >> 16);         
        tbufdata[MCP_SIDH] = (INT8U) (canid >> 3 );
        tbufdata[MCP_SIDL] = (INT8U) ((canid & 0x07 ) << 5);
    }
    else
    {
        tbufdata[MCP_SIDH] = (INT8U) (canid >> 3 );
        tbufdata[MCP_SIDL] = (INT8U) ((canid & 0x07 ) << 5);
        tbufdata[MCP_EID0] = 0;
        tbufdata[MCP_EID8] = 0;
    }
    mcp2515_setRegisterS( mcp_addr, tbufdata, 4 );
}

Can this fix be added to the code, or some similar modification be made?

Probably invalid transmission check?

Hi,
In mcp_can.cpp, line 707 there's a check if the message has been transmitted:

res1= mcp2515_readRegister(txbuf_n); /* read send buff ctrl reg */
res1 = res1 & 0x08;

but shouldn't it read txbuf_n - 1 instead of txbuf_n since txbuf_n will be SIDH address rather than CTRL address?

MCP_CAN on ESP8266 NodeMCU - SPI not correct?

Hello,

on an Arduino Uno i succesfully realized a MCP2515 Shield running perfectly.
Now i am using a ESP8266 with the following SPI Devices:
1x IL9341 TFT, 1x 74HC595 Expander (4x74HC595) and an MCP2515 CAN BUS Shield.

TFT is working fine, also the 74HC595's , the MCP2515 works fine also,
BUT: There is a problem using the 74HC595 AND the MCP2515 together!
If i don't use the MCP, the 74HC595 are working fine, if the MCP2515 is also running, there are always flickering (on the output-LEDs) of the 74HC595!
I can fix it with using spi.begin and spi.end in the 74HC595 Part, but then the MCP is not working anymore!
Is there something missing initializing the SPI on the MCP_CAN Library?
It would be the right way for a spi-device to begin and end, but the mcp_can seems not to do so!

Please can you help me?

regards,
Wolfram.

Install does not work in Windows 7

How is this library actually installed?
On Windows 7 (64 bit) (Arduino 1.6.1)
"MySketches/libraries" does not exist
Default place for sketches is:
"C:\Users\Documents\Arduino"
And imported library (Sketch | Import Library... | Add Library..." is placed in:
"C:\Users\Documents\Arduino\libraries\CAN_BUS_Shield-master"
While most libraries are in:
"C:\Program Files (x86)\Arduino\libraries"
Whinges for ever about missing .h files one after another which can individually be fixed by copying into the folder "CAN_BUS_Shield-master"

(Am Arduino novice.)

RTR code

How to send RTR request with can bus?

Required to Reinit Driver Consistently

In my application, I am required to read and store up to 20 CAN IDs that are sent as a burst message. Is this library able to handle that?

I find that after reading two of the CAN messages (ID + Data), I am unable to read anymore unless I reinitialize the driver, after which it can only read 2 and then stops. This leads me to missing any other CAN message in the burst. Is this a limitation of the driver?

Identifying Car Data MCP2515

Hello there

I am working on this project to read car data and sending it to cloud ,and found this library to read car data via OBD. I was wondering about the car data that I recieved my car is a Proton Exora 2014 and the data that I recieved is weird so to say here is the data I recieved https://github.com/coryjfowler/MCP_CAN_lib. I'm using MCP2515 and ESP32 TTGO CALL SIM800 to read and send the data.

And for starters I just wanted to detect Car RPM and I assume that RXid is 0x310 (knowing this is the only value that changes the moment the engine started) which is different from the normal 7DF / 7E8 request

I'm following the example to read RPM from @coryjfowler here is the code

/* OBD2 TACHO
   By moty22.co.uk
   Lib written By: Cory J. Fowler  December 20th, 2016
   https://github.com/coryjfowler/MCP_CAN_lib
*/

#include <mcp_can.h>
#include <SPI.h>
#define standard 1  //standard is 11 bits
#define txID 0x310  //0x7E9

unsigned long rxId;
byte len;
byte rxBuf[8];

unsigned char txData[8] = {0x02, 0x01, 0x0C, 0x55, 0x55, 0x55, 0x55, 0x55}; //PID to request 010C

MCP_CAN CAN0(13);                                // Set CAN0 CS to pin 10

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

  // Initialize MCP2515 running at baudrate of 500kb/s . If your can board has 16MHz crystal change the setting.
  if (CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_8MHZ) == CAN_OK)
    Serial.println("Initialized Successfully!");
  else
    Serial.println("**Error Initializing**");

  CAN0.setMode(MCP_NORMAL);                          // Set operation mode to normal so the MCP2515 sends acks to received data.

}

void loop()
{
  byte back = 0;
  unsigned int rpm = 100;

  delay(500);
  CAN0.sendMsgBuf(txID, 8, txData); //request data from ECU

  for (byte i = 0; i < 20; ++i)
  { //get 20 times unless reply arrived from the ECU
    CAN0.readMsgBuf(&rxId, &len, rxBuf);     // Get CAN data - 03 41 0C 3D 0F
    if (rxId == 0x310) {
      Serial.print(rxId, HEX);  //0x7E8
      Serial.print("\t");
      for (byte i = 0; i < len; i++) // print the data
      {
        Serial.print(rxBuf[i], HEX);
        Serial.print("\t");
      }
      Serial.println();
      back = 1;

    }
    if (back) {
      rpm = (rxBuf[3] * 256 + rxBuf[4]) / 4; //calculate the rpm
      Serial.println(rpm);
      break;
    }
  }
}

what I've tried :

  1. using the standard recieve and transfer ID but it is not detecting anything,
  2. changing txid to 0x310 or 0x00 (I use 0x00 from the data that is detected from Can_recieve) and keeping rxid 0x310, I can recieve some data from this but the value is all over the place between 100 to 7000
  3. using txid to 0x310 and rxid 0x310 but changing the txdata from the default {0x02, 0x01, 0x0C, 0x55, 0x55, 0x55, 0x55, 0x55} to {0x02, 0x01, 0x0C, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA} which has the same result

I'm not sure if I define it correctly (which i don't think so) and would like to have a ittle bit of insight on this

this is the Can_recieve code that I use

// CAN Receive Example
//

#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 18                              // Set INT to pin 2
MCP_CAN CAN0(13);                               // Set CS to pin 10


void setup()
{
  Serial.begin(115200);
  
  // Initialize MCP2515 running at 8MHz with a baudrate of 500kb/s and the masks and filters disabled.
  if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK)
    Serial.println("MCP2515 Initialized Successfully!");
  else
    Serial.println("Error Initializing MCP2515...");
  
  CAN0.setMode(MCP_NORMAL);                     // Set operation mode to normal so the MCP2515 sends acks to received data.

  pinMode(CAN0_INT, INPUT);                            // Configuring pin for /INT input
  
  Serial.println("MCP2515 Recieve");
}

void loop()
{
  if(!digitalRead(CAN0_INT))                         // If CAN0_INT pin is low, read receive buffer
  {
    CAN0.readMsgBuf(&rxId, &len, rxBuf);      // Read data: len = data length, buf = data byte(s)
    
    if((rxId & 0x80000000) == 0x80000000)     // Determine if ID is standard (11 bits) or extended (29 bits)
      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){    // Determine if message is a remote request frame.
      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();
  }
}

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

RobotDyn Shield

Hi there! I'm trying to get this CAN bus shield working:

https://robotdyn.com/can-bus-shield-for-arduino.html

I tried the Arduino library from here, burned it onto an Uno and mounted the shield. In the Arduino serial monitor all I get is this:

image

I found a fork of this library:

https://github.com/coryjfowler/MCP_CAN_lib by @coryjfowler

I setup the library and ran the "loopback" example file and I got this:

image

So this seems like I'm heading in the right direction, however when I tried some of the other examples I wasn't able to get any live data from my car. I believe I had the car's baudrate set properly... Not totally sure where I need to start looking here... Any help would be greatly appreciated!

SdFat + MCP_CAN_lib

Hi, first of all thank you for your work!

I've been researching and trying to make both Sd and MCP2515 work fine.
The issue is that I must call function sd.begin everytime before reading from CAN, or CAN won't work.

Using SHARED_SPI.

Simplified code:

#include "mcp_can.h"
#include "SdFat.h"

ExFile file;
SdExFat sd;

MCP_CAN CAN(10);

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

  initSD();
  initCANBUS();
}

void loop()
{
  readCANBUS();
  saveSD();
  initSD(); //THIS MUST BE CALLED OR NEXT readCANBUS() wont work.
}

void initSD()
{
  sd.begin(53, SD_SCK_MHZ(16));
}

void saveSD()
{
  file = sd.open("test", FILE_WRITE);

  if (file)
  {
    file.print("test");
    file.close();
  }
}

void initCANBUS()
{
  while (CAN_OK != CAN.begin(MCP_ANY, CAN_250KBPS, MCP_8MHZ))
  {
    delay(100);
  }

  CAN.setMode(MCP_NORMAL);
}

void readCANBUS()
{
  //...
}

Extended ID issue

I am trying to send information to extended address for GM SW can. If I add the address to the line:
byte sndStat4 = CAN0.sendMsgBuf(0x810EC4040, 8, data4);
byte sndStat5 = CAN0.sendMsgBuf(0x810EC8040, 8, data5);
I get
Standard ID: 0x040 DLC: 8 Data: 0x47 0x36 0x52 0x53 0x31 0x45 0x34 0x33
Standard ID: 0x040 DLC: 8 Data: 0x45 0x55 0x36 0x30 0x30 0x32 0x36 0x30
This is the data I sent but the wrong ID.

If I send byte sndStat6 = CAN0.sendMsgBuf(0x812CA040, 8, data6); // Engine_Information_1_LS
I get
Extended ID: 0x012CC040 DLC: 8 Data: 0x01 0x01 0xCF 0x8D 0x00 0x55 0x06 0x6D

also, I am trying to send around 12 different can signals but only see maybe 4 sent back.
/* CAN Loopback Example

  • This example sends a message once a second and receives that message
  • no CAN bus is required. This example will test the functionality
  • of the protocol controller, and connections to it.
  • Written By: Cory J. Fowler - October 5th 2016
    */

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

// CAN TX Variables
unsigned long prevTX = 0; // Variable to store last execution time
unsigned long prevTX1 = 0; // Variable to store last execution time
const unsigned int invlTX = 1000; // One second interval constant
const unsigned int invlTX1 = 1000; // One second interval constant
unsigned long prevTX2 = 0; // Variable to store last execution time
unsigned long prevTX3 = 0; // Variable to store last execution time
const unsigned int invlTX2 = 1000; // One second interval constant
const unsigned int invlTX3 = 1000; // One second interval constant
unsigned long prevTX4 = 0; // Variable to store last execution time
unsigned long prevTX5 = 0; // Variable to store last execution time
const unsigned int invlTX4 = 1000; // One second interval constant
const unsigned int invlTX5 = 1000; // One second interval constant

byte data0[] = {0xFE, 0x02, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00}; // 101 command
byte data1[] = {0x00, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // VNMF_LS_621_BCM
byte data2[] = {0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // SW CAN $624
byte data3[] = {0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // VNMF_LS_62C_IPC CGM_LS IPC_LS
byte data4[] = {0x47, 0x36, 0x52, 0x53, 0x31, 0x45, 0x34, 0x33}; // vin 2 0to 9 hex to accii G6RS1E43
byte data5[] = {0x45, 0x55, 0x36, 0x30, 0x30, 0x32, 0x36, 0x30}; // vin 10 to 17 hex to accii EU600260
byte data6[] = {0x00, 0x00, 0x00, 0x00, 0x35, 0x00, 0xA3, 0x01}; // Engine_Information_1_LS
byte data7[] = {0x01, 0x01, 0xCF, 0x8D, 0x00, 0x55, 0x06, 0x6D}; // Engine_Information_2_LS
byte data8[] = {0x50, 0x00, 0x79, 0x00, 0x00, 0x00, 0x06, 0xA5}; // Engine_Information_3_LS
byte data9[] = {0x60, 0x60, 0x01, 0xF4}; // Wheel_Pulses
byte data10[] = {0x02}; // System_Power_mode
byte data11[] = {0x00, 0x2B, 0x00, 0x2B, 0x00, 0x2A, 0x00, 0x2A}; // Wheel_Grnd_Velocity_LS Power_Mode_Info_LS
byte data12[] = {0x04, 0x03, 0x00}; // Power_Mode_info

// CAN RX Variables
long unsigned int rxId;
unsigned char len;
unsigned char rxBuf[128];

// Serial Output String Buffer
char msgString[128];

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

void setup()
{
Serial.begin(115200); // CAN is running at 500,000BPS; 115,200BPS is SLOW, not FAST, thus 9600 is crippling.

// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
if(CAN0.begin(MCP_ANY, CAN_33K3BPS, MCP_16MHZ) == CAN_OK)
Serial.println("MCP2515 Initialized Successfully!");
else
Serial.println("Error Initializing MCP2515...");

// Since we do not set NORMAL mode, we are in loopback mode by default.//Changed from example JJ
//CAN0.setMode(MCP_NORMAL);

pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input

Serial.println("Bosch CAN test...");
}

void loop()
{
if(millis() - prevTX >= invlTX)
{ // Send this at a one second interval.
prevTX = millis();
byte sndStat0 = CAN0.sendMsgBuf(0x1101, 8, data0); // 101 command
byte sndStat1 = CAN0.sendMsgBuf(0x1621, 8, data1); //VNMF_LS_621_BCM

if(millis() - prevTX1 >= invlTX1)
{ // Send this at a one second interval.
prevTX1 = millis();
byte sndStat2 = CAN0.sendMsgBuf(0x1624, 8, data2); //SW CAN $624
byte sndStat3 = CAN0.sendMsgBuf(0x162C, 8, data3); //VNMF_LS_62C_IPC CGM_LS IPC_LS
}
if(millis() - prevTX2 >= invlTX2)
{ // Send this at a one second interval.
prevTX2 = millis();

byte sndStat4 = CAN0.sendMsgBuf(0x80EC4040, 8, data4); // vin 2 0to 9 hex to accii G6RS1E43   
byte sndStat5 = CAN0.sendMsgBuf(0x80EC8040, 8, data5); // vin 10 to 17 hex to accii EU600260

}

if(millis() - prevTX3 >= invlTX3)
{ // Send this at a one second interval.
prevTX3 = millis();

byte sndStat6 = CAN0.sendMsgBuf(0x812CA040, 8, data6); // Engine_Information_1_LS
byte sndStat7 = CAN0.sendMsgBuf(0x812CC040, 8, data7); // Engine_Information_2_LS

}

if(millis() - prevTX4 >= invlTX4)
{ // Send this at a one second interval.
prevTX4 = millis();

byte sndStat8 = CAN0.sendMsgBuf(0x802CE040, 8, data8); // Engine_Information_3_LS 
byte sndStat9 = CAN0.sendMsgBuf(0x8077C040, 4, data9);  // Wheel_Pulses

}
if(millis() - prevTX5 >= invlTX5)
{ // Send this at a one second interval.
prevTX5 = millis();

byte sndStat10 = CAN0.sendMsgBuf(0x80242040, 2, data10);  //System_Power_mode
byte sndStat11 = CAN0.sendMsgBuf(0x806B8040, 8, data11);  //Wheel_Grnd_Velocity_LS 
byte sndStat12 = CAN0.sendMsgBuf(0x80242040, 3, data12);  // Power_Mode_info

}

}
if(!digitalRead(CAN0_INT)) // If CAN0_INT pin is low, read receive buffer
{
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)

if((rxId & 0x80000000) == 0x80000000)             // Determine if ID is standard (11 bits) or extended (29 bits)
  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){            // Determine if message is a remote request frame.
  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();

}

}

/*********************************************************************************************************
this is what I see in play back
Standard ID: 0x101 DLC: 8 Data: 0xFE 0x02 0x10 0x02 0x00 0x00 0x00 0x00
Standard ID: 0x624 DLC: 8 Data: 0x00 0x20 0x00 0x00 0x00 0x00 0x00 0x00
Extended ID: 0x006B8040 DLC: 8 Data: 0x00 0x2B 0x00 0x2B 0x00 0x2A 0x00 0x2A
Standard ID: 0x62C DLC: 8 Data: 0x00 0x40 0x00 0x00 0x00 0x00 0x00 0x00
Extended ID: 0x00242040 DLC: 3 Data: 0x04 0x03 0x00
Standard ID: 0x101 DLC: 8 Data: 0xFE 0x02 0x10 0x02 0x00 0x00 0x00 0x00
Standard ID: 0x624 DLC: 8 Data: 0x00 0x20 0x00 0x00 0x00 0x00 0x00 0x00
Extended ID: 0x00EC4040 DLC: 8 Data: 0x47 0x36 0x52 0x53 0x31 0x45 0x34 0x33
Standard ID: 0x62C DLC: 8 Data: 0x00 0x40 0x00 0x00 0x00 0x00 0x00 0x00
Extended ID: 0x00EC8040 DLC: 8 Data: 0x45 0x55 0x36 0x30 0x30 0x32 0x36 0x30
Standard ID: 0x101 DLC: 8 Data: 0xFE 0x02 0x10 0x02 0x00 0x00 0x00 0x00
Standard ID: 0x624 DLC: 8 Data: 0x00 0x20 0x00 0x00 0x00 0x00 0x00 0x00
Extended ID: 0x006B8040 DLC: 8 Data: 0x00 0x2B 0x00 0x2B 0x00 0x2A 0x00 0x2A
Standard ID: 0x62C DLC: 8 Data: 0x00 0x40 0x00 0x00 0x00 0x00 0x00 0x00
Extended ID: 0x00242040 DLC: 3 Data: 0x04 0x03 0x00
Standard ID: 0x101 DLC: 8 Data: 0xFE 0x02 0x10 0x02 0x00 0x00 0x00 0x00
Standard ID: 0x624 DLC: 8 Data: 0x00 0x20 0x00 0x00 0x00 0x00 0x00 0x00
Extended ID: 0x012CA040 DLC: 8 Data: 0x00 0x00 0x00 0x00 0x35 0x00 0xA3 0x01
Standard ID: 0x62C DLC: 8 Data: 0x00 0x40 0x00 0x00 0x00 0x00 0x00 0x00
Extended ID: 0x012CC040 DLC: 8 Data: 0x01 0x01 0xCF 0x8D 0x00 0x55 0x06 0x6D
Standard ID: 0x101 DLC: 8 Data: 0xFE 0x02 0x10 0x02 0x00 0x00 0x00 0x00
Standard ID: 0x624 DLC: 8 Data: 0x00 0x20 0x00 0x00 0x00 0x00 0x00 0x00
Extended ID: 0x006B8040 DLC: 8 Data: 0x00 0x2B 0x00 0x2B 0x00 0x2A 0x00 0x2A
Standard ID: 0x62C DLC: 8 Data: 0x00 0x40 0x00 0x00 0x00 0x00 0x00 0x00
Extended ID: 0x00242040 DLC: 3 Data: 0x04 0x03 0x00
Standard ID: 0x101 DLC: 8 Data: 0xFE 0x02 0x10 0x02 0x00 0x00 0x00 0x00
Standard ID: 0x624 DLC: 8 Data: 0x00 0x20 0x00 0x00 0x00 0x00 0x00 0x00
Extended ID: 0x012CA040 DLC: 8 Data: 0x00 0x00 0x00 0x00 0x35 0x00 0xA3 0x01
Standard ID: 0x62C DLC: 8 Data: 0x00 0x40 0x00 0x00 0x00 0x00 0x00 0x00
Extended ID: 0x012CC040 DLC: 8 Data: 0x01 0x01 0xCF 0x8D 0x00 0x55 0x06 0x6D
Standard ID: 0x101 DLC: 8 Data: 0xFE 0x02 0x10 0x02 0x00 0x00 0x00 0x00
Standard ID: 0x624 DLC: 8 Data: 0x00 0x20 0x00 0x00 0x00 0x00 0x00 0x00
Extended ID: 0x006B8040 DLC: 8 Data: 0x00 0x2B 0x00 0x2B 0x00 0x2A 0x00 0x2A
Standard ID: 0x62C DLC: 8 Data: 0x00 0x40 0x00 0x00 0x00 0x00 0x00 0x00
Extended ID: 0x00242040 DLC: 3 Data: 0x04 0x03 0x00
Standard ID: 0x101 DLC: 8 Data: 0xFE 0x02 0x10 0x02 0x00 0x00 0x00 0x00
Standard ID: 0x624 DLC: 8 Data: 0x00 0x20 0x00 0x00 0x00 0x00 0x00 0x00
Extended ID: 0x006B8040 DLC: 8 Data: 0x00 0x2B 0x00 0x2B 0x00 0x2A 0x00 0x2A
Standard ID: 0x62C DLC: 8 Data: 0x00 0x40 0x00 0x00 0x00 0x00 0x00 0x00

Problem with setting Filter and Mask during bus traffic

Hello,
I have got still trouble with setting Filters and Masks when other Nodes are sending messages very fast:
My Shield is using in industrial application and I do not have influence if other nodes are sending and with which frequency. So when there is a node sending messages (with IDs which should be blocked) when MCP is initializing, filtering does not work because I only can call init_Mask and init_Filt after CAN.begin is called. Maybe it is better if there is a possibility to set filters and masks before CAN.begin.

Example: A node is sending on ID 5 very, very fast all time.

When starting sketch I call CAN.begin and init_Mask and init_Filt to receive ID 522 only.

    if(CAN_OK == CAN.begin(CAN_500KBPS))                   // init can bus : baudrate = 500k
    {
        Serial.println("CAN BUS Shield init ok!");
        CAN.init_Mask(0, 0, 0x3FF);
        CAN.init_Mask(1, 0, 0x3FF);
        CAN.init_Filt(0, 0, 0x20A);
        CAN.init_Filt(1, 0, 0x20A);
        CAN.init_Filt(2, 0, 0x20A);
        CAN.init_Filt(3, 0, 0x20A);
        CAN.init_Filt(4, 0, 0x20A);
        CAN.init_Filt(5, 0, 0x20A);
        delay(100);
        Serial.println("masks and filters set!");
    }

The result: No message is filtered. All messages from all IDs arrive the buffer. MCP gets overflow.

_If there is no other Node sending data when resetting MCP, the filtering is working find and only ID 522 comes into buffer._

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.