Page 1 of 1

Serial2 echoing incoming data

Posted: Sun May 10, 2020 10:33 pm
by montyx
Hello!

Honestly I feel myself so dumb, because I cannot figure it out, what I've made wrong. :oops:
I try to implement a simple Telnet - UART communication, but when I send a character with telnet, the UART sends it back.

Code: Select all

WiFiServer server(23);
WiFiClient serverClient;

void setup() {
    Serial.begin(115200);
    delay(5000);
    Serial.println("\nConnecting");

        Serial.println("Connecting Wifi ");
        WiFi.begin(ssid, password);
        while (WiFi.status() != WL_CONNECTED) {
            delay(500);
            Serial.println("Connecting to WiFi...");
        }

        Serial.println("");
        Serial.print("WiFi connected ");
        Serial.print("IP address: ");
        Serial.println(WiFi.localIP());

    //start UART and the server
    Serial2.begin(57600);
    server.begin();
    server.setNoDelay(true);

    Serial.print("Ready! Use 'telnet ");
    Serial.print(WiFi.localIP());
    Serial.println(" 23' to connect");
}

void loop() {
    //check if there are any new clients
    if (server.hasClient()){
        //find free/disconnected spot
        if (!serverClient || !serverClient.connected()){
            if(serverClient)
                serverClient.stop();

            serverClient = server.available();
            if (!serverClient)
                Serial.println("available broken");
            else {
                server.available().stop();
            }

            Serial.print("New client: ");
            Serial.println(serverClient.remoteIP());
        }
    }

    //check telnet clients for data
    if (serverClient && serverClient.connected()){
        if(serverClient.available()){
            //get data from the telnet client and push it to the UART
            while(serverClient.available())
                Serial2.write(serverClient.read()); // This triggers the Serial2.available() method below
            serverClient.flush();
        }
    }
    else {
        if (serverClient) {
            serverClient.stop();
        }
    }

    //check UART for data
    if(Serial2.available()){
        size_t len = Serial2.available();
        uint8_t sbuf[len];
        Serial2.readBytes(sbuf, len);
        //push UART data to all connected telnet clients
        if (serverClient && serverClient.connected()){
            serverClient.write(sbuf, len);
            delay(1);
            Serial2.flush();
        }
    }
}
Please help me out, this code is much more simpler than I usually handle, and I cannot find what cause this echo.
Thanks in advance!

Re: Serial2 echoing incoming data

Posted: Mon May 11, 2020 12:58 pm
by ESP_Sprite
So you see exactly what you typed (and not e.g. every letter repeated 2 times)? Are you sure your telnet client itself does not echo the text back? As far as I can see, this is the default behaviour.

Re: Serial2 echoing incoming data

Posted: Mon May 11, 2020 6:49 pm
by montyx
ESP_Sprite wrote:
Mon May 11, 2020 12:58 pm
So you see exactly what you typed (and not e.g. every letter repeated 2 times)? Are you sure your telnet client itself does not echo the text back? As far as I can see, this is the default behaviour.
Yes I am. I've tested it, just put a Serial.println("COMM"); in the first row of the following condition:

Code: Select all

//check telnet clients for data
    if (serverClient && serverClient.connected()){
        Serial.println("COMM"); // <<< HERE
        if(serverClient.available()){
            //get data from the telnet client and push it to the UART
            while(serverClient.available())
                Serial2.write(serverClient.read()); // This triggers the Serial2.available() method below
            serverClient.flush();
        }
    }
After that when I send a character in Telnet, the Serial Monitor writes a "COMM" and the character.

Re: Serial2 echoing incoming data

Posted: Mon Nov 22, 2021 8:54 am
by mostafahk
I have same problem.
I'm using ModbusRTU library in Serial2, and it doesn't works correctly, But it works with Serial0.
I've spend lot of time to realize there is some junk in RX buffer when you didn't received byte on RX pin.
This problem is always in Serial2.
I've made some changes in Modbus library to clear RX buffer after transmitting data and it's works correctly, but the problem is somewhere else.