Reading serial data - from device that is breaking up streams

I have simplified my code to a very simple program and I discovered something interesting when communicating with an old device via serial.

  • For those who are interested, i am communicating with an ISCO 6710 -

The issue is that I want to read serial data into a character array and then do fun things with the array.
I discovered that the device looks like it is sending the data in discontinuous streams.
So, while I expect to get:

Command: ??**Model 6712 HW Rev:A1 SW Rev:2.31.0000 ID 112808 5203>
Check_Protocol
got

Instead I get:

Command: ?
Check_Protocol
Command: ?
Check_Protocol
Command:

Check_Protocol
Command:

Check_Protocol
Command: *
Check_Protocol
got*
Command: **
Check_Protocol
Command: Mod
Check_Protocol
Command: el 671
Check_Protocol
Command: 2 HW
Check_Protocol
Command: Rev:
Check_Protocol
Command:
Check_Protocol
Command: A1 SW
Check_Protocol
Command: Rev:
Check_Protocol
Command: 2.31
Check_Protocol
Command: .0000
Check_Protocol
Command: ID 1
Check_Protocol
Command: 12808
Check_Protocol
Command: 5203
Check_Protocol
Command:

Check_Protocol
Command: >

Check_Protocol
Command:

Check_Protocol
Command:

Check_Protocol
Command:

Check_Protocol

Now. I think I've gotten it to almost work (it spits the whole string) but I want to make sure that there isn't perhaps an easier or more elegant way of dealing with the broken up strings and I have a big problem! The Check_Protocol function is always being called for some reason.

Here is the program:

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  // set the data rate for the SoftwareSerial port
  Serial2.begin(19200);
//  delay (100);

}


void loop() // run over and over
{

   if (Serial.available())
    Serial2.write(Serial.read()); // need to send "???" to initiate communication

  char inSerial[90];   
  int i=0; 
//  delay(1000); 
//Do I just need a delay to make it work? 
//This program is very simplified, so I would have delays all over my "real" program.
  
//  The line below is designed to go until the end of the reading. Seems like a kludge
 if (Serial2.available() > 0)   // This seems redundant, yes. makes it so check protocol isn't called continuously
  {             
  while(Serial2.peek() != '>') // I know that when I get a ">" I should be done...I'm not sure it will work for all my returns 
// but it should work for most.
   {
     while (Serial2.available() > 0) {
         inSerial[i]=Serial2.read(); //read data  
         i++;      
       }
       inSerial[i]='\0'; 
      Check_Protocol(inSerial);  // the printed string should get longer and longer
   }    
 }
}


void Check_Protocol(char inStr[])
{   
  Serial.print("Command: ");
  Serial.println(inStr);
       
  Serial.println("Check_Protocol");
  if(!strcmp(inStr,"*")) Serial.println("got*");
//  if(!strcmp(inStr,"")) Serial.println("off");
}
   if (Serial.available())
    Serial2.write(Serial.read()); // need to send "???" to initiate communication

That would take a minimum of three executions of loop() to send, though it is likely to take far more than that.

 if (Serial2.available() > 0)   // This seems redundant, yes. makes it so check protocol isn't called continuously
  {             
  while(Serial2.peek() != '>') // I know that when I get a ">" I should be done...I'm not sure it will work for all my returns 
// but it should work for most.
   {
     while (Serial2.available() > 0) {
         inSerial[i]=Serial2.read(); //read data  
         i++;      
       }
       inSerial[i]='\0'; 
      Check_Protocol(inSerial);  // the printed string should get longer and longer
   }    
 }

The inconsistent placement of the { does not make for readable code. The inconsistent indenting does not help, either.

But the biggest problem here is that you don't seem to understand just how slow serial data arrives. Kind of like how I type. Ssslllooowwwlllyyy.

You seem to think that the whole packet will arrive at once. It most certainly will not.

You need to read data, as it arrives, and save it, until the '>' arrives. Only then should you call Check_Protocol().