私はレーシングシミュレータを開発していますが、問題があります。私はArduinoにデータを送るC++プログラムを持っています。 Arduinoがそれを受け取り、文字列を解析し、その部分文字列の1つをTFTに表示する必要があります。私はArduinoがC++プログラムが終了したときにTFTの値だけを出力することに気づいたので、Serial.read()
関数がfalseを返すときに値を出力します。ライブデータを取得するにはどうすればよいですか?Arduinoシリアル切断時の読み取り
ここで私はあなたのArduinoのコードを持参:
#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"変数の値 –