Serial2 echoing incoming data

montyx
Posts: 2
Joined: Sun May 10, 2020 10:27 pm

Serial2 echoing incoming data

Postby montyx » Sun May 10, 2020 10:33 pm

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!

ESP_Sprite
Posts: 9709
Joined: Thu Nov 26, 2015 4:08 am

Re: Serial2 echoing incoming data

Postby ESP_Sprite » 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.

montyx
Posts: 2
Joined: Sun May 10, 2020 10:27 pm

Re: Serial2 echoing incoming data

Postby montyx » Mon May 11, 2020 6:49 pm

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.

mostafahk
Posts: 4
Joined: Tue Dec 29, 2020 6:07 am

Re: Serial2 echoing incoming data

Postby mostafahk » Mon Nov 22, 2021 8:54 am

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.

Who is online

Users browsing this forum: No registered users and 84 guests