Add timeout to CAN write to prevent crashing

This commit is contained in:
Daniel 2023-08-16 23:55:28 +03:00
parent 56f7f205e4
commit 7025811c6e
4 changed files with 31 additions and 10 deletions

View file

@ -1,4 +1,5 @@
#include "ESP32CAN.h"
#include <Arduino.h>
int ESP32CAN::CANInit()
{
@ -6,7 +7,24 @@ int ESP32CAN::CANInit()
}
int ESP32CAN::CANWriteFrame(const CAN_frame_t* p_frame)
{
return CAN_write_frame(p_frame);
static unsigned long start_time;
int result = -1;
if(tx_ok){
result = CAN_write_frame(p_frame);
tx_ok = (result == 0) ? true : false;
if(tx_ok == false){
Serial.println("CAN failure! Check wires");
LEDcolor = 3;
start_time = millis();
}
}
else{
if((millis() - start_time) >= 2000){
tx_ok = true;
LEDcolor = 0;
}
}
return result;
}
int ESP32CAN::CANStop()
{