Serial.read: interrupted string

Hi guys,

for my next project I like to implement a balance to a automated dosing system controlled by an Arduino Mega2560. My first Step is to implement a stable serial connection between arduino & a load cell amplifier and Arduino & PC. Unfortunately the read data string from the load cell amplifier is broken sometime:

S+000246
S+000246
S+000246
S+000246
S+0002
46
S+000246
S+000246
S+000246
S+000246
S+000246
S+000246
S+00
0246
S+000246
S+000246
S+000246

This is my Code:

#include <stdlib.h> 

void setup() {
  
    Serial.begin(115200);
    Serial2.begin(115200);      
   
   }


void loop() {
   
         
  while (Serial.available()) {
      Serial.flush();
      character = Serial.read();
      inData.concat(character);
      }
     
    if (inData != "") {
      newCommand = true;
      }
      else {
        newCommand = false;
        }
   

     if (newCommand == true) {
         SensorOut = inData;
         
         }
         else {
           SensorOut = "GS";
           
           } 

           Serial2.println(String(SensorOut));
        
        
        delay(10);    // Delay waiting for answer

        while (Serial2.available()) {
          character = Serial2.read();
          SensorIn.concat(character);
          }
        
        if ( SensorIn.length() > 1 ) {
          Serial.println(SensorIn);
      }
     
         
     SensorOut = "";
     SensorIn = "";
     newCommand = false;
       
  
}

I tried to change the delay time between sending an receiving on serial2 without any effect. The load cell amplifier ends every string with CR. I thouht the function serial.read is also waiting for this command. So I can’t understand why the string is interrupted sometimes. Any idea? Thanks a lot.

It may be this:

        delay(10);    // Delay waiting for answer

        while (Serial2.available()) {
          character = Serial2.read();
          SensorIn.concat(character);
          }

You are in that loop only while characters are available. Maybe the last few have not arrived when you emptied the rx buffer. If there is a cr/lf on each response, I would wait for that. Or put the delay(10) inside the "while(Serial2.available())" loop.

Hi SurferTim,

thank you for your quick reply. Your suggestion solved the problem. I put a 1 milli delay inside the while loop:

// delay(10);    // Delay waiting for answer
        while (Serial2.available()) {
          character = Serial2.read();
          SensorIn.concat(character);
          delay (1);
          }

Thanks a lot.

indata != ""

You can't compare strings in this way! You must use something like strcmp(indata,"")

Mark

Oh yes, thanks.

Your suggestion solved the problem

No, it masked the problem. The problem still exists.

Which version of the IDE are you using? If you are using 1.0 or later, the flush() function blocks until the outgoing buffer is empty. I fail to see how that is a useful thing to do.

If you are using 0023 or earlier, flush() dumps random amounts of unread data from the incoming buffer. After verifying that there is data, you dump it all and then proceed to read something. That makes no sense at all.

It appears that the number of characters in a packet is a constant, and that the start of a packet is a known quantity. You should read whatever is available, without a delay(). You know when a packet starts, and how long a packet is, so wasting resources using the String class is unnecessary. When the S arrives, reset index and set the 0th element of a suitably sized char array to NULL.

Then, each time a character arrives, store the character, increment the index, and store another NULL.

Only do something with the serial data when the proper number of characters has been received.

And, clearly that wasn't all of your code.

PaulS:

Your suggestion solved the problem

No, it masked the problem. The problem still exists.

Correct. As in the output of a GPS, there should be a terminating character in that string, like cr/lf or the like. That is why I recommended waiting for that character as a first choice.