Arduino 到 C++ 串行读取胡言乱语
Arduino to C++ Serial Reads Gibberish
我正在开发一个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,这是专门为其设计的。
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 使用新行和不使用新行读取文件
- 读取文件并输入到矢量中
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 无法找到/读取配置文件.conf-FileIOException
- 如何使用Luacneneneba API正确读取字符串和表参数
- C++将文本文件中的数据读取到结构数组中
- 正在将csv文件读取为双精度矢量
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 为什么在读取文件大小时文件IO速度会发生变化
- 正在读取二进制文件(is_open)
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 如何在c++中从文本文件中逐行读取整数
- SSH通过/sbin/SSH无法读取RSA密钥文件(从控制台运行)
- 独立读取-修改-写入顺序
- 从文本文件中读取时钟时间和事件时间并进行处理
- 将文本文件读取到2D数组中,但数组开始为超过数组[39][]的任何内容输出胡言乱语
- Arduino 到 C++ 串行读取胡言乱语