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.