Arduino serial event not returning -
i'm working on arduino project , receive commands serial port
arduino serial event. won't fulfill condition, making code not knowing serial event has been finished. here code.
void serialevent() { while (serial.available()) { // new byte: char inchar = (char)serial.read(); if (inchar == '`') { // add inputstring: inputstring += inchar; // if incoming character newline, set flag // main loop can it: if (inchar == '|') { settingsreceived = true; // <----- never called serial.println("true"); // <------ nor } } } }
i've tried passing string `hello|
serial monitor yet won't respond. additionally, i've tried line ending
no line ending
, newline
yet won't work, can please help? thanks!
i have figured problem, serial.read()
read byte per time, example, `hello|
split
` h e l l o |
so first time, if (inchar == '
`')
true, therefore entering actions inside, second char, (h, e, l, l, o, |) not char "`", meaning if (inchar == '
`')
not true, not letting enter condition after that.
here should propoer way of doing it:
void serialevent() { if (serial.available()) { char inchar = (char)serial.read(); if (inchar != '`') { return; // <----- rejecting char if not starting ` } } while (serial.available()) { // new byte: char inchar = (char)serial.read(); // add inputstring: inputstring += inchar; // if incoming character newline, set flag // main loop can it: if (inchar == '|') { settingsreceived = true; serial.println("true"); } } }
Comments
Post a Comment