COMMUNICATION
Handle data on serial port
I need to input some serial data via the serial port - actually whether some switched have been pushed.
As I understand it, the 'arrival' of serial data to the serial port is not an event and is not handled by HANDLEEVENTS.
Is it reasonable or advisable to get serial data by making a HANDLE.SERIAL.DATA routine (analagous to HANDLEEVENTS) that is inserted in the event loop i.e.
FN HANDLE.SERIAL.DATA
if LOF = 0 then return to event loop
if LOF <> 0 then FN doSERIAL.DATA
END FN
FN doSERIAL.DATA
do whatever I want with the serial data (analagous to FN doDIALOG)
END FN
'main loop
DO
HANDLEEVENTS
HANDLE.SERIAL.DATA
UNTIL 0
Or is there a more standardized way to handle serial data?.
This may be a good approach for your needs. It may not work for everyone, though, because often the data is "context sensitive" - in other words, you need to keep track of more than the data coming in - you also need to know what it is in response to.
I'm working on a small project that needs to check the serial port looking for input. The input will only be a _single byte_ in size and will come at intervals of maybe a few a second to maybe one every minute or so.
I have a FN to check for data in the serial buffer. It process one byte if there is something there or drops back if empty. The input buffer is set to the defalt 64 bytes.
Is is it OK to call the FN at every NullEvent? I feel guilty using them all up. Or should I somehow only check on every tenth (or whatever) NullEvent?
I know this was talked about in the last couple of weeks but sad to say I wasn't paying any attention then.
I find the On Timer FN functionName thingy to be really good at this. You specify how much time, and every time that much time passes, it calls the function (provided, of course, that your program gets back to the handleevents). You'll love it!
|