First commands from TerraTerm to Arduino contain junk characters. How to remove them?

46 views Asked by At

I have an Arduino Uno Program that takes a string (a command) send via Telnet from a TeraTerm Console to set I/O Expanders. The first 4-5 strings I send always contain junk characters before the actual string and thus fail to function. Afterwards it works fine. When I send a command after a longer period of idleness (~ 20 minutes) the first string contains junk characters too, but further commands work as expected. What can I do to determine where the issue lies and how to remedy it? The final Arduino program is supposed to get commands from LabVIEW, but I can’t test that yet. I am very new to Arduino and programming in general. If there are any guides or resources that explain this I’d be glad if you send them my way.

I have tried a client.flush() but it doesn’t appear to be doing anything. I am not sure what I could try. The web hasn’t given me any answers.

Here is a stripped down version of the Arduino program I am using that has the exact issue:

#include <SPI.h>
#include <Ethernet.h>

//Mac-Adresse des Arduino
byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x6D, 0x65 };
IPAddress ip(192,168,206,225);
IPAddress gateway(192,168,204,1);
IPAddress subnet(255, 255, 205, 20);
// Telnet standard port 23
EthernetServer server(23);
boolean alreadyConnected = false;

String commandString = "";
void setup() {
delay(200);
    Serial.begin(9600);

  //Server Setup
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  while (!Serial){;}
      Serial.print("- Arduino's IP address   : ");
      Serial.println(Ethernet.localIP());
      Serial.print("- Gateway's IP address   : ");
      Serial.println(Ethernet.gatewayIP());
      Serial.print("- Network's subnet mask  : ");
      Serial.println(Ethernet.subnetMask());
      Serial.print("- DNS server's IP address: ");
      Serial.println(Ethernet.dnsServerIP());
  //Setup Ausdruck
  if (Ethernet.begin(mac) == 0) 
  {
    Serial.println("Keine IP-Adresse erhalten.");

    // check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware)
      Serial.println("Ethernet Schild nicht gefunden.");

    // check for Ethernet cable
    if (Ethernet.linkStatus() == LinkOFF)
      Serial.println("Ethernet Kabel nicht verbunden.");
  }
      Serial.print("- Arduino's IP address   : ");
      Serial.println(Ethernet.localIP());
      Serial.print("- Gateway's IP address   : ");
      Serial.println(Ethernet.gatewayIP());
      Serial.print("- Network's subnet mask  : ");
      Serial.println(Ethernet.subnetMask());
      Serial.print("- DNS server's IP address: ");
      Serial.println(Ethernet.dnsServerIP());

}

void loop() {
 EthernetClient client = server.available();
  if (client) 
    {
      if (!alreadyConnected) 
        {
          // clear out the input buffer:
          client.flush();    
          commandString = ""; //clear the commandString variable
          server.print("--> Please type your command and hit Return...");
          alreadyConnected = true;
        }
      if(client.available()) 
        { 
          char newChar = client.read();  // read the bytes incoming from the client:
          if (newChar == '\r') //If a 0x0D is received, a Carriage Return, then evaluate the command
            {  
              server.print("Received this command: ");
              server.println(commandString);
              Serial.print("Recieved this command:");
              Serial.println(commandString);
              commandString += '\r';
              commandString = "";
            } 
          else
            {
            commandString += newChar; //Serial.print(commandString);
            }
        } 
    }
}

Serial Monitor Output shows the junk characters, but the TeraTerm client doesn't.

1

There are 1 answers

1
user23736704 On

I found a solution, also I am not quite sure why it works. On the same day I asked the question this article was written: https://devcodef1.com/news/1187718/removing-junk-characters-in-arduino-uno-s-commands

Using the structure described there I built a bit of code that works.

void loop() {
 EthernetClient client = server.available();

  if (client) 
    {
      if (!alreadyConnected) 
        {
          // clear out the input buffer:
          client.flush();    
          incoming = ""; //
          server.print("--> Please type your command and hit Return...");
          alreadyConnected = true;
        }
      if(client.available()>0){
        while(client.available()>0){
          incoming += (char)client.read();
          }
        if (incoming.length()>4){
          commandString = incoming.substring(0);
          incoming = "";
          processCommand(commandString);
          }
      }
    }
}

It appears that the junk characters, as displayed in the picture attached to my original question, are contrary to appearances only 4 characters. This code sends the junk characters as it's own command to the arduino where they are immediately rejected. Then normal commands can be issued. The only problem that persists is that periods of idleness result in a string with some junk characters once.