阅读时连续搜索某些单词?
Searching in serial for certain words WHILE reading?
读串口时有时输入的数据太大无法保存,但无论如何我们都需要在其中搜索某些单词。
现在我们积累了所有的数据,然后才寻找特定的词。
我想这样做 while reading, without saving the data
,所以要改进这个:
boolean waitFor(char *target, long timeout)
{
unsigned long tm=millis();
while( 1 )
{
if( millis() - tm >= timeout )
return 0;
char wifiContent[50]={0};
int readWifiIndex=0;
while( wifiSerial.available() )
{
if(readWifiIndex<50)
{
wifiContent[readWifiIndex]=(char)wifiSerial.read();
readWifiIndex++;
delay(1);
}
}
//**check of specific words
if( strstr(wifiContent,target) )
return 1;
}
}
对于任何需要它的人,这里有一个更优雅的解决方案,它的重量要轻得多,并且可以节省大量资源。
此函数搜索给定的单词,并且也有超时。效果很好。
boolean waitFor(char *target, long timeout)
{
unsigned long tm=millis();
while( 1 )
{
if( millis() - tm >= timeout )
return 0;
int readWifiIndex=0;
while( Serial.available() )
{
char cr=(char)Serial.read();
delay(1);
if(target[readWifiIndex] ==cr)
{
readWifiIndex++;
if( strlen(target) ==readWifiIndex )
return 1;
}
else
readWifiIndex=0;
}
读串口时有时输入的数据太大无法保存,但无论如何我们都需要在其中搜索某些单词。
现在我们积累了所有的数据,然后才寻找特定的词。
我想这样做 while reading, without saving the data
,所以要改进这个:
boolean waitFor(char *target, long timeout)
{
unsigned long tm=millis();
while( 1 )
{
if( millis() - tm >= timeout )
return 0;
char wifiContent[50]={0};
int readWifiIndex=0;
while( wifiSerial.available() )
{
if(readWifiIndex<50)
{
wifiContent[readWifiIndex]=(char)wifiSerial.read();
readWifiIndex++;
delay(1);
}
}
//**check of specific words
if( strstr(wifiContent,target) )
return 1;
}
}
对于任何需要它的人,这里有一个更优雅的解决方案,它的重量要轻得多,并且可以节省大量资源。
此函数搜索给定的单词,并且也有超时。效果很好。
boolean waitFor(char *target, long timeout)
{
unsigned long tm=millis();
while( 1 )
{
if( millis() - tm >= timeout )
return 0;
int readWifiIndex=0;
while( Serial.available() )
{
char cr=(char)Serial.read();
delay(1);
if(target[readWifiIndex] ==cr)
{
readWifiIndex++;
if( strlen(target) ==readWifiIndex )
return 1;
}
else
readWifiIndex=0;
}