Dear All,
I actually have a problem while sending a
AT+CENG?
command. I use it to get CellID of the towel
The answer of that AT command contain about 130 carcaters and I just read it
Get the number of bytes (characters) available for reading from the serial port. This is data that’s already arrived and stored in the serial receive buffer (which holds 64 bytes). available() inherits from the Stream utility class.
I observed that my response break after the 66th (someting 73rd) caracters.
I think this is the cause, no?
How can I hold more than 64 caracter with _cell.avaliable
Here is my function with send the command and wait for the answer. It’s well commented to understand
uint8_t WI968C::sendATcommandt(char* ATcommand, char* expected_answer1, char* expected_answer2, unsigned int timeout, bool ln, bool debug_buff)
{
// ATcommand => The command sent
// expected_answer1 => The first cacarters I a, waiting from the response. It usually the OK caracters
// expected_answer2 => (optionale) In the case I am waintg for two caracters of the response
// => if expected_answer1 or expected_anser2 are read, we exit the do loop before the end of the timeout
// timeout => Leave the do loop after the timeout, if excpected_answer1 or expected_answer2 are not met
//debug_buffer => This allow me to raed the full response of the AT command. If it true, the do loop will run to the timeout, evenif
// excpected_answer1 or expected_answer2 are met
// Intilize the variables
uint8_t x = 0;
uint8_t answer = 0;
unsigned long previous;
// Clean the buffer
memset(buffer, '\0', BUFFERSIZE); // BUFFERSITE is 200
memset(response, '\0', NBLINES); // NBLINES is egal to 10
//Serial.println(F("membuf:")); Serial.println(buffer);
// Clean _cell
while(_cell.available() > 0) _cell.read();
previous = millis();
// This is used to escape the following \r and \n
// You will undertand below
bool flag = false;
// If true, add \n to the AT command
if(ln)
{
_cell.println(ATcommand);
//snprintf(buffer, sizeof(buffer),"AT%s\r\n",ATcommand);
}
else
{
_cell.print(ATcommand);
//snprintf(buffer, sizeof(buffer),"AT%s",ATcommand);
}
// Start do loop until the while condition is met (timeout and expected answers)
do{
// If _cell is available
if(_cell.available() > 0)
{
//sprint(F("BUFFERSIZE:")); Serial.print(x), sprint(F("/")); Serial.println(BUFFERSIZE);
// This is a security in case of the number of caraters of the response is larger than the buffer size
if(x < BUFFERSIZE-1)
{
// Read the caracters of the response, one by one
char r = _cell.read();
// The following coe will remove
// 1. the " by escaping the " caracter
// 2. Keep only one \n
// 3. to replace a \r with a \n
// The goal of that code is to have
/*
AT+CENG?
CENG: 3,0
+CENG:0,228,02,1388,147d,15,10
+CENG:1,228,02,1388,147d,15,10
+CENG:2,228,02,1388,147d,15,10
+CENG:3,228,02,1388,147d,15,10
+CENG:4,228,02,1388,147d,15,10
OK
*/
// instead of
/*
AT+CENG?
CENG: 3,0
+CENG:0,228,02,1388,147d,15,10
+CENG:1,228,02,1388,147d,15,10
+CENG:2,228,02,1388,147d,15,10
+CENG:3,228,02,1388,147d,15,10
+CENG:4,228,02,1388,147d,15,10
OK
*/
switch (r)
{
// Continue and ignore the cacarter)
case '"':
// set to false because \n or \r have never been red
flag = false;
continue;
case '\n':
// If \n is the first caracter of _cell.read(), escape it
if(x==0)
{
// and change to true, in case if it follow by another \n
flag = true;
continue;
}
// In a At response, there is somethime two \n or \r\n
// if \n is not preceded by a \n or a \t
if(flag==false)
{
// replace it by \n
buffer[x] = '\n';
// set to true, in case of it is follow by another \n
// If it's the case, it will ignore it
flag=true;
break;
}
else
{
// If it's the case, it ignore it
continue;
}
// Same comment as for the \n, but it look for the \r and replace it by a \n
case '\r':
if(x==0)
{
flag = true;
continue;
}
if(flag==false)
{
buffer[x] = '\n';
flag=true;
break;
}
else
{
continue;
}
// If the is no ", or no \r or no \n, fill the buffer
default:
flag = false;
buffer[x] = r;
}
// Display the index, and the matching caracter
Serial.print(x);
Serial.print(F(":"));
Serial.println(buffer[x]);
// increase the inde
x++;
// If debug is enable, leave the loop as soon as it meet the expected_answer1
// or the expected_answer2, by chnaging the answer varaibale from 0 to 1 or 2
if(!debug_buff)
{
if (strstr(buffer, expected_answer1) != NULL)
{
answer = 1;
}
if(expected_answer2 != NULL){
if (strstr(buffer, expected_answer2) != NULL)
{
answer = 2;
}
}
} // end if debug_buff
} // end x<BUFFERSITE
else
{
#ifdef DEBUG
sprintln(F("*Overflow! 3*"));
#endif
}
}
else{
//Serial.println(F("N/A"));
//Serial.println(x);
}// End cell available
} // end do
while((answer == 0) && ((millis() - previous) < timeout));
// Leave the loop, if answer is above 0 or because of the timeout
// end the buffer caracrter by a \0
buffer[x]='\0';
// If debug is enable, display the full response to control the result
if(debug_buff)
{
#ifdef DEBUG
sprintln(F("\nDEBUG:"));
sprintln(buffer);
#endif
// To return the correct value of answer
if (strstr(buffer, expected_answer1) != NULL)
{
answer = 1;
}
if(expected_answer2 != NULL){
if (strstr(buffer, expected_answer2) != NULL)
{
answer = 2;
}
}
}
// Put into array
// The goal of this, is to have an easyway to use the answer. It the reason why, I remove the \n and the \r
// For the above response exemple, response should return me
/*
response[0] => AT+CENG?
response[1] => +CENG: 3,0
response[2] => +CENG:0,228,02,1388,147d,15,10
response[3] => +CENG:1,228,02,1388,147d,15,10
response[4] => +CENG:2,228,02,1388,147d,15,10
response[5] => +CENG:3,228,02,1388,147d,15,10
response[6] => +CENG:4,228,02,1388,147d,15,10
response[7] => OK
*/
char *p = buffer;
byte i = 0;
response[i++] = p;
while (*p) {
if (*p=='\n') {
*p++ = '\0';
if (i<NBLINES)
{
response[i++] = p;
}
}
else
{
p++;
}
}
while(i<NBLINES) // NBLINE is egual to 10
{
response[i++] = p;
}
return answer;
}
I hope you can help me to solve that problem, because I am very annoyed.
Feel you free to ask me more detail, if I was not clear enough.
Many thank!!