help wuth printing string

I don’t understand why he is printing me more char then I have
I want to print only finalpart[0] and [1] - but he is printing me 5 more

can someone help&explain me please ,

thanks ,

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
int test=7;

void setup()  
{
  pinMode(test,INPUT);
  Serial.begin(19200); // Open serial communications and wait for port to open:
  delay(1500);
  Serial.println("Ready to work!");
  mySerial.begin(19200);
  delay(1500);

}

void loop() 
{
  if (digitalRead(test)==HIGH)
  {
    //for (int t=0;t<1;t++) //start of t
    //{
    char response[5];
    char finalPart[2];
    mySerial.println("AT");
    delay(1400); //the time to take to prees and leave the button
    while (mySerial.available() )
      for (int i=0;i<6;i++)
      {
        response[i]=mySerial.read();
      }//end for i
    Serial.print("this is the full respons - ");
    Serial.println(response);
    finalPart[0]=response[0];
    finalPart[1]=response[1];
    finalPart[2]=('\n');
    Serial.print("this is final - ");
    Serial.print(finalPart[0]);
    Serial.println(finalPart[1]);
    Serial.println(millis());
    Serial.println(finalPart);

    if (strcmp(finalPart,"OK")==0)
    {
      digitalWrite (13,HIGH);
      Serial.print("working!!!!!!!!!!!!!!");
      delay (1500);
      digitalWrite(13,LOW);
    }

    //Serial.println ("bla");
    // }//end for t 
  }//end if
}//end loop

the img is what happen when I press the button 1 time

1.PNG

What was wrong with your other thread?

Serial.println(finalPart);

finalPart is never explicitly null-terminated and isn't big enough to carry a null-terminator.

finalPart[2]=('\n');

Put a 3rd element in a 2 element array. Not a good idea.

while (mySerial.available() )
      for (int i=0;i<6;i++)
      {
        response[i]=mySerial.read();

“I’ve been told there’s at least one character available to read, so I’m going to read all six of them into my five character buffer”

Not good.

First of all thank you for explaining me in simple English. it’s was very helpful for me to understand what I did wrong . :slight_smile:
but I think that my logic is o.k. (fix me if I’m wrong…)
if there is at least 1 character it’s mean that the modem is alive - so I will get an answer from him
but maybe this is my mistake -
I know that the replay is "OK " - so for this I need 3 char (‘O’ ,‘K’ ,’\n’ ) -right?
and then save only the first 2 response[0] and response[1]?
so the code will be -

while (mySerial.available() )
 {       for (int i=0,i<2,i++)
        response[i]=mySerial.read();
}
char finalPart[2];
finalPart[0]=response[0];
finalPart[1]=response[1];
Serial.println(finalPart);

now it will work? (I’m not near my Arduino to see if it’s working)

if my logic is wrong - please tell me because I want to learn and understand what I’m doing and not just to do copy-past.

Thanks ,

while (mySerial.available() )
 {       for (int i=0,i<2,i++)
        response[i]=mySerial.read();
}

mySerial.available() returns the number of bytes in the buffer; you’re running a for loop inside a while loop, which means the for loop will get run the same number of times as the characters in the buffer. There is no sense in doing that. If mySerial.available() returns 1, you’ll still try to read 2 items from a buffer that only has 1 item.

char finalPart[2];
finalPart[0]=response[0];
finalPart[1]=response[1];
Serial.println(finalPart);

Serial.println() expect a null terminated array of chars. That means if you are expecting it to print “OK” then you will need to provide it with an array that looks like this: {‘O’, ‘K’, ‘\0’}. In your code, the array isn’t big enough and you never null-terminate the array.

       for (int i=0,i<2,i++)

That won’t do what you seem to think it will.

It is also incorrect in the first place.

You need to read and store every character, as it arrives, and only do something when the packet terminator arrives. You do not read serial data in a for loop. Ever.

Try this code that receives packets that start with < and end with >.

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

If you want, you can remove SOP and started, and the code that deals with them. Replace > with \n, if you want. Do NOT get rid of EOP or ended.

Where it says “Process the packet”, the data that was sent between the < and the > will be in inData.

david1234: but I think that my logic is o.k. (fix me if I'm wrong......) if there is at least 1 character it's mean that the modem is alive - so I will get an answer from him

Your pc will be sending data at 1920 characters per second (19200 bits/s / 10 bits/char), that's about 0.5 ms per character, which is actually quite slow compared to your Arduino. You check that it has received 1 character and then read it from the buffer, but then immediately try and read the second character, there will only be a few micro-seconds between reading the first character and trying to read the second character, so it won't have arrived yet. You need to wait until Serial.available==3 (or however many characters you expect) before trying to read them

still not good -
now I get nothing just a blank line

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
int test=7;


//#define SOP ' '
#define EOP '\n'
bool started = false;
bool ended = false;
char inData[80];
byte index;


void setup()  
{
  pinMode(test,INPUT);
  Serial.begin(19200); // Open serial communications and wait for port to open:
  delay(1500);
  Serial.println("Ready to work!");
  mySerial.begin(19200);
  delay(1500);

}


void loop()
{
  if (digitalRead(test)==HIGH)
  {
    mySerial.println("AT");
    // Read all serial data available, as fast as possible
    while(mySerial.available() > 0)
    {
      char inChar =mySerial.read();
   //   if(inChar == SOP)
      //{
        index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
     // }
      if(inChar == EOP)
      {
        ended = true;
        break;
      }
      else
      {
        if(index < 79)
        {
          inData[index] = inChar;
          index++;
          inData[index] = '\0';
        }
      }
    }

    // We are here either because all pending serial
    // data has been read OR because an end of
    // packet marker arrived. Which is it?
    if(started && ended)
    {
      // The end of packet marker arrived. Process the packet

      // Reset for the next packet
      started = false;
      ended = false;
      index = 0;
      inData[index] = '\0';
    }

}
  Serial.println (inData);  
}

what is woring now?

what is woring now?

What is worrying is that you seem to be resetting your buffer index every time you receive a character.

   //   if(inChar == SOP)
      //{
        index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
     // }

Your computer is equipped with a delete key, isn't it? You should have used it!

o.k - I see this . so what do I need to define in SOP - so every char that he gets is good?

so what do I need to define in SOP - so every char that he gets is good?

Nothing.

If you want, you can remove SOP and started, and the code that deals with them.

That means the ENTIRE block of code, not just the if statement that wraps it.

I understand that the problem is with me

but still not working

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
int test=7;

#define SOP '<'
#define EOP '\n'
bool started = false;
bool ended = false;
char inData[80];
byte index;

void setup()  
{
  pinMode(test,INPUT);
  pinMode(13,OUTPUT);
  Serial.begin(19200); // Open serial communications and wait for port to open:
  delay(1500);
  Serial.println("Ready to work!");
  mySerial.begin(19200);
  delay(1500);

}

void loop()
{
  if (digitalRead(test)==HIGH)
  {
    mySerial.println("AT");
    // Read all serial data available, as fast as possible
    while(mySerial.available() > 3)
    {
      char inChar = mySerial.read();
      
      if(inChar == EOP)
      {
        ended = true;
        break;
      }
      else
      {
        if(index < 79)
        {
          inData[index] = inChar;
          index++;
          inData[index] = '\0';
        }
      }
    }

    // We are here either because all pending serial
    // data has been read OR because an end of
    // packet marker arrived. Which is it?
    if(started && ended)
    {
      // The end of packet marker arrived. Process the packet

      // Reset for the next packet
      started = false;
      ended = false;
      index = 0;
      inData[index] = '\0';
    }
  }
  Serial.println(inData);
}

still not working

Am I surprised?

if(started && ended)

I don't think so.

(I may be coming across as obtuse, but I'm trying to get you to think)

it’s doesn’t help if you don’t tell me what I’m doing wrong
I’m really appreciate you help and the patience you have for me , but if I don’t know the logic (and you can see I’m getting stuck with it according to my problems) - it will much more helpful to say “this won’t work because you need to do 1…2…3…”

again I want to learn -this is why I ask so many questions

if I understand it correctly this part

 if(index < 79)
        {
          inData[index] = inChar;
          index++;
         inData[index] = '\0';

always give “Enter - new line” is it not?

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
int test=7;

#define SOP '<'
#define EOP '\n'
bool started = false;
bool ended = false;
char inData[80];
byte index;

void setup()  
{
  pinMode(test,INPUT);
  pinMode(13,OUTPUT);
  Serial.begin(19200); // Open serial communications and wait for port to open:
  delay(1500);
  Serial.println("Ready to work!");
  mySerial.begin(19200);
  delay(1500);

}

void loop()
{
  if (digitalRead(test)==HIGH)
  {
    mySerial.println("AT");
    // Read all serial data available, as fast as possible
    while(mySerial.available() > 3)
    {
      char inChar = mySerial.read();
      
      if(inChar == EOP)
      {
        ended = true;
        break;
      }
      else
      {
        if(index < 79)
        {
          inData[index] = inChar;
          index++;
          inData[index] = '\0';
        }
      }
    }

    // We are here either because all pending serial
    // data has been read OR because an end of
    // packet marker arrived. Which is it?
    if(ended)
    {
      // The end of packet marker arrived. Process the packet

      // Reset for the next packet
      started = false;
      ended = false;
      index = 0;
      inData[index] = '\0';
    }
  }
  Serial.println(inData);
}

always give "Enter - new line" is it not?

No, that section first checks to see if there is sufficient space in the buffer, then writes in the character you just read at the end, then writes a null ('\0') to terminate the string. I don't know where "Enter - new line" comes from.

thanks , but I'm too lost , so I will start a new post from start (because I think this is where my problem is....)

Thanks again :-)