I've been using boost's asynchronous statechart to write a program. In this program I communicate with another device using an asynchronous serial port. I have a state that waits for a confirmation from the device over the serial port and then posts a "confirmation received" event. This works well but, I would also like to implement a "timeout" event.
In previous programs I had been using switch case statements for my state machines where I had code that could be run each time the loop was run. This meant I could run code and check if I should change state due to something timing out. Like this:
while(1){
switch (state){
case 0:{
sendMessage();
state = 1;
sendTime = boost::chrono::steady_clock::now();
}
break;
case 1:{
if (isConfirmationReceived()){
// do something
state = 2;
}
else if (boost::chrono::steady_clock::now() > sendTime + boost::chrono::duration<double>(WAIT_LENGTH)){
//raise a time out error
state = 3;
}
}
break;
// etc etc
}
}
How would I implement something like this using boost::StateChart? Should i be thinking about this a completely different way?
I don't know if it is best practice but I have solved this problem by sending my SM
EvTickevents periodically (1ms or something) and handling the event in a super state. You can also addClearTimeout() SetTimeout()andDisableTimeout()member functions and access them like this:you can let the superstate transition to an error state on timeout or post an
EvOnTimeoutto the SM which can be handled differently depending on state.