我的 arduino 正在根据这个程序给出输出——其中一部分是
Serial.print("A");
Serial.print(sensorValue);
Serial.print("B");
Serial.println();
Serial.print("C");
Serial.print(sensorValue1);
Serial.print("D");
Serial.println();
在arduino的串行监视器中的输出显示为C491D A192B C49D A192B C484D A196B C482D A193B C483D A199B C485D A196B C486D A198B
现在我的 Node.js 运行以下代码
var cleanData = ""; // this stores the clean data
var cleanData1 = "";
var readData = ""; // this stores the buffer
sp.on("data", function (data) { // call back when data is received
readData += data.toString(); // append data to buffer
// console.log(readData);
// if the letters "A" and "B" are found on the buffer then isolate what"s in the middle
// as clean data. Then clear the buffer.
if (readData.indexOf("D") >= 0 && readData.indexOf("C") >= 0) {
cleanData1 = readData.substring(readData.indexOf("C") + 1, readData.indexOf("D"));
readData = "";
console.log(cleanData1);
io.sockets.emit("message", cleanData1);
}
else if(readData.indexOf("B") >= 0 && readData.indexOf("A") >= 0)
{
cleanData = readData.substring(readData.indexOf("A") + 1, readData.indexOf("B"));
readData = "";
console.log(cleanData);
io.sockets.emit("message", cleanData);
}
});
相同的控制台读数没有给出预期的结果 D A181B C D A181B C 181 462 181 462 181 462 181 462 181 462 462 462 462 462 462 462 462 462 462 462 462 462 462
从控制台读数很明显,读数不如预期。读数应该是一个以 4 开头,另一个以 1 开头。实际上有控制台读数,例如
D A181B C D A181B C D A181B C D A181B C D A181B C
这根本不应该出现如果我阻止了一个 If 语句块,那么任何一个块显示的读数都是完美的。
我哪里错了?