Teensy 4.0 CAN Communication with Raspberry Pi 4 CAN Hat (Wrong Messages)

ryan1

Member
Hello! I am trying to establish CAN communication between my Teensy 4.0 with a Waveshare SN65HVD230 CAN Board (Amazon: https://www.amazon.com/SN65HVD230-CAN-Board-Communication-Development/dp/B00KM6XMXO) and a Raspberry Pi with a RS485 CAN Hat (Waveshare: https://www.waveshare.com/rs485-can-hat.htm).

The code on my Teensy is this:

Code:
#include <FlexCAN_T4.h>

const int ledPin = 13;
volatile int counter = 0;

FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> myCan1;

void setup() {

Serial.begin(9600);
pinMode(ledPin, OUTPUT);
  
myCan1.begin();
myCan1.setBaudRate(500000);

}

void loop() {

CAN_message_t rmsg;

 if ( myCan1.read(rmsg) ) 
 {
  Serial.print("MB "); Serial.print(msg.mb);
  Serial.print("  OVERRUN: "); Serial.print(msg.flags.overrun);
  Serial.print("  LEN: "); Serial.print(msg.len);
  Serial.print(" EXT: "); Serial.print(msg.flags.extended);
  Serial.print(" TS: "); Serial.print(msg.timestamp);
  Serial.print(" ID: "); Serial.print(msg.id, HEX);
  Serial.print(" Buffer: ");
  for ( uint8_t i = 0; i < msg.len; i++ ) {
    Serial.print(msg.buf[i], HEX); Serial.print(" ");
  } Serial.println();
 }

digitalWrite(ledPin, !digitalRead(ledPin));
Serial.println(counter);
counter++;
delay (1000);
}

The code on my Raspberry Pi (via Python) is this:

Code:
import os
import can
import time

os.system('sudo /sbin/ip link set can0 down')
os.system('sudo ip link set can0 type can bitrate 500000')
os.system('sudo ifconfig can0 up')

with can.interface.Bus(channel='can0', interface='socketcan', bitrate=500000) as bus:
    msg = can.Message(arbitration_id=0x123, data=[1,3,5], is_extended_id=False)
    bus.send(msg)

print('hello world')


os.system('sudo ifconfig can0 down')

Every time I hit run on my Raspberry Pi, I get the following on my Teensy's serial monitor (the "counter" integer prints as well, but that's just there as a sanity check for me, so I won't paste those numbers here):

Code:
MB 0  OVERRUN: 0  LEN: 8 EXT: 0 TS: 0 ID: 0 Buffer: 0 0 0 0 0 0 0 0

Assuming my wiring and termination is all correct, what could the issue be? Earlier this week on my Teensy, I was just printing the message ID (msg.id) to my serial monitor, and noticed that every time I hit run on my Raspberry Pi Python script, the Teensy would print out a different message ID every time. So, I decided to print out all of the transmitted data packet contents (as you see in the code above) to see if it might shed any more light on the problem, but I'm a little stumped now. I've also tried swapping out my Waveshare SN65HVD230 CAN Board with a different one to make sure it wasn't just a broken chip, but no success there either.

I apologize for the poorly written code, but I've been trying to debug for a while and haven't had any success unfortunately. Thank you in advance! Please let me know if you need any more information.
 
Back
Top