Page 1 of 1

Weird serial delays

Posted: Thu Sep 10, 2020 8:36 pm
by Galeforce9
Hello

I am new to the esp32 but not programming. I have a new project but have a strange issue. I am using serial1 and serial2 and both ports are used to transfer rs485 data.

The data being sent to serial1 is a three byte packet, followed by by a delay of approximately 2.5ms and then repeated. I read the data and echo each byte to the opposing serial port and also to the monitor port for display. The serial speed is 9600 baud but the monitor port is running at 115,200 baud so it does not add any delay. However when I look on the scope the following happens.
Data in as above three bytes, 2.5ms delay repeat
Data out nine bytes (3 x 3 bytes as one packet) approx 10ms delay repeat.

I can not understand why this is happening. The loop code is as follows:

Code: Select all

void loop(){

  if (Serial1.available()) {
    S_Data1 = Serial1.read();
    //Serial.print(char(S_Data1));
    Serial2.write(S_Data1);
    if (S_Data1<0x10) {Serial.print("0");}
    Serial.print(S_Data1,HEX);
    Serial.print(" ");
    
    if (S_Data1 == 0x23){
      Serial.print(char(13));
      Serial.print(char(10));
    }
  }

 
If anyone can help as I need the byte timing to match from input to output or the data stream fails.

Thanks in advice

Ian

Re: Weird serial delays

Posted: Sat Sep 12, 2020 6:19 pm
by lbernstone
Serial (UART) on esp32 is buffered. If you want to force a send for your timing, use Serial.flush().

Re: Weird serial delays

Posted: Mon Sep 14, 2020 7:52 pm
by Galeforce9
Hello

Many thanks for your reply. I'm not sure serial flush is what I need but I will give it a try as soon as I can. I have read here:
https://www.baldengineer.com/when-do-yo ... flush.html
so understand its function.

My normal platform (not used for a couple of years)is the PIC32 with a Mikroe compiler. I have done similar projects with this and have an ISR for both serial tx and rx with cyclic buffers. Incoming serial is read into a buffer and a pointer updated. With TX, data is placed into the buffer and a pointer set. The TX int is set and automatically the data flows out until finished. The timing is very accurate and no hold up of the mpu as all the data is 100% under interrupt control. Can this be achieved withe the esp32? if so are there any examples I could look at.

Kind regards

Ian

Re: Weird serial delays

Posted: Tue Sep 15, 2020 4:20 pm
by lbernstone
If you are an experienced programmer and want more control over your timing, I suggest you use the IDF driver instead of Arduino. https://docs.espressif.com/projects/esp ... /uart.html

Re: Weird serial delays

Posted: Sun Sep 20, 2020 10:02 pm
by mrburnette
Galeforce9 wrote:
Mon Sep 14, 2020 7:52 pm
... I have done similar projects with this and have an ISR for both serial tx and rx with cyclic buffers. Incoming serial is read into a buffer and a pointer updated. With TX, data is placed into the buffer and a pointer set. The TX int is set and automatically the data flows out until finished. The timing is very accurate and no hold up of the mpu as all the data is 100% under interrupt control. Can this be achieved withe the esp32? if so are there any examples I could look at.
...
Arduino on ESP32 uses FreeRTOS to enable the dual-cpu architecture: cpu_0 is for the wireless threads and cpu_1 is reserved for the Arduino sketch ... by default.

Short overview: https://www.hackster.io/rayburne/esp32- ... res-8dd948

If you intend on having critical timing, move away from Arduino and use the native IDF functions.