Basic help needed understanding can bus and FlexCAN

Status
Not open for further replies.

keith-i

Member
I'm a relative newcomer to Arduino but have succesfully completed a couple of projects. I'm now moving on to a can bus project and need some help getting started with FlexCAN (on a Teensy 3.6).

My aim is to read 4 can bus messages from a boat engine (0x280, 0x288, 0x380 and 0x588) and transfer some of the data to a NMEA2000 network. I can deal with the NMEA part using Timo Lappalainen's code, and I can read the engine canbus messages with Collin80's FlexCAN code. Where I'm struggling is getting the can bus data out from FlexCAN and into NMEA.

I have been advised to:
create new module for handling standard CAN messages with interrupts. Then just poll the buffer

Using Collin80's code I can receive the messages and view them on serial monitor.

Code:
#include <FlexCAN.h>

class ExampleClass : public CANListener 
{
public:
   void printFrame(CAN_message_t &frame, int mailbox);
   bool frameHandler(CAN_message_t &frame, int mailbox, uint8_t controller); //overrides the parent version so we can actually do something
};

void ExampleClass::printFrame(CAN_message_t &frame, int mailbox)
{
   Serial.print("ID: ");
   Serial.print(frame.id, HEX);
   Serial.print(" Data: ");
   for (int c = 0; c < frame.len; c++) 
   {
      Serial.print(frame.buf[c], HEX);
      Serial.write(' ');
   }
   Serial.write('\r');
   Serial.write('\n');
}

bool ExampleClass::frameHandler(CAN_message_t &frame, int mailbox, uint8_t controller)
{
    printFrame(frame, mailbox);

    return true;
}

ExampleClass exampleClass;

// -------------------------------------------------------------
void setup(void)
{
  delay(1000);
  Serial.println(F("Hello Teensy Single CAN Receiving Example With Objects."));

  Can0.begin(500000);  

  //if using enable pins on a transceiver they need to be set on
  pinMode(2, OUTPUT);
  digitalWrite(2, HIGH);

  Can0.attachObj(&exampleClass);
  exampleClass.attachGeneralHandler();
}


// -------------------------------------------------------------
void loop(void)
{
  delay(1000);
  Serial.write('.');
}

Question 1: Where are the messages? Where do mailboxes or buffers come into this? How do I reference them?

Question 2: Presumably I need to filter the messages and pass them to a variable and select the data bytes I'm interested in.

Question 3: What are frameHandler, GeneralHandler and exampleClass in laymans terms.

Finally, a simple explanation of the interrupt and polling process would be appreciated so that I can start researching that element.

Any help to get me started would be appreciated.
 
Status
Not open for further replies.
Back
Top