Arduino 到 C++ 串行读取胡言乱语

Arduino to C++ Serial Reads Gibberish

本文关键字:读取 胡言乱语 C++ Arduino      更新时间:2023-10-16

我正在开发一个C++程序,与运行自动化系统的Arduino进行通信。 我正在使用以下串行库:

https://playground.arduino.cc/Interfacing/CPPWindows

我正在尝试向Arduino发送一个字符串,然后将其解析为一个命令,该命令将告诉它系统应该如何移动。 从串行读取命令的代码是这样的:

'void serialEvent(( {

while (Serial.available()) {
char in = (char)Serial.read();
input.concat(in);
}
int endChar = input.indexOf('r');
if (endChar != -1) {
//inputLog.concat(input);
//inputLog.concat('n');
String command = input.substring(0,endChar);
command.toLowerCase();
input.remove(0, endChar+1);
String keyword = command.substring(0,4);
if(keyword == "move"){
int i = command.indexOf('x');
if (i != -1) {
endChar = command.indexOf(i, ',');
xMove = command.substring(i + 1, endChar).toFloat();
}
i = command.indexOf('y');
if (i != -1) {
endChar = command.indexOf(i, ',');
yMove = command.substring(i + 1, endChar).toFloat();
}
} else if (keyword == "stop") {
xMove = 0;
yMove = 0;
} else if (keyword == "xpos") {
Serial.print("X: ");
Serial.println(xPos);
} else if (keyword == "ypos") {
Serial.print("Y: ");
Serial.println(yPos);
} else if (keyword == "getp") {
Serial.print("X: ");
Serial.print(xPos);
Serial.print(", Y: ");
Serial.println(yPos);
} else if (keyword == "setx") {
xPos = command.substring(4).toFloat();
} else if (keyword == "sety") {
yPos = command.substring(4).toFloat();
} else if (keyword == "setp") {
int i = command.indexOf('x');
if (i != -1) {
endChar = command.indexOf(i, ',');
xPos = command.substring(i + 1, endChar).toFloat();
}
i = command.indexOf('y');
if (i != -1) {
endChar = command.indexOf(i, ',');
yPos = command.substring(i + 1, endChar).toFloat();
}
} else if (keyword == "getl") {
Serial.println(inputLog);
inputLog = "";
} else {
Serial.println("error: 1");
return;
}
Serial.println("okr");
//Serial.print(nc);
}

}'

这部分似乎运行正常,因为我可以使用Arduino串行监视器运行它并且它可以工作。 以下是我用C++编写的代码的简短测试:

while (SP->IsConnected())
{
string outputData = "";
cin >> outputData;
outputData.append(pcr);
//outputData.append(pnc);
writeResult = SP->WriteData((char*)outputData.c_str(), outputData.length());
//writeResult = writeResult && SP->WriteData(pnc, 1);
//writeResult = writeResult & SP->WriteData(pcr, 1);

while (true) {
Sleep(500);
char incomingData[256] = "";
readResult = SP->ReadData(incomingData, dataLength);
//printf("Bytes read: (0 means no data available) %in", readResult);
//incomingData[readResult] = 0;
input.append(incomingData, 0, readResult);
check_ok = input.find(pcr);
check_error = input.find(error);
if (check_ok != string::npos || check_error != string::npos || readResult == 0) {
cout << "Receiving: " << input << 'n';
input.clear();
break;
}
}
}

变量 pcr 在代码前面被定义为指向回车符的指针,Arduino 使用它来标识命令的结尾。 我运行此代码的结果是 Arduino 似乎读出了第一个命令并执行它;但是,它也读取了几个乱码字符,其中大多数等于 -52 作为数值。 任何后续命令似乎都不起作用,arduino 继续获取胡言乱语字符。此外,C++中的读取数据功能似乎工作正常。 有谁知道为什么会这样? Arduino的串行缓冲区是否以某种方式被WriteData功能损坏?

<小时 />

已解决

我似乎发现了这个问题。 语句 outputData(pcr( 需要是 outputData(pcr, 0, 1(,因为指针不是空终止的。 因此,字符串附加额外的字符,直到达到空字符。

您的数据线有多长? 您是否使用 TTL 级别发送数据? 如果您的线路长度超过几米,并且您使用的是TTL级通信(直接在设备之间布线(,那么您肯定会收到来自其他设备的垃圾。长线通信线路应改用RS232或RS485,这是专门为其设计的。