-
Notifications
You must be signed in to change notification settings - Fork 7.6k
After receiving data through the serial port of the ESP32 chip, there will be a delay of several tens of mS before replying #8850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Do you actually have a fixed length payload? If so, using the onReceive cb, and then readBytes should be a good bit faster than reading bytes out of the queue one at a time. Otherwise, you are kinda stuck polling and figuring out if the sender is done, which is inherently slow. |
Shouldn't |
Anyway, I guess this is not an Arduino Core issue, but more a question about how to implement it using Arduino. |
As @lbernstone suggested it can done using something like this: #define JSON_LEN 100
void gotUARTbytes(void) {
static uint8_t serialBuff[JSON_LEN] = { 0 };
static uint16_t receCount = 0;
static unsigned long startReadTime = 0;
if (receCount == 0) {
startReadTime = micros();
}
uint16_t readLength = Serial.read(&serialBuff[receCount], JSON_LEN - receCount);
receCount += readLength;
if (receCount >= 100) {
Serial.printf("\nTime to receive %d bytes = %ld us\n", receCount, micros() - startReadTime);
receCount = 0;
}
}
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.onReceive(gotUARTbytes); // all is done in gotUARTbytes
}
void loop() {
} Output --- Receives 100 bytes in less than 50 micro seconds.
|
I send a character through a computer serial port, and the interval between receiving replies is very long, with 146-91=55mS. I am not sure why there is such a long delay. Is there a problem with my program?Please check the below, Thanks [2023-11-09 10:08:04.091]# SEND ASCII> [2023-11-09 10:08:04.146]# RECV ASCII> [2023-11-09 10:08:39.402]# SEND ASCII> [2023-11-09 10:08:39.457]# RECV ASCII> |
I test this code, the reply is 555-496=59mS, What caused the delay?Thanks! [2023-11-09 10:43:04.555]# RECV ASCII> Time to receive 3 bytes = 21 us `#define JSON_LEN 100 void gotUARTbytes(void) { if (receCount == 0) { uint16_t readLength = Serial.read(&serialBuff[receCount], JSON_LEN - receCount); if (receCount >= 1) { void setup() { void loop() { |
At 115200 bits per second, a UART transmission of just one character takes about 10 to 11 bits in time, which means about 90 microseconds. Therefore the transmssion of 100 bytes takes 9000 microseconds, or 9 ms. Sending back a short reply to the computer may take, maybe, 1ms. But there is the time that the computer takes to receive, process it and tell how long it took. This "long time" of 59 ms may be due to the computer processing for sending and receiving... My suggestion is to use UART1 RX and TX pins (or like in the example, it uses internal loopback) and run a test using only the ESP32 to check how long does it take to send, receive, process and report back. UART1 ---> RX1<<-->>TX1 (can be done with a wire or internally to the ESP32, like a software loopback) As expected this example reports that ot takes 9ms to send/receive 100 bytes.Example: #include <Arduino.h>
// There are two ways to make this sketch work:
// By physically connecting the pins 4 and 5 and then create a physical UART loopback,
// Or by using the internal IO_MUX to connect the TX signal to the RX pin, creating the
// same loopback internally.
#define USE_INTERNAL_PIN_LOOPBACK 1 // 1 uses the internal loopback, 0 for wiring pins 4 and 5 externally
#define DATA_SIZE 100 // 100 bytes - under fifo limit of 127 bytes
#define BAUD 115200 // Any baudrate from 300 to 115200
#define TEST_UART 1 // Serial1 will be used for the loopback testing with different RX FIFO FULL values
#define RXPIN 4 // GPIO 4 => RX for Serial1
#define TXPIN 5 // GPIO 5 => TX for Serial1
void setup() {
// UART0 will be used to log information into Serial Monitor
Serial.begin(115200);
// UART1 will have its RX<->TX cross connected
// GPIO4 <--> GPIO5 using external wire
Serial1.begin(BAUD, SERIAL_8N1, RXPIN, TXPIN); // Rx = 4, Tx = 5 will work for ESP32, S2, S3 and C3
#if USE_INTERNAL_PIN_LOOPBACK
uart_internal_loopback(TEST_UART, RXPIN);
#endif
Serial.printf("\n\n================================\nTest Case\n================================\n");
testAndReport();
}
void loop() {
}
void testAndReport() {
// Let's send 100 bytes from Serial1 rx<->tx and mesaure time to send and receive
uint8_t bytesReceived = 0;
uint8_t dataSent[DATA_SIZE], dataReceived[DATA_SIZE];
uint8_t i;
// initialize all data
for (i = 0; i < DATA_SIZE; i++) {
dataSent[i] = '0' + (i % 10); // fill it with a repeated sequence of 0..9 characters
dataReceived[i] = 0;
}
Serial.printf("Testing the time for receiving %d bytes at %d baud:", DATA_SIZE, BAUD);
Serial.flush(); // wait Serial FIFO to be empty and then spend almost no time processing it
uint32_t now = millis(); // considere the time to send data and received it
size_t sentBytes = Serial1.write(dataSent, sizeof(dataSent));
while (bytesReceived < DATA_SIZE) {
bytesReceived += Serial1.read(dataReceived, DATA_SIZE);
// safety for array limit && timeout... in 5 seconds...
if (millis() - now > 5000) break;
}
Serial.printf("\nIt has sent %d bytes from Serial1 TX to Serial1 RX\n", sentBytes);
uint32_t pastTime = millis() - now; // cosideres "some time" to replay back... line above.
Serial.printf("It took %d milliseconds to read %d bytes\n", pastTime, bytesReceived);
Serial.print("Received data: [");
Serial.write(dataReceived, DATA_SIZE);
Serial.println("]");
Serial.println("========================\nFinished!");
} |
Board
ESP32 Dev Module
Device Description
#define JSON_LEN 100
uint32_t lastRxTime;
uint32_t waitTime =500;
uint32_t Time1 = 0;
uint32_t Time2;
uint32_t Time3;
uint32_t Time4;
void checkComm(void) {
static uint8_t serialBuff[JSON_LEN] = { 0 };
static uint16_t receCount = 0;
if (Serial.available()) {
if (Time1 == 0) {
Time1 = micros();
}
if (receCount < JSON_LEN) {
serialBuff[receCount] = Serial.read();
lastRxTime =micros();
if (lastRxTime == 0)
lastRxTime = 1;
receCount++;
} else {
receCount = 0;
lastRxTime = 0;
}
}
if (lastRxTime != 0 && micros() - lastRxTime >= waitTime) {
}
}
void setup() {
Serial.begin(115200);
while (!Serial)
;
}
void loop() {
checkComm();
}
Hardware Configuration
normal
Version
v2.0.7
IDE Name
Arduino
Operating System
Win 11
Flash frequency
40
PSRAM enabled
no
Upload speed
115200
Description
After receiving data through the serial port of the ESP32 chip, there will be a delay of several tens of mS before replying.
We need it reply in 5mS
Sketch
Debug Message
Other Steps to Reproduce
no
I have checked existing issues, online documentation and the Troubleshooting Guide
The text was updated successfully, but these errors were encountered: