以下为普通函数做回调函数的实现例子。同时包含串口异常处理。异常20次后进行程序复位处理。
Serial.onReceiveError(OnUart0ReceiveError);//调用错误事件
Serial1.onReceiveError(OnUart1ReceiveError);//调用错误事件
#include "esp_err.h"
#include "esp_system.h"
#include "esp_task_wdt.h" //设置看门狗用
#include "soc/rtc_wdt.h"
/*
经测试比较,这种直接调用函数的方案和例程: SerialReceiveEventOnRxChar2 结果相同,com1和com2 接收速度互不影响。
而 SerialEvent 则明显 serial1受 serial0的影响。
*/
#define TWDT_TIMEOUT_S 5
#define TASK_RESET_PERIOD_MS 1000
#define MAIN_DELAY_MS 10000
#define BTN_STOP_ALARM 0
#define LED_D4 12
#define LED_D5 13
#define TXD1 0
#define RXD1 1
#define ToggleOutput(out) do { digitalWrite(out, !digitalRead(out)); } while(0)
#define ToggleOutBlink(out,interval) do {static uint32_t estp=0;if( (millis()-estp)>=interval ){ estp=millis();digitalWrite(out, !digitalRead(out));} } while(0)
#define MaxUartErrorCount 20
#define MaxUartRxBufferSize 512
#define Uart0BaudRate 9600
#define Uart1BaudRate 115200
volatile uint16_t SerialErrCount;
volatile uint16_t Serial1ErrCount;
String comdata = "";//声明字符串变量
String comdata1 = "";//声明字符串变量
void OnUart0ReceiveError(hardwareSerial_error_t hwerr){
while(1){
switch(hwerr) {
case UART_BREAK_ERROR:Serial.println("UART_BREAK_ERROR");
SerialErrCount++; //Serial1.eventQueueReset();
if(SerialErrCount>=MaxUartErrorCount)
//esp_restart();
ESP.restart();
break;
case UART_BUFFER_FULL_ERROR:Serial.println("UART_BUFFER_FULL_ERROR");
SerialErrCount++; //Serial1.eventQueueReset();
if(SerialErrCount>=MaxUartErrorCount)
//esp_restart();
ESP.restart();
break;
case UART_FIFO_OVF_ERROR:Serial.println("UART_FIFO_OVF_ERROR");
SerialErrCount++; //Serial1.eventQueueReset();
if(SerialErrCount>=MaxUartErrorCount)
//esp_restart();
ESP.restart();
break;
case UART_FRAME_ERROR:Serial.println("UART_FRAME_ERROR");
SerialErrCount++; //Serial1.eventQueueReset();
if(SerialErrCount>=MaxUartErrorCount)
//esp_restart();
ESP.restart();
break;
case UART_PARITY_ERROR:Serial.println("UART_PARITY_ERROR");
SerialErrCount++; //Serial1.eventQueueReset();
if(SerialErrCount>=MaxUartErrorCount)
//esp_restart();
ESP.restart();
break;
default:vTaskDelay(1); //必须放一个以便响应其它人物的消息循环,否则将死锁。
break;
}
}
}
void OnUart1ReceiveError(hardwareSerial_error_t hwerr){
while(1){
switch(hwerr) {
case UART_BREAK_ERROR:Serial1.println("UART_BREAK_ERROR");
Serial1ErrCount++; //Serial1.eventQueueReset();
if(Serial1ErrCount>=MaxUartErrorCount)
esp_restart();
break;
case UART_BUFFER_FULL_ERROR: Serial1.println("UART_BUFFER_FULL_ERROR");
Serial1ErrCount++; //Serial1.eventQueueReset();
if(Serial1ErrCount>=MaxUartErrorCount)
//esp_restart();
ESP.restart();
break;
case UART_FIFO_OVF_ERROR:Serial1.println("UART_FIFO_OVF_ERROR");
Serial1ErrCount++; //Serial1.eventQueueReset();
if(Serial1ErrCount>=MaxUartErrorCount)
//esp_restart();
ESP.restart();
break;
case UART_FRAME_ERROR: Serial1.println("UART_FRAME_ERROR");
Serial1ErrCount++; //Serial1.eventQueueReset();
if(Serial1ErrCount>=MaxUartErrorCount)
//esp_restart();
ESP.restart();
break;
case UART_PARITY_ERROR:Serial1.println("UART_PARITY_ERROR");
Serial1ErrCount++; //Serial1.eventQueueReset();
if(Serial1ErrCount>=MaxUartErrorCount)
//esp_restart();
ESP.restart();
break;
default:vTaskDelay(1); //必须放一个以便响应其它人物的消息循环,否则将死锁。
break;
}
}
}
void setup() {
Serial.begin(Uart0BaudRate);
Serial1.begin(Uart1BaudRate,SERIAL_8N1,RXD1,TXD1);
//Serial.onReceive(UART0_RX_IRQ);
Serial.println("Send data to UART0 in order to activate the RX callback");
//Serial1.onReceive(UART1_RX_IRQ);
Serial1.println("Send data to UART1 in order to activate the RX callback");
while(Serial.read()>= 0){}//clear serialport0
while(Serial1.read()>= 0){}//clear serialport1
Serial.onReceiveError(OnUart0ReceiveError);//调用错误事件
Serial1.onReceiveError(OnUart1ReceiveError);//调用错误事件
pinMode(LED_D4, OUTPUT);
pinMode(LED_D5, OUTPUT); //
esp_task_wdt_init(TWDT_TIMEOUT_S, false);//uint32_t timeout:TWDT计时溢出的周期,单位秒。
}
void loop() {
esp_task_wdt_reset();
vTaskDelay(pdMS_TO_TICKS(TASK_RESET_PERIOD_MS));//1000 ms 喂狗一次
//Serial.println("Sleeping for 10 seconds...");
//delay(10000);
}