State Machines and Timeouts. How could I do/control it?

Started by
1 comment, last by riruilo 15 years, 7 months ago
Hi all! Could you help me? I'm developing a embedded program to control a roulette (a real one). I send commands thru serial port, and then, the roulette reply to my program. My program has to play matches all the time. And this is more or less easy using a state machine (send switch on, wait confirmation, send launch ball, recieve confirmation, wait until recieve winning ball, then swallow the ball, wait some seconds, start again...) My problem is that I don't know how to control timeouts or waiting periods (Sleep(1000) does not seem a good idea). For instance, after sending launch the ball, if I don't recieve anything in the next 5 seconds (for example) I would have to retry, or jump to an error state. Do you have any idea about how I can to this? My idea is to use a thread that every 2 seconsds test the current state to know if there is any timeout and use a variable like start_time every time I send a message. if current_time-start_time>5 seconds then timeout or retry. But, Is this going to work if I add code at entry and exit of my states? (I ask this because my entry and exit code will take a few seconds) Am I going to have thread or blocking problems? Thank you very much for your time. I'm looking forward to hearing opinions. Ricardo.
I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser gate. All those moments will be lost in time, like tears in rain. Time to die.
Advertisement
If you're writing an embedded program, you're either running in an OS specific to the hardware which will provide special real-time processing functions that may help, or you're running alone on the hardware.

In the first case, check the OS documentation (or tell us which one it is).

In the second case, you just have to loop and poll until you timeout.
Well, I said embedded but my system is just a common PC (just the tower) with any Linux distribution, situated under the roulette. But I'm not using any real time OS. I said embedded because I have a specific purpose computer, as a phone or a MP3. Perhaps calling my system an embedded is not right in my case.

You say "In the second case, you just have to loop and poll until you timeout."

I understand what you mean with poll (I already proposed that) but could you explain what do you mean with loop, please?

Thanks for your help.
I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser gate. All those moments will be lost in time, like tears in rain. Time to die.

This topic is closed to new replies.

Advertisement