Problem with buffer?

Hi guys. I’m working on arduino uno r3 using gps and gsm devices couse i’m trying to do tracking device. I’m sending message to device with instructions what to do and this is working well but when i want to read “written” phone numbers from text file using buffer, something is going bad. Take look on code:

//this is piece of code to copy data from gsm shild with sms sender number
void ReadMessage() 
{
  char buffer2[64];
  char comparetext[25];
  for (int i=0; i<count;i++ )
  { buffer2[i]=char(buffer[i]);}  
  memcpy(comparetext,buffer2,25);

   for(int i=9; i <21; i++){
        ret2[i-9] = comparetext[i];
   }
   
   Serial.println(F("ret2"));
   Serial.println(ret2);
   Serial.println(F("End of ret2"));  
   sdStart();
   sdRead();
 
       Serial.println(F("reto")); // this should be all phone numbers copied from text file
       Serial.println(reto);  
       Serial.println(F("ret2 again")); // this should be sms sender phone number
       Serial.println(ret2);
   
        if(strstr(reto, ret2))
        {
            Serial.println("Match!");
             
        }
// this is how looks reading from sd card
void sdRead()
{
 // re-open the file for reading:
    myFile = SD.open("phone.txt");
    if (myFile) {
    
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      myFile.read(buf,64);    
       
    }
    // close the file:
    myFile.close();
  } else {
   // if the file didn't open, print an error:
    Serial.println(F("error opening phone.txt"));
  }
    memcpy(reto,buf,25);
  
}
This is my output:
+CMT: "+48XXXXXXXXX","","14/06/01,09:48:55+08" // my phone number
Lokalizuj // my sms instruction
ret2
+48XXXXXXXXX // my phone number
End of ret2
initialization done.
reto
+48503234543+48516572342+48519572342+48XXXXXXXXX+48515242342
ret2 again
+48XXXXXXXX+48503234543+48516572342+48519572342+48XXXXXXXXX+48515242342

As you can see reto is good (this are phone numbers written in text file) but ret2 is changing and it looks like last character of my phone number is gone ( → +48XXXXXXXX+48503234543 etc) and there is reto data after my phone number. WTF?? :stuck_out_tongue:
Of course i was trying without memcpy using buf instead of reto and it was the same problem. Please help me :slight_smile:

Edit: As you can see I’m trying to check if sms sender number is written in my text file.

You probably have an issue that your char arrays are not null-terminated.

It is difficult to tell for sure, because your code is incomplete.

Piece of code without includes and others variables not connected with buffers

File myFile;
SoftwareSerial GPRS(2, 3);
byte buffer[64]; 
char ret2[11];
char reto[12];
char buf[64];
int count=0;

And before ReadMessage() i’ve got this connected with buffers:

GPRS.listen();
if (GPRS.available()) 
{
while(GPRS.available()) 
{
buffer[count++]=GPRS.read(); 
if(count == 64)break;
}

Serial.write(buffer,count); 
ReadMessage();
clearBufferArray(); 
count=0; 
}
if (Serial.available()) 
{
GPRS.write(Serial.read());
}
}

void clearBufferArray() 
{
for (int i=0; i<count;i++)
{
 buffer[i]=NULL;} 
}

I think that is all needed because there are no more places where a buffer is used

Show the whole program please. If it is very long perhaps you could write a short complete program that illustrates the problem.

…R

You probably have an issue that your char arrays are not null-terminated.

That should be definitely. reto and ret2 aren't even the same size, so expecting them to contain the same data is also unrealistic.

I don't want ret2 and reto to contain same data. Ret2 should contain sms sender phone number, reto phone numbers written in text file.

I just want to compare them and check if sms sender phone number is in my text file stored on sd card

DataCounter++;

That's a rather stupid name for something that isn't counting data.

Frankly, it makes little sense trying to solve the issue of parsing data from the GPRS device when using two instances of SoftwareSerial on the same 328-based Arduino makes so little sense.

Only one instance can listen at a time, so the phone will be ignored while listening to the GPS, and the GPS will be ignored while listening to the phone. I'm nearly certain that you don't want to be ignoring either one.

So, you need an Arduino with more hardware serial ports and more memory, like the Mega.

I know that but for now i have just uno r3 and it is working well. I'm going to buy mega in future. But this is not what I'm asking. I've got problem with buffer.. Help me with it :slight_smile:

I've got problem with buffer.. Help me with it

If you go to the doctor after nearly cutting you leg off with a chainsaw, and complain about a sore ankle, you are NOT going to get treatment for the sore ankle.

When you are trying to do something with the Arduino that is clearly not going to work, I'm not interested in helping you with buffer issues, especially when you:

  1. have not dealt with the issues that have already been mentioned
  2. may be running out of memory.

I was running out of memory once and gprs wasn't running fine. Now i've got some memory and everything is working. I'm started learning programming recently so sorry for my mistakes (and for my english too). I just want you to steer me what is wrong with buffers.

I have my usual problem with your code - there is so much stuff in loop() that it would take ages to figure out what the program is trying to do.

I suggest you move a lot more of the code into functions with meaningfull names that each deal with a single piece of the action.

That would make it easy for people here to focus on the problem area. You may have the whole program concept in your head but we haven't. It might even bring the solution to your attention.

Alternatively, as I suggested earlier, write a short sketch that just has enough stuff to illustrate the problem. In my experience that is the best way to figure out solutions to problems.

...R

Try null terminating it.
Try bigger buffer.

Whichever helps :slight_smile: