- Fri Nov 04, 2016 12:24 pm
#57662
A different serialbranch perspective...
Unlike an Arduino C++ program which after once running through setup would then continuously poll around LOOP(), an ESP_Basic script is effectively one big setup program that get's ran once, with everything else being event driven.
So in the case of serial branching, you create a branch subroutine to do whatever you wish with the incoming serial characters - such as print them to screen for instance - then you declare your serial branch once in the main program, and sit back to WAIT for serial events to occur and cause branching to your waiting handler subroutine.
Code: Select allcls
serialbranch [myserialhandler] 'tell the interpreter where to branch on the event of incoming serial characters
wait 'wait for serial events to occur
end 'never gets this far, just waits above for serial events, branches to handle them, then returns and waits for more
[myserialhandler]
serialinput mymsg 'receives all new serialinput characters into the specified mymsg variable
print "My new msg=" & mymsg 'handle newly received mymsg
print "Length of mymsg=" & len(mymsg)
return 'return amd wait for more serial events
Serial comms just can't get any simpler than that! But there is a gotcha to be aware of.
The serial event branch gets triggered when a linefeed (LF) decimal 10 is received, and the triggering LF is appended after any prior received characters. So if you send just one letter "A" character for instance, it will not trigger a serial event unless, and until, followed by a LF character ... in which case the incoming letter "A" is now actually a 2 characters long msg because of the LF.
So two things to remember and be aware of...
1. single characters will not trigger a serialbranch event - they must be followed by a linefeed (dec 10) chr. So remember to add a linefeed char to trigger serial branching.
2. all serialbranch event received characters will also include the invisible linefeed char at the end. So remember to trim off any unwanted LF characters at the end of received serialbranch msgs.