Strange Serial2 receive delay

reiners
Posts: 2
Joined: Sun Feb 20, 2022 9:30 pm

Strange Serial2 receive delay

Postby reiners » Sun Feb 20, 2022 9:48 pm

Hello all,

I am running into a strange delay with Serial receive using the Arduino Serial2.read() method on a TTGO ESP32 LoRa board. I am using Arduino IDE since that is giving me easy access to the OLED display and the LoRa receiver on the board. My goal is to use the board to get me a long range remote control for a Zoom HN4 Pro recorder.
The Zoom remote works via a serial interface, 3.3V, normal Serial UART with 2400Baud, no parity, 1 stopbit. This is easily connected with one of the serial ports of the ESP32 board. The protocol is fairly straightforward, but due to the delay of the read function of the ESP the connection does not get established. Here in short the protocol:
The remote initiates the connection by sending 0x00 bytes until the Zoom responds. The original remote does this in chunks, it sends a couple bytes at about 1 byte every 30ms, then waits a couple 100ms, then repeats, until the Zoom answers. The Zoom is answering with a 0x80 byte, after which it expects a 0xa1 from the remote. It then answers with its status information. The problem I am having now is this: My code is sending 5 0x00 bytes at about 30 ms spacing, then waits a while, before it sends the next. All of this is done in the loop() function. Also in the loop() is the receive part, in each cycle it checks the receive buffer for available bytes, if there is anything available, it reads it. If it receives a 0x80 it "immediately" sends out the 0xa1 to the Zoom, hoping to establish the connection. Now here is the catch:
While the read() and the write() are basically immediately following each other, I can see on the scope that there is a 50ms delay between the byte being sent out by the Zoom and the answer from the ESP32. I verified by some variations in the code that it is indeed the receive buffer that apparently waits for some time before the byte shows up in the serial2.available() result. This delay unfortunately is too large for the Zoom to respond to it, so I need to find a way to get rid of it.
Anyone have an idea?
Here is the sample code, which has been reduced to the max to just concentrate on the issue at hand:

Code: Select all

/*
 Name:        connect.ino
 Created:    20.02.2022 21:49:50
 Author:    Admin
*/

long timeLastSend = 0;
int state = 0;
int count = 0;

// the setup function runs once when you press reset or power the board
void setup() {
    Serial.begin(115200);
    Serial2.begin(2400, SERIAL_8N1, 23, 22);
    timeLastSend = millis();
}

// the loop function runs over and over again until power down or reset
void loop() {
    if (millis() > timeLastSend + 30 && state == 0) {
        if (count < 5) {
            Serial2.write(0x00);
            Serial2.flush();
            Serial.println("sending 0x00");
        }
        count++;
        if (count == 50)
            count = 0;
        timeLastSend = millis();
    }
    if (Serial2.available() > 0) {
        byte reply = Serial2.read();
        Serial.print("received: "); Serial.println(reply, HEX);
        if (state == 1) {
            // connection success!
            Serial.println("succeacsas!");
            state = 2;
        }
        if (reply == 0x80) {
            state = 1;
            Serial2.write(0xa1);
            Serial.println("reached state 1, writing 0xa1");
        }
    }
    // timeout after five seconds if connection cannot be established
    if (state == 1 && millis() > timeLastSend + 5000) {
        state = 0;
        Serial.println("timeout");
    }
}

As you can see, except for a simple serial.println statement there is basically nothing between the serial.available() and the write(0xa1), there is nothing in the loop() which could potentially hold it up, and Serial2.available() is called in every cycle. Therefore I was expecting the data to show up a few ms after it has been sent, but on the scope the time between the 0x80 from the Zoom and the 0xa1 from the ESP32 is roughly (and fairly constantly) 50ms.

I'd be grateful for any light that someone can shed on this, and even more grateful if someone can point me to a way to make this work in Arduino IDE.

Many thanks
Reiner

reiners
Posts: 2
Joined: Sun Feb 20, 2022 9:30 pm

Re: Strange Serial2 receive delay - WORKAROUND

Postby reiners » Mon Feb 21, 2022 9:22 am

Hello all,
just to let you know that I found a workaround, which does solve my issue perfectly. However, it does neither explain the receive delay nor solve it.
I am now simply using software serial to do the job by including the ESP32SoftwareSerial library in Arduino IDE. This works perfectly fine and the connection is established reliably.
So, issue solved for me, after spending many hours plus some hardware cost (for the original Zoom remote to have a reference and a USB scope to actually see what is going on). It might still be interesting to know what is causing this, but finally I got it working and I am not really going to bother any more why hardware serial is not doing the job.

Just wanted to post this in case anybody else is having a similar issue and might benefit from this workaorund.

Who is online

Users browsing this forum: No registered users and 32 guests