Skip to content

serial cannot receive the last byte#1824

@piaoyezju

Description

@piaoyezju

Hardware:

Board: ESP32 Dev Module
Core Installation/update date: latest
IDE name: Arduino IDE

Description:

A few minutes after booting, the serial port will not receive the last byte unless there is new data behind it, and then the new data is still missing the last byte.
The code is as follows, connect P16, P17
please help me ,thanks!

Sketch:

//#include "SPIFFS.h" #include<WiFi.h> HardwareSerial USE_SERIAL1(1); HardwareSerial USE_SERIAL2(2); #defineDEBUG_SERIAL Serial #defineUART1_RX19 #defineUART1_TX18 #defineUART2_RX16 #defineUART2_TX17 #defineUART0_RX23 #defineUART0_TX22 byte state = 0x00; unsignedlong expireTime = 0; voidstartQuery(){if (state == 0x00){// new queryif(USE_SERIAL2.available() > 0){DEBUG_SERIAL.println("new query, clear data:" + String(USE_SERIAL2.available())); while (USE_SERIAL2.available() > 0){USE_SERIAL2.read()} } expireTime = millis() + 300; state = 0x01; byte cmd[9] ={0x2b, 0x00, 0x01, 0x00, 0x01, 0xa1, 0x00, 0x00, 0x7e}; USE_SERIAL2.write(cmd, 9)} elseif (state == 0x01){if(USE_SERIAL2.available() >= 9){while (USE_SERIAL2.available() >= 9){byte head = USE_SERIAL2.read(); if (head == 0x2b){byte buf[8] ={0}; USE_SERIAL2.readBytes(buf, 8); state = 0x00; DEBUG_SERIAL.print(".")}else{DEBUG_SERIAL.print("error first byte:"); DEBUG_SERIAL.print(head, HEX ); DEBUG_SERIAL.println("")} } }elseif (expireTime < millis()){// timeout DEBUG_SERIAL.println("timeout, clear data:" + String(USE_SERIAL2.available())); while (USE_SERIAL2.available() > 0){USE_SERIAL2.read()} state = 0x00} } } voidsetup(){// SPIFFS.begin(true); WiFi.begin(); USE_SERIAL1.begin(38400, SERIAL_8N1, UART1_RX, UART1_TX); USE_SERIAL2.begin(38400, SERIAL_8N1, UART2_RX, UART2_TX); DEBUG_SERIAL.begin(115200)} voidloop(){startQuery()}

Debug Messages:

..................... error first byte:7E timeout, clear data: 8 error first byte:7E timeout, clear data: 8 error first byte:7E timeout, clear data: 8 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions