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.
//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 + ?
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?
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:
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:
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 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.