我有一个 Arduino Uno 程序,它采用从 TeraTerm 控制台通过 Telnet 发送的字符串(命令)来设置 I/O 扩展器。我发送的前 4-5 个字符串在实际字符串之前总是包含垃圾字符,因此无法运行。之后就可以正常工作了。当我在较长一段空闲时间(约 20 分钟)后发送命令时,第一个字符串也包含垃圾字符,但其他命令按预期工作。我可以做什么来确定问题所在以及如何解决? 最终的 Arduino 程序应该从 LabVIEW 获取命令,但我还无法测试。 我对 Arduino 和一般编程非常陌生。如果有任何指南或资源可以解释这一点,我会很高兴您将它们发送给我。
我尝试过 client.flush() 但它似乎没有做任何事情。我不确定我可以尝试什么。网络还没有给我任何答案。
这是我正在使用的 Arduino 程序的精简版本,它具有确切的问题:
#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);
}
}
}
}
我找到了一个解决方案,但我也不太确定它为什么有效。同一天我问了这篇文章写的问题: https://devcodef1.com/news/1187718/removing-junk-characters-in-arduino-uno-s-commands
使用此处描述的结构,我构建了一些有效的代码。
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);
}
}
}
}
看来,垃圾字符,如我原来问题所附图片所示,与外观相反,只有 4 个字符。此代码将垃圾字符作为它自己的命令发送到 arduino,并立即被拒绝。然后就可以发出正常的命令了。唯一持续存在的问题是,一段时间的空闲会导致字符串中出现一些垃圾字符。