Weird serial delays

Galeforce9
Posts: 7
Joined: Thu Sep 10, 2020 8:22 pm

Weird serial delays

Postby Galeforce9 » Thu Sep 10, 2020 8:36 pm

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

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: Weird serial delays

Postby lbernstone » Sat Sep 12, 2020 6:19 pm

Serial (UART) on esp32 is buffered. If you want to force a send for your timing, use Serial.flush().

Galeforce9
Posts: 7
Joined: Thu Sep 10, 2020 8:22 pm

Re: Weird serial delays

Postby Galeforce9 » Mon Sep 14, 2020 7:52 pm

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

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: Weird serial delays

Postby lbernstone » Tue Sep 15, 2020 4:20 pm

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

mrburnette
Posts: 22
Joined: Mon Feb 12, 2018 6:50 pm

Re: Weird serial delays

Postby mrburnette » Sun Sep 20, 2020 10:02 pm

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.

Who is online

Users browsing this forum: No registered users and 79 guests