简体   繁体   中英

Arduino Serial Read when disconnected

I am developing a racing simulator, and I have a problem. I have a C++ program that sends data to Arduino. Arduino receives it, parses the string and one of those substrings must be displayed in a TFT. I have noticed that the Arduino only prints the value in the TFT when the C++ program finishes, so it only prints the value when the Serial.read() function returns false. How can I get the live data, to print the live values?

Here I bring you the Arduino code:

#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);
}

I have tried to change this part:

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("");
  }
}

To this, and it works the same:

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);
}

And this:

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);
  }
}

Hope you can help me!

Thanks!

When you use Serial.readString() it tries to read until it gets a timeout. The default timeout on serial is 1 sec. That means that after the last character has been read, it waits 1 sec. more before it finishes. You can set the timeout value manually in the setup by using Serial.setTimeout(10); right after Serial.begin() or as @Sami suggested, you can use Serial.readStringUntil("xxx") where 'xxx' is the end of the string that you send fx. '\\n' or '\\r\\n' if you terminate the string with a linebreak.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM