I am stumped. Trying to dump whatever my esp32 sees with CAN 2.0B 29-bit. I can pull 11 bit no issues when hooked up to a device that is transmitting 2.0B data. Sniffer shows data going out, esp32 shows NO data.
To try and test the code I used a Mega and Esp32:
ESP32 hooked up to CAN chip is the SN65HVD230 (receiving device). To test, I used coryjfowler's MCP CAN lib for the CANBus shield 2.0 on a Mega (sending device) and hooked it up to the VD230 on the ESP32.
sample code to dump whatever it sees on the ESP32 (receiving device)
#include <Arduino.h>
#include <ESP32CAN.h>
#include <CAN_config.h>
CAN_device_t CAN_cfg; // CAN Config
const int rx_queue_size = 10; // Receive Queue size
void setup() {
Serial.begin(115200);
Serial.println("Read stuff");
// Initialize CAN bus
CAN_cfg.speed = CAN_SPEED_1000KBPS;
CAN_cfg.tx_pin_id = GPIO_NUM_25;
CAN_cfg.rx_pin_id = GPIO_NUM_26;
CAN_cfg.rx_queue = xQueueCreate(rx_queue_size, sizeof(CAN_frame_t));
ESP32Can.CANInit();
// Init CAN Module
if (ESP32Can.CANInit() != ESP_OK) {
Serial.println("CAN initialization failed");
while (1); // halt the program
}
Serial.println("CAN initialized");
}
void loop() {
CAN_frame_t rx_frame;
// Receive next CAN frame from queue
if (xQueueReceive(CAN_cfg.rx_queue, &rx_frame, 3 * portTICK_PERIOD_MS) == pdTRUE) {
Serial.print("MsgID: ");
Serial.print(rx_frame.MsgID, HEX);
Serial.print(", ID Type: ");
if (rx_frame.FIR.B.FF == CAN_frame_std) {
Serial.print("11-bit");
} else if (rx_frame.FIR.B.FF == CAN_frame_ext) {
Serial.print("29-bit");
}
Serial.print(", Data: ");
for (int i = 0; i < rx_frame.FIR.B.DLC; i++) {
Serial.print(rx_frame.data.u8[i], HEX);
Serial.print(" ");
}
Serial.println();
}
}
code on the Mega sending both 11 bit and 29 bit data:
#include <Arduino.h>
#include <mcp_can.h>
#define PIN_CAN_CS 9 // chip select
#define PIN_CAN_INT 2 // interrupt
MCP_CAN CAN0(PIN_CAN_CS);
void setup() {
Serial.begin(115200);
CAN0.begin(CAN_500KBPS);
}
void loop() {
// Create dummy message to transmit
uint8_t data[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
uint8_t len = sizeof(data) / sizeof(data[0]);
// 29-bit ID
uint32_t id_29bit = 0x1234567;
bool ext_29bit = true; // Use extended 29-bit ID
// Transmit 29-bit message
if (CAN0.sendMsgBuf(id_29bit, ext_29bit, len, data) == CAN_OK) {
Serial.println("29-bit Message transmitted successfully");
} else {
Serial.println("Error transmitting 29-bit message");
}
delay(1000);
// 11-bit ID
uint32_t id_11bit = 0x123;
bool ext_11bit = false; // Use standard 11-bit ID
// Transmit 11-bit message
if (CAN0.sendMsgBuf(id_11bit, ext_11bit, len, data) == CAN_OK) {
Serial.println("11-bit Message transmitted successfully");
} else {
Serial.println("Error transmitting 11-bit message");
}
delay(1000);
}
If I leave the ESP32 at 1000k and the Mega at 500k sending data both 11 and 29 bit is seen on the ESP32. If I set the Mega at 1000k, NO DATA on the ESP32. This is what is happening with the ESP32 and I dont know if its the library on the esp32 (Michael Wagner's ESP32CAN) or if its the hardware.
The ESP32 with the same transceiver is able to get 11bit data from various other devices but 29 bit from this one specific one it cant.