레이싱 시뮬레이터를 개발 중이고 문제가 있습니다. 데이터를 Arduino로 보내는 C++ 프로그램이 있습니다. Arduino가이를 받아 들여 문자열을 파싱하고 그 하위 문자열 중 하나를 TFT에 표시해야합니다. 나는 Arduino가 C++ 프로그램이 끝날 때 TFT의 값만 출력하므로 Serial.read()
함수가 false를 반환 할 때만 값을 출력한다는 것을 알았습니다. 실제 값을 인쇄하려면 어떻게해야합니까? 여기Arduino Serial 연결이 끊긴 상태에서 읽음
내가 당신에게 아두 이노 코드를 가지고 : 나는이 부분 변경을 시도
#include <SoftwareSerial.h>
#include <Adafruit_GFX.h>
#include <UTFTGLUE.h>
UTFTGLUE myGLCD(0x9488, A5, A4, A3, A2, A0);
char lastgear = '0';
extern uint8_t Bigfont[];
String sParams[3];
int iCount, i;
String sLine;
void setup() {
randomSeed(analogRead(5)); //.kbv Due does not like A0
pinMode(A0, OUTPUT); //.kbv mcufriend have RD on A0
digitalWrite(A0, HIGH);
// Set up the LCD
myGLCD.InitLCD();
myGLCD.clrScr();
myGLCD.setFont(BigFont);
myGLCD.setTextSize(5);
myGLCD.print(" Victor Casado", LEFT, 15);
delay(500);
myGLCD.clrScr();
myGLCD.setTextSize(24);
Serial.begin(9600);
}
void loop() {
while (Serial.available() > 0) {
// reading the line from file
sLine = Serial.readString();
// parse only if exists
if (sLine.length() > 0) {
// parse the line
iCount = StringSplit(sLine, ',', sParams, 3);
// print the extracted paramters
for (i = 0; i < iCount; i++) {
Serial.print(sParams[i]);
myGLCD.setColor(255, 255, 255);
myGLCD.print(sParams[0], 200, 75);
}
Serial.println("");
}
}
}
int StringSplit(String sInput, char cDelim, String sParams[], int iMaxParams) {
int iParamCount = 0;
int iPosDelim, iPosStart = 0;
do {
// Searching the delimiter using indexOf()
iPosDelim = sInput.indexOf(cDelim, iPosStart);
if (iPosDelim > (iPosStart + 1)) {
// Adding a new parameter using substring()
sParams[iParamCount] = sInput.substring(iPosStart, iPosDelim - 1);
iParamCount++;
// Checking the number of parameters
if (iParamCount >= iMaxParams) {
return (iParamCount);
}
iPosStart = iPosDelim + 1;
}
} while (iPosDelim >= 0);
if (iParamCount < iMaxParams) {
// Adding the last parameter as the end of the line
sParams[iParamCount] = sInput.substring(iPosStart);
iParamCount++;
}
return (iParamCount);
}
: 여기에
while (Serial.available() > 0) {
// reading the line from file
sLine = Serial.readString();
// parse only if exists
if (sLine.length() > 0) {
// parse the line
iCount = StringSplit(sLine, ',', sParams, 3);
// print the extracted paramters
for (i = 0; i < iCount; i++) {
Serial.print(sParams[i]);
myGLCD.setColor(255, 255, 255);
myGLCD.print(sParams[0], 200, 75);
}
Serial.println("");
}
}
을하고는 동일하게 작동합니다 :
while (Serial.available() > 0) {
// reading the line from file
sLine = Serial.readString();
// parse only if exists
if (sLine.length() > 0) {
// parse the line
iCount = StringSplit(sLine, ',', sParams, 3);
// print the extracted paramters
for (i = 0; i < iCount; i++) {
Serial.print(sParams[i]);
}
Serial.println("");
}
myGLCD.setColor(255, 255, 255);
myGLCD.print(sParams[0], 200, 75);
}
그리고 :
while (Serial.available() > 0) {
// reading the line from file
sLine = Serial.readString();
// parse only if exists
if (sLine.length() > 0) {
// parse the line
iCount = StringSplit(sLine, ',', sParams, 3);
// print the extracted paramters
for (i = 0; i < iCount; i++) {
Serial.print(sParams[i]);
}
Serial.println("");
myGLCD.setColor(255, 255, 255);
myGLCD.print(sParams[0], 200, 75);
}
}
희망을 보내주세요.
감사합니다.
스트림이 끝날 때가 아니라 (프로그램이 끝날 때까지) 특정 터미네이터 문자가 나타날 때까지 문자열을 읽으려면'Serial.readStringUntil()'을 사용하십시오. –
'Serial.read()'는 false를 반환 할 수 없습니다. 정수를 반환합니다. –
안녕하세요, readStringUntil 함께 시도하고 데이터를 잡는 완벽하게 작동하지만 첫 번째 입력 예를 들어 "2"때, 그래서 "2", 변경에도 불구하고 계속 표시됩니다 직렬 새로 고치지 않습니다. 변수의 값 –