I have been experimenting with different codes that I found on the internet.
When I set all the baud-rates etc. the same on the sending and receiving arduino'sI manage to get the code using the Can bus Sniffer code I have been using.
Code I have been using:
#include <SPI.h>
#include "mcp_can.h"
INT32U canId = 0x000;
unsigned char len = 0;
unsigned char buf[8];
char str[20];
void setup()
{
//SPI.setClockDivider(SPI_CLOCK_DIV2);
Serial.begin(115200);
START_INIT:
if (CAN_OK == CAN.begin(CAN_100KBPS))
{
Serial.println("CAN BUS Shield init ok!");
}
else
{
Serial.println("CAN BUS Shield init fail");
Serial.println("Init CAN BUS Shield again");
delay(100);
goto START_INIT;
}
}
void loop()
{
if (CAN_MSGAVAIL == CAN.checkReceive())
{
CAN.readMsgBuf(&len, buf);
canId = CAN.getCanId();
Serial.print("<"); Serial.print(canId); Serial.print(",");
for (int i = 0; i < len; i++)
{
Serial.print(buf); Serial.print(",");
}
Serial.print(">");
Serial.println();
}
}
But I found some code on another website I have been using to learn code and what does what, This project that was done involved hacking the can bus of a vehicle, they also had a send and receive code for testing purposes. When I use their send and receive code on the arduino's everything works.
But when I try using the sniffer code above to get the message nothing happens?? Could this have anything to do with my current problem I am having with the elevator CAN BUS.
The sending code from the project in question is:
#include <SPI.h>
#include "mcp_can.h"
#include <Canbus.h> // don't forget to include these
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>
void setup()
{
Serial.begin(115200); //Initialise MCP2515 CAN controller at the specified speed
if(Canbus.init(CAN_100KBPS))
Serial.println("CAN Init ok");
else
Serial.println("Can't Init CAN");
delay(1000);
}
void loop()
{
tCAN message;
message.id = 0x21; //formatted in HEX
message.header.rtr = 0;
message.header.length = 8; //formatted in DEC
message.data[0] = 0x40;
message.data[1] = 0x05;
message.data[2] = 0x30;
message.data[3] = 0xFF; //formatted in HEX
message.data[4] = 0x00;
message.data[5] = 0x40;
message.data[6] = 0x00;
message.data[7] = 0x00;
mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);
mcp2515_send_message(&message);
delay(1000);
}
And the receiving code is as follows:
#include <SPI.h>
#include "mcp_can.h"
#include <Canbus.h> // don't forget to include these
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>
void setup()
{
Serial.begin(115200); //For debugging purposes
//Initialise MCP2515 CAN controller at the specified speed
if(Canbus.init(CAN_100KBPS))
Serial.println("CAN Init ok");
else
Serial.println("Can't Init CAN");
delay(1000);
}
void loop()
{
tCAN message;
if (mcp2515_check_message())
{
if (mcp2515_get_message(&message))
{
Serial.print("ID: ");
Serial.print(message.id,HEX);
Serial.print(", ");
Serial.print("Data: ");
for(int i=0;i<message.header.length;i++)
{
Serial.print(message.data,HEX);
Serial.print(" ");
}
Serial.println("");
}}
}
I have no idea what the difference in the codes are, I thought it was basically just another way of telling the arduino what to do??
I am thinking my main problems for this project I am working on has to be the CAN BUS SPEED and maybe some sort of command message that the elevator control needs??
But if it was a message the elevator is looking for would the other display units still work normally??
The way I understand it I am basically just adding a node to the can bus? should will this node be excluded from getting data before there is some sort of message sent?