Read char array, clear it's values and read it again

Hello. I do need some help on my code. Basically what i am trying to do is reading the entered string and outputting correct responses to the entered text. I manage to do that, yet what i cannot do is clear the string so i can enter new data into it and read it again. After each proper sentence (for example: +SIND: 4) the variable "at_buffer" which contains string entered into serial port should reset so that i can enter another sentence into it.

Also 1 thing. The proper sentences start with "+". That means when i enter it 1st, arduino will read + as an empty char. How is it possible to change it that + is read as + ?

I hope you understand my point. Would someone have any ideas on how to make that happen? Thanks.

#define ilgis 90

char at_buffer[ilgis];
char buffidx;
char kint;

void setup()
{
Serial.begin(9600);
Serial.println("Decode is starting");
delay(1000);
kint=1;
}


void readATString(void)
{ 
char c;
while (1) {
if(Serial.available() > 0) { 
c=Serial.read();
delay(200); 

at_buffer[buffidx++]= c; 
ProcessATString();
}
}
}

void ProcessATString() {

if( strstr(at_buffer, "+SIND: 8") != 0 ) {
Serial.println("GPRS unavailable");
}
if( strstr(at_buffer, "+SIND: 11") != 0 ) {
Serial.println("GPRS connected");
}
if( strstr(at_buffer, "+SIND: 4") != 0 ) {
Serial.println("GPRS AT ready");
}

if( strstr(at_buffer, "AT+CGATT?") != 0 ) {
if(strstr(at_buffer, "CGATT : 1") != 0 ) {
if(kint > 0){
Serial.println("GPRS connected");
kint = 0;
}
if(strstr(at_buffer, "OK") != 0 ){
Serial.println("Waiting for GPRS");
kint = 1;
}
}
}

if( strstr(at_buffer, "AT+CGACT=1,1") != 0 ) { 
if(strstr(at_buffer, "OK") != 0 ) {
Serial.println("Activate PDP Context");
}
}
}


void loop() { 
while (Serial.available()) { 
int c = Serial.read();
if (c) {

readATString();

}
}
}

try this

//clear out param for new param
    memset(&at_buffer[0], 0, sizeof(at_buffer));

Also 1 thing. The proper sentences start with "+". That means when i enter it 1st, arduino will read + as an empty char. How is it possible to change it that + is read as + ?

not sure what you mean.

Start by using '/0' (null) to terminate your string, this is the normal way of dealing with strings.

Then in readAtString after the call to processAtString set at_buffer[0]='/0'; and buffidx =0;

buffidx should be a byte not a char as its a counter not a character.

Mark

Thanks for response.

if( strstr(at_buffer, "+SIND: 8") != 0 ) {
Serial.println("GPRS unavailable");
memset(&at_buffer[0], 0, sizeof(at_buffer));
}
if( strstr(at_buffer, "+SIND: 11") != 0 ) {
Serial.println("GPRS connected");
memset(&at_buffer[0], 0, sizeof(at_buffer));
}

I have tried to put this line after each condition sentence yet after this line is executed, string is cleared and i am unable to enter any other data into the array. Or maybe memset should be executed elsewhere? Sorry for silly questions for i am newbie in programming.

mistergreen:

Also 1 thing. The proper sentences start with "+". That means when i enter it 1st, arduino will read + as an empty char. How is it possible to change it that + is read as + ?

not sure what you mean.

What i mean is when i enter "+SIND: 4" into the serial port, arduino reads it as "SIND: 4" - it reads the "+" char as a gap

holmes4:
Start by using '/0' (null) to terminate your string, this is the normal way of dealing with strings.

Then in readAtString after the call to processAtString set at_buffer[0]='/0'; and buffidx =0;

buffidx should be a byte not a char as its a counter not a character.

Mark

Thanks for information. I have tried out your suggestion yet when i insert those lines after the call to processAtString, the word which is sent from Serial port is read not as 1 word, but as separate chars. For example if i enter "SIND", it is read not 1 time the word "SIND" but 4 times as separate letters "S" "I" "N" "D" and as a result my at_buffer contains only letter D.

I assume i am trying to clear the string in the wrong line on the code. Any thoughts?

Cheers!

at_buffer[buffidx++]= c; 
ProcessATString();

You need to append a null terminator to the char array in order for it to be used as a c-string. I suggest you do this each time you append a character to it, so that the buffer always contains a well-formed c-string:

at_buffer[buffidx++]= c; 
at_buffer[buffidx]= '\0';

I suggest you only process the buffer when there is something in it i.e. buffidx > 0, and set buffidx = 0 after you have processed it.

Also, before appending a character to the array, ensure that the array is not already full. Remember to allow space to hold the null terminator, as well as the string itself:

#define ilgis 90

char at_buffer[ilgis+1];
char buffidx;
...

if(buffidx < ilgis)
{
   at_buffer[buffidx++]= c; 
   at_buffer[buffidx]= '\0'; 
}
else
{
  // buffer full
  buffidx = 0;
  at_buffer[buffidx]= '\0';
}

In this example I’ve handled the ‘buffer full’ condition by flushing the buffer, since you don’t have any way to detect when a complete message has been received. If you message is expected to be terminated by a newline then I’d rather use that to trigger processing of the message.

Thank you everyone :slight_smile: It seems that i didn't handle the string termination properly. I would like to thank you for pointing that out as it will help not only to work on this project but also in the future.

Thank you guys and cheers!