ESP32_Arduino_CAN/examples/esp32can_receive/esp32can_receive.ino

38 lines
1.1 KiB
Arduino
Raw Permalink Normal View History

2022-12-23 23:43:53 +01:00
/* ESP32 Arduino CAN Receive Basic Demo
* This example will receive messages on the CAN bus
*
* An external transceiver is required and should be connected
* to the CAN_tx and CAN_rx gpio pins specified by CANInit. Be sure
* to use a 3.3V compatable transceiver such as the SN65HVD23x
*
*/
2022-12-14 02:02:31 +01:00
#include <Arduino.h>
#include <ESP32CAN.h>
void setup() {
Serial.begin(115200);
Serial.println("ESP32-Arduino-CAN Receive Basic Demo");
2022-12-23 23:43:53 +01:00
/* initialize and start, use pin 5 as CAN_tx and pin 4 as CAN_rx, CAN bus is set to 500kbps */
2022-12-14 02:02:31 +01:00
ESP32Can.CANInit(GPIO_NUM_5, GPIO_NUM_4, ESP32CAN_SPEED_500KBPS);
}
void loop() {
twai_message_t rx_frame;
2022-12-23 23:43:53 +01:00
if (ESP32CAN_OK == ESP32Can.CANReadFrame(&rx_frame)) { /* only print when CAN message is received*/
Serial.print(rx_frame.identifier, HEX); /* print the CAN ID*/
2022-12-23 19:20:26 +01:00
Serial.print(" ");
2022-12-23 23:43:53 +01:00
Serial.print(rx_frame.data_length_code); /* print number of bytes in data frame*/
2022-12-23 19:20:26 +01:00
2022-12-23 23:43:53 +01:00
for (int i=0; i<rx_frame.data_length_code; i++) { /* print the data frame*/
2022-12-23 19:20:26 +01:00
Serial.print(rx_frame.data[i], HEX);
}
2022-12-14 02:02:31 +01:00
2022-12-23 19:20:26 +01:00
Serial.println();
2022-12-14 02:02:31 +01:00
}
delay(1000);
}