I would like to test the contents of data received over a serial connection. In my loop() I store the data in bffr; and afterwards I would like to test to see if there are some predefined words in it.
#define BFFSZ 90
char bffr[BFFSZ];
char bffridx;
void loop()
{
readline();
//- Test the bffr
if(strstr(bffr, "CLOSED")){ //- WORKS
Serial.println("> conn. closed");
digitalWrite(A1, HIGH);
}else if(strstr(bffr, "RING")){ //- WORKS
Serial.println("> Someone is ringing");
digitalWrite(A2, HIGH);
}else if(strstr(bffr, "0,7,j,BLINK")){ //- Doesn't work
Serial.println("> Blink");
digitalWrite(A2, HIGH);
delay(300);
digitalWrite(A2, LOW);
}
}
void readline()
{
memset(bffr,0,sizeof(bffr));
char c;
int i =0;
bffridx = 0; // start at beginning
long previousMillis = millis();
while (1) {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > 20000) {
Serial.println("TIMEOUT");
return;
}
delay(2);
c=gsm.SimpleRead2();
//- debug only, to see if there is something received.
Serial.write(c);
if (c == -1)
continue;
if (c == '\n')
continue;
if ((bffridx == BFFSZ-1) || (c == '\r')) {
bffr[bffridx] = 0;
return;
}
bffr[bffridx++]= c;
delay(2);
}
}
The debug-line makes that I can see the data flowing, eg when I phone the SIM.
But it looks like the buffer isn't tested properly. The first two will work, but the last test fails. It is part of a larger string: $0,7,j,BLINK,567^
But I thought the strstr(1,2)
searches for 2 in 1. And as 0,7,j,BLINK,
is within the haystack I assume it wil return true. But apparently not...
The gsm.SimpleRead2()
comes from GSMlib and is my altered version of gsm.SimpleRead(). Instead of printing the char, I just return it so we can buffer it in the readline
function.
void SIMCOM900::SimpleRead()
{
char datain;
if(_cell.available()>0){
datain=_cell.read();
if(datain>0){
Serial.print(datain);
}
}
}
char SIMCOM900::SimpleRead2()
{
char datain;
if(_cell.available()>0){
datain=_cell.read();
if(datain>0){
return datain;
}
}
}
Does anyone know the answer to this?
gsm.SimpleRead2()
never returns non-printable characters (ASCII 0x00-0x1F)? That could explain why you don't see anything in the debug with Serial and the searched string can then not be found.SimpleRead2
come from? Could you provide a link?bffr
which causes the test to fail. How can I strip a string with non-printable characters?isPrintable(c)
to check it and addc
tobffr
only in that case. This is part of standard C library, there are many otherisXxxx(c)
functions defined inWCharacter.h
.