Trouble Flushing Incoming Serial Data

Hello,

So I am trying to communicate with an AliCAT (Mass Flow Meter) that sends out RS232 data to my particle photon but just cant quite get it to work. The AliCAT is sending out strings that are 38 characters long, I currently have it in streaming mode so it is continuously sending out these big strings. My end goal is to collect one of these strings every five minutes and record it to an sd card, but I am getting stuck at the collection part. My code so far looks like this, the idea was to have the particle look for data every 5 seconds (shortened from 5min to aid troubleshooting) and record what it finds if the index becomes larger than 4 (again, shortened for troubleshooting). Troubleshooting via Putty and a RS232-usb connected to a RS232 to TTL and sending it strings

char incomingByte = 0;   // for incoming serial data
char inData[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte Index = 0; // Index into array; where to store the character


     void setup()
       {
        Serial1.begin(9600, SERIAL_8N1);
        }
    void loop()
     while (Serial1.available() > 0)
      {
     if(Index < 19) // One less than the size of the array
        {
             inChar = Serial1.read(); // Read a character
             inData[Index] = inChar; // Store it
             Index++; // Increment where to write next
             inData[Index] = '\0'; // Null terminate the string
        }
                   if (Index > 4)
          {
               Particle.publish("It Went!",inData);
             Index=0;
             delay(5000);
            }
            
            Serial1.print("I received: "); Serial1.println(inData);
      }
     }        

The problem I am running into is that even when the index goes above 4 and it prints, the extra data is not flushed. So if I send the string “Helloooooo”, it will print “Hello” but then go on to print “ooooo” as well. What I need is for it to print “Hello” then forget the rest until 5 seconds is up, then I want it to start looking for a fresh set of new data. Any ideas here? I’ve tried things like .fflush and .clear but neither seem to work.

Another option I have is I can set the AliCAT to only send a string of data when I send it, for example, “B” then hit enter. Here I was having trouble getting it to “hit enter”, this is what I tried:

 if(Index < 1)
        {
         Serial1.print("B");
        Serial1.write(0x0D);
        }

Is there a better way to do this? Thanks for your input!

Danny

I usually use this to flush the RX buffer

  while(Serial1.read() >= 0);

This just dumps the whole remaining content immediately without even looking at it.

1 Like

That did not work, it ended up giving me a bunch of garbage characters, image attached. Why would it do that?

All I did was switch “while (Serial1.available() > 0)” for “while(Serial1.read() >= 0);”

As I said, this statement flushes out all data that’s in the RX buffer, so there won’t be anything left to read and the “garbage” you read there is in fact the -1 value Serial1.read() gives you when you try to read from an empty buffer.

You yourself asked how to get rid of extra data after you got what you wanted, and my code was the answer to that question.
So if you keep your original code and put my statement after the portion wher your desired payload got read, you’ll have what you asked for.

2 Likes

Ah, I see. That worked, thank you

1 Like