How to check if a string is on the serial port ??
ok ive got modem outputing data arduino .
example want turn led on if "led_on" sent on serial port. need buffer clear on every cr , ignore incorrect strings , reset.
because messenger method works if type incorrect string in seems stop working untill reset . im guessing buffer needs reset after each cr.
ive found 2 way of doing both problems , can me have working 1 ?
code 1 , i found example of reading data serial buffer , cant check if string in buffer .
code 2
this messenger's library checkstring method , work if send other set commard random text confuses untill reset it. seems clears buffer after match ect.
i need except other strings , turn led on when "on" is sent.
any or examples great , reading
luke
example want turn led on if "led_on" sent on serial port. need buffer clear on every cr , ignore incorrect strings , reset.
because messenger method works if type incorrect string in seems stop working untill reset . im guessing buffer needs reset after each cr.
ive found 2 way of doing both problems , can me have working 1 ?
code 1 , i found example of reading data serial buffer , cant check if string in buffer .
code: [select]
#define strlen 16
char buffer[strlen];
int bufferindex = 0;
void setup()
{
serial.begin(9600);
}
void loop()
{
if( serial.available())
{
char ch = serial.read();
if( ch == '\r') // terminating carriage return
{
buffer[ bufferindex ] = 0; // terminate string 0 bufferindex = 0; // reset index ready string
// string
if (buffer =="led_on"){ // put in it doesnt work!
serial.println("led on");
}
}
else
buffer[ bufferindex++ ] = ch; // add character buffer
}
}
code 2
this messenger's library checkstring method , work if send other set commard random text confuses untill reset it. seems clears buffer after match ect.
i need except other strings , turn led on when "on" is sent.
code: [select]
// example demonstrates messenger's checkstring method
// turns on led attached pin 13 if receives "on"
// turns off if receives "off"
#include <messenger.h>
// instantiate messenger object message function , default separator
// (the space character)
messenger message = messenger();
// define messenger function
void messagecompleted() {
// loop echo each element of message separately
while ( message.available() ) {
if ( message.checkstring("on") ) {
digitalwrite(13,high);
} else if ( message.checkstring("off") ) {
digitalwrite(13,low);
}
}
}
void setup() {
// initiate serial communication
serial.begin(115200);
message.attach(messagecompleted);
pinmode(13,output);
}
void loop() {
// following line effective way of
// feeding serial data messenger
while ( serial.available() ) message.process( serial.read() );
}
any or examples great , reading
luke
in first code, collecting characters read serial port array, , null terminating string. good.
the problem comparison. == operator needs objects, not arrays of objects, on either side.
to compare strings, strings needs null terminated (as yours are), , need use strcmp function:
the problem comparison. == operator needs objects, not arrays of objects, on either side.
to compare strings, strings needs null terminated (as yours are), , need use strcmp function:
code: [select]
if(strcmp(buffer, "led_on") == 0) // if strcmp returns 0, strings match
{
// turn led on
}
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > How to check if a string is on the serial port ??
arduino
Comments
Post a Comment