您好,提前谢谢您的帮助。
我有一个应用程序,读取速度,转速和齿轮值从BeamNG驱动器和串行打印到一个COM端口。它在循环中工作,使用结束字节分隔值。
RPM_END_BYTE = '+';
SPEED_END_BYTE = '=';
GEAR_END_BYTE = '!';
string.toInt();
转换它。这是程序串行输出的一个循环的样子:
02500+120=6!
我还使用了下面的代码,当我通过串行监视器输入数字并以“+”结束时,该代码可以工作,但它与我的软件的工作方式不同。
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
#include <LiquidCrystal.h>
boolean newData = false;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
}
void loop() {
recvWithEndMarker();
showNewData();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '+';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '+'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
lcd.setCursor(0, 1);
lcd.clear();
lcd.print(receivedChars);
newData = false;
}