断开连接时的Arduino串行读取

时间:2017-12-02 10:21:49

标签: c++ api arduino communication

我正在开发一个赛车模拟器,我有一个问题。我有一个将数据发送到Arduino的C ++程序。 Arduino接收它,解析字符串,其中一个子串必须显示在TFT中。我注意到,当C ++程序完成时,Arduino只会在TFT中打印该值,因此它仅在Serial.read()函数返回false时打印该值。如何获取实时数据,以打印实时值?

在这里,我为您带来了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);
  }
}

希望你能帮助我!

谢谢!

1 个答案:

答案 0 :(得分:0)

使用Serial.readString()时,它会尝试读取,直到超时为止。 串行上的默认超时为1秒。这意味着在读完最后一个字符后,它等待1秒。在完成之前更多。您可以在Serial.setTimeout(10);之后使用Serial.begin()或在@Sami建议后使用Serial.readStringUntil("xxx")手动设置设置超时值,您可以使用editTextStyle其中'xxx'是您的字符串的结尾发送fx。 '\ n'或'\ r \ n',如果您使用换行符终止字符串。