Sending commands through the serial port with control response

Hey everyone :slight_smile: ; I have a problem with my program arduino and I need some help :
I'm trying to send commands (from a text file in a memory card (SD card)) through the serial port with control response: This file contains the commands and expected answers as follows:
command1
Reply1
command2
Answer2
the number of command is 28 and the answers either. So I want to send each command and I expect an answer received by the serial port, I compare it with the expected response that is already stored in the text file, if they are equal I pass the following command if I stop the shipment. But when I compile I still have a problem in reading the responses received :confused: only Reply1 is received and compared, and then it goes to 2nd but then it is sent to Answer2 is not received and the compilation stops. However, if I put the read loop of replies in a comment, the 28 commands are normally sent. Is there anyone who can help me to solve this problem ?? thank you :slight_smile: and
here is my code:
void loop()
{
while (my_file)
{
if (repp==false)
{
do
{
c= my_file.read();
command+=c;
}
while(c !='\n');
do
{
c= my_file.read();
expectedresponse+=c;
}
while(c !='\n');
{
Serial.println(command);
//Serial.println(expectedresponse);
do
{
if (Serial.available()>0 )
{
in=Serial.read();
responsesreceived +=in;
}
}
while(in != '\n');
Serial.println(responsesreceived);

if (responsesreceived !=expectedresponse)
{
repp=true;
}
expectedresponse="";
command="";
responsesreceived="";
}
}
}
}

here is my code:

That is some of the code. So, here is some of the answer. Don't use the String class. You need to, instead...

sarrah:

void loop()

{
while (my_file)
{
if (repp==false)
{
do
{
c= my_file.read();
command+=c;
}
while(c !='\n');
do
{
c= my_file.read();
expectedresponse+=c;
}
while(c !='\n');
{
Serial.println(command);
//Serial.println(expectedresponse);
do
{
if (Serial.available()>0 )
{
in=Serial.read();
responsesreceived +=in;
}
}
while(in != '\n');
Serial.println(responsesreceived);

if (responsesreceived !=expectedresponse)
{
repp=true;
}
expectedresponse="";
command="";
responsesreceived="";
}
}
}
}

Is that partial code supposed to somehow be better?

What
is
with
the
stupid
indentation?

#include <SD.h>
File my_file;
String command;
String expectedresponse;
String responsesreceived;
boolean rep;
boolean repp;
char in;
char c;
int i;
void setup ()
{
Serial.begin(9600);
  if (!SD.begin(4)) 
     {
Serial.println("a startup error");
return;
     }
Serial.println("SD card");
Commande_Valeo= SD.open("CMD25.txt",FILE_READ);
if (Commande_Valeo) 
    {
Serial.print("done");
    } 
else 
    {
Serial.println("Error");
    }
}void loop()
{
while (my_file) 
{
if (repp==false)
{
do
{ 
c= my_file.read(); 
command+=c;
}
while(c !='\n');
do
{ 
c= my_file.read(); 
expectedresponse+=c;
}
while(c !='\n');
{
Serial.println(command);
//Serial.println(expectedresponse);
do
{ 
if (Serial.available()>0 )
{ 
in=Serial.read();
responsesreceived +=in; 
} 
} 
while(in != '\n');
Serial.println(responsesreceived);

if (responsesreceived !=expectedresponse)
{
repp=true;
}
expectedresponse="";
command="";
responsesreceived="";
}
}
}
}

i've just put the full code PaulS .
I really need some help and I'm sorry about the some of the code because it's my first time here .
So , I need what instead ? would you please help me and thanks.....

So , I need what instead ?

The first thing you need is to use Tools + Auto Format, to fix that horrid indenting.

}void loop()

What is this garbage? NOTHING follows a }. EVER!

Serial.println(command);
//Serial.println(expectedresponse);
do
{
if (Serial.available()>0 )
{

How long are you waiting for a response? Is there a snowball's chance in hell that there will be a reply 65 nanoseconds after the command is buffered (NOT sent)?

You need to make your response system send a response with a start marker, the reply, and an end marker.

Then, you need to do a while loop, that does nothing until the start of the reply is received.

When that happens, you have another while loop that reads and stores data until the end marker arrives.

If you can't control what the other device is sending, you really haven't a hope of doing what you want to do.