Canbus use

Status
Not open for further replies.

mark.winger

Active member
I'm struggling to figure out how to begin. I would like to us a teensy 3.2 to monitor the can bus on a GM vehicle to capture when the car is in reverse, and capture the steering when buttons for phone and media control. I want to integrate this with openauto pro. I would like the teensy simply to listen to the gmlan, and when one of the buttons is pressed or reverse is entered, the teensy would simple send usb key code to raspberry pi based on what message it sees.

But I am having difficulty with how to listen for this info on the can bus. Is there anyone who has done something like this or can point me to how to read these messages from the bus?
 
... how to listen for this info on the can bus.
You may want to start with just a basic CAN bus monitor and see what messages are on the bus.
Here is a the code that I have been using:
Code:
// https://github.com/collin80/FlexCAN_Library
// https://en.wikipedia.org/wiki/CAN_bus

#include <FlexCAN.h>  // TI SN65HVD230 CAN-bus transceiver module: CTX pin 3, CRX pin 4

static CAN_message_t msg;
static CAN_filter_t allPassFilter;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(115200);
  while (!Serial) ;       // wait for serial monitor
  
  allPassFilter.flags.extended = 1;  // 1 for extended/29 bit IDs
  Can0.begin(250000, allPassFilter); // init CAN bus @ 250kbps
  Serial.println("CAN initialized and listening");
}

void loop() {
  if (Can0.read(msg)) {
    digitalWrite(LED_BUILTIN, HIGH);
    Serial.printf("0x%08X", msg.id); Serial.print(" ");
    Serial.printf("0x%04X", msg.timestamp); Serial.print(" ");
    Serial.printf("0x%02X", msg.flags); Serial.print(" ");
    Serial.printf("0x%02X", msg.len); Serial.print(" ");
    for (byte i = 0; i < sizeof(msg.buf); i++) {
      Serial.printf("0x%02X", msg.buf[i]); Serial.print(" ");
    }
    Serial.println("");
    digitalWrite(LED_BUILTIN, LOW);
  }
}

This will work but if you are really going to dive into the CAN bus data, then a CAN bus analyzer like this one, will help a lot. It saved me a lot of time while debugging a Teensy-based CAN bus device.

Paul
 
I have been using these:

TI SN65HVD230 CAN-bus transceiver module.png

Google for "TI SN65HVD230 CAN-bus transceiver module".
No configuration needed for this transceiver, just hookup the CRX & CTX pins to the appropriate Teensy CAN bus pins.

Paul
 
I'm not that familiar with light-duty vehicle interfacing but I have some mileage extracting data from heavy-duty equipment like winter operations gear. I usually take this older FlexCAN lib on the T3.2 as it's dead simple to use, once you begin() it you can just keep polling messages off the bus.

Code:
#include <FlexCAN.h>

FlexCAN *flexCAN;
CAN_message_t canMessage;

void setup() {
    flexCAN = new FlexCAN(250000); // or whatever baud rate you need here
    flexCAN->begin();
}
  
void loop() {
    if (flexCAN->available()) {
        flexCAN->read(canMessage);
        
        Serial.printf("PGN %04x buf %02x%02x%02x%02x%02x%02x%02x%02x, canMessage.len = %u\r\n",
            canMessage.id,
            canMessage.buf[0], canMessage.buf[1], canMessage.buf[2], canMessage.buf[3],
            canMessage.buf[4], canMessage.buf[5], canMessage.buf[6], canMessage.buf[7],
            canMessage.len
        );
    }
}

As PaulS and TeensyWolf said any CAN transceiver will do it, I have a preference for the MCP2562 as most of the stuff in my boards is 5V and this one has split voltages for Vss and Vio, so I can feed 3.3V to its Vio pin and safely interface the Teensy.

I would like the teensy simply to listen to the gmlan, and when one of the buttons is pressed or reverse is entered, the teensy would simple send usb key code to raspberry pi based on what message it sees.

I'm not aware of the GMLAN protocol operation, but I'd guess it just broadcasts its data on the CAN bus; If that's the case it would be just a matter of knowing the ID of the messages carrying the specific data you want. Later you can add masks and filters to only listen for the messages you actually need.
 
Status
Not open for further replies.
Back
Top