Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve CAN_Read_Demo.ino #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 37 additions & 31 deletions examples/CAN_Read_Demo/CAN_Read_Demo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,51 @@ Distributed as-is; no warranty is given.
#include <mcp2515.h>
#include <mcp2515_defs.h>

//********************************Setup Loop*********************************//
tCAN message;

void setup() {
Serial.begin(9600); // For debug use
Serial.println("CAN Read - Testing receival of CAN Bus message");
Serial.println("CAN Read - Testing receival of CAN Bus message");

delay(1000);
if(Canbus.init(CANSPEED_500)) //Initialise MCP2515 CAN controller at the specified speed

if (Canbus.init(CANSPEED_500)) {
Serial.println("CAN Init ok");
else
} else {
Serial.println("Can't init CAN");

}

delay(1000);
}

//********************************Main Loop*********************************//

void loop(){

tCAN message;
if (mcp2515_check_message())
{
if (mcp2515_get_message(&message))
{
//if(message.id == 0x620 and message.data[2] == 0xFF) //uncomment when you want to filter
//{

Serial.print("ID: ");
Serial.print(message.id,HEX);
Serial.print(", ");
Serial.print("Data: ");
Serial.print(message.header.length,DEC);
for(int i=0;i<message.header.length;i++)
{
Serial.print(message.data[i],HEX);
Serial.print(" ");
}
Serial.println("");
//}
}}
void loop() {

if (!mcp2515_check_message()) {
return;
}

memset(&message, 0, sizeof(message))

if (!mcp2515_get_message(&message)) {
return;
}

/* uncomment when you want to filter
if(message.id == 0x620 and message.data[2] != 0xFF) {
return;
}
*/

Serial.print("ID: ");
Serial.print(message.id,HEX);
Serial.print(", ");
Serial.print("Data: ");
Serial.print(message.header.length,DEC);

for(int i = 0; i < message.header.length; i++) {
Serial.print(message.data[i], HEX);
Serial.print(" ");
}

Serial.println();
}