Needing to clear buffer for some reason

This code is on the slave side of an Uno with a soft serial port connected by a RS-485 bus. Receiving commands S,1 (Open Shade) or S,2 (Close Shade) or S,3 (Status of shade position) determines the action to take.
I was seeing a second duplicate command which caused issues until I started clearing the serial buffer after a command was executed. It is not being sent twice. I am scratching my head as to how the duplicate command is being generated. The flush is working but why might I be needing it?

/******* READ SERIAL PORT FOR DATA COMMAND FROM XOJO *********/
void RS485SerialCheck()  {
  inputString = "";// Clear string to start
  
  while (RS485Serial.available()) {
    // get the new byte:
    char inChar = (char)RS485Serial.read();
    // add it to the inputString:
    inputString += inChar;
 
    if ((inChar == '\r') && ((inputString.charAt(0)) == 'S')) { // Carriage Return CR

      //******Open Shutter Command******
      if ((inputString.charAt(2)) == '1') {
        findWestClosedlimit(); //Find closed first
        delay(400);
        findWestOpenlimit();
        delay(400);
        findEastClosedlimit(); //Find closed first
        findEastOpenlimit();

        while (RS485Serial.available ()) //Flush port
          RS485Serial.read ();
      }

      //******Close Shutter Command******
      else if ((inputString.charAt(2)) == '2') {
        findWestClosedlimit();
        delay(400);
        findEastClosedlimit();

        while (RS485Serial.available ()) //Flush port
          RS485Serial.read ();
      }
      //******Data Return******
      if ((inputString.charAt(2)) == '3') {
        ReturnDataToXOJO();

         while (RS485Serial.available ()) //Flush port
          RS485Serial.read ();
      }
    }
  }
}

UNO, Strings, += message collection, delay for timings, only a snippet shown,
looks like bad code to me, but it's only a snippet.

Edit: Oh, I forgot the dropping of messages.

Then what would good code look like for receiving data? This was an example I used from the Arduino examples.

I use something like the following to call oneLineReceived when a line from Serial is complete.

void handleSerial() {
  static uint8_t bIndex;
  static uint8_t buffer[maxMsgLen + 1];
  bool lineReady = false;
  if (Serial.available()) {
    uint8_t inChar = Serial.read();
    if (inChar != cbLF) {
      if (inChar == cbCR) {
        lineReady = true;
      } else {
        buffer[bIndex++] = inChar;
        lineReady = (bIndex == maxMsgLen);
      }
      if (lineReady) {
        buffer[bIndex] = 0;
        oneLineReceived((const char*)buffer);
        bIndex = 0;
      }
    }
  }
}

The Serial Basics post was a jewel for me and it solved my need to clear out the buffer. My question is since we're now working with C Strings how do I find what a particular Chr is in a c string as you might with a string?

I did assign a (String inputString = "") to the chr string and the below works but there must be a better approach.

if ((inputString.charAt(1)) == '2')

You can directly access the character that is part of the c-string.

if (chrArr[0] == 'X') if the char array starts with a 'X'.

There are a ton of utility functions that work on/with c-strings aka zero terminated char arrays.

https://cplusplus.com/reference/cstring/strlen/

Yes, I just discovered it and it works. I'll check the link out to get better acquainted. Thanks for all your help. I believe I'm back on track for the moment. C++ is very new to me.

That is a link I did need. Thanks

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.