??? 11/15/06 22:42 Read: times |
#128052 - Finite state machines Responding to: ???'s previous message |
Finite state machines are a fundamental concept in computing. It sounds complex but the basis is very simple.
I quickly found some stuff regarding Perl http://www.perl.com/pub/a/2004/09/23/fsms.html Effectively it is a case statement in Delphi. Your code determines which entry in the case statement will execute next event. Please excuse my poor Pascal - I've been programming in 'c' recently. My poor brain always takes a little while to change over! integer comms_state; //make sure the variable is persistant! //from each timer event case comms_state begin 0: //send poll //send poll comms_state:=1; 1: //test for response //check for received characters if received characters ok comms_state:=2 else comms_state:=0; 2://got response from unit, send config //send config data etc etc end; This is a crude example, but similar to what you'd be writing. Basically you write your code so that you do what you need to do, then get out - no sitting in wait loops. The variable comms_state tells you what is going to be executed next event. There is no reason why you cannot have many state machines - each doing a specific task. I might have a state machine to decode the low level data packets, then have a state machine like above to manage the higher level polling etc. It is a simple but powerful concept for writing complex sequences. Written well, it can also be used as a formal proof of what the code does. The cpu you're using is a finite state machine! |