Pages: [1]   Go Down
Author Topic: Read char array, clear it's values and read it again  (Read 887 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
#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();

}
}
}
« Last Edit: March 17, 2013, 09:59:32 am by nesisekasi » Logged

0
Offline Offline
Full Member
***
Karma: 4
Posts: 196
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

try this

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

Quote
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.
« Last Edit: March 17, 2013, 10:12:55 am by mistergreen » Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 25
Posts: 1873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for response.

Code:
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.



Quote
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



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!
« Last Edit: March 17, 2013, 10:44:11 am by nesisekasi » Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11173
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:

Code:
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:

Code:
#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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you everyone smiley 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!
Logged

Pages: [1]   Go Up
Jump to: