By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,203 Members | 1,620 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,203 IT Pros & Developers. It's quick & easy.

State machine in C#

P: n/a
I have a C++ application which I am porting to C#. This application is
controlling a transport belt. This transport belt is moving bins foreward
and backward. I have a state machine controlling this transport belt. In c++
there is an integer named state which holds the state of the belt. Bit 0-2
is telling which task is going on (move belt forward, move belt to middle
possition and so on..) Bit 3 is telling if there is a bin in the middle
possition of the belt. Bit 4-7 is telling some thing about the physical
state of the belt. Bit 8-11 is telling something about exceptions. This is
working well, but I think it is a little hard to understand if someone is
going to take over my code someday. I want to make this simpler. Maybe using
enums and the flags attribute? What do you think?

Thank you.
Nov 10 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
I guest the enum with flags is a great option, I have someting like that
with an industrial printer, the printer have like 20 states (and varius of
the states can be set in some point of time), I have enum with flags to
indicate the state of the printer, and works very well.

Regards,

Bela Istok

"nyhetsgrupper" <ny***********@gmail.comwrote in message
news:es*************@TK2MSFTNGP02.phx.gbl...
>I have a C++ application which I am porting to C#. This application is
controlling a transport belt. This transport belt is moving bins foreward
and backward. I have a state machine controlling this transport belt. In
c++ there is an integer named state which holds the state of the belt. Bit
0-2 is telling which task is going on (move belt forward, move belt to
middle possition and so on..) Bit 3 is telling if there is a bin in the
middle possition of the belt. Bit 4-7 is telling some thing about the
physical state of the belt. Bit 8-11 is telling something about exceptions.
This is working well, but I think it is a little hard to understand if
someone is going to take over my code someday. I want to make this simpler.
Maybe using enums and the flags attribute? What do you think?

Thank you.

Nov 10 '06 #2

P: n/a

"nyhetsgrupper" <ny***********@gmail.comwrote in message
news:es*************@TK2MSFTNGP02.phx.gbl...
>I have a C++ application which I am porting to C#. This application is
controlling a transport belt. This transport belt is moving bins foreward
and backward. I have a state machine controlling this transport belt. In
c++ there is an integer named state which holds the state of the belt. Bit
0-2 is telling which task is going on (move belt forward, move belt to
middle possition and so on..) Bit 3 is telling if there is a bin in the
middle possition of the belt. Bit 4-7 is telling some thing about the
physical state of the belt. Bit 8-11 is telling something about exceptions.
This is working well, but I think it is a little hard to understand if
someone is going to take over my code someday. I want to make this simpler.
Maybe using enums and the flags attribute? What do you think?
Since you have memory to spare (else you wouldn't be used .NET), I would
recommend splitting the state into four separate 8-bit variables, with an
enum defining the appropriate states for each. Some enums (exceptions) will
probably need FlagsAttribute, but several of them probably won't. You'll
eliminate a lot of bitmask code which is a great confusion to many unskilled
programmers, and if you're concerned about the abilities of someone taking
over your code this could be a sticky point.

However, if you often switch based on the entire number, then I would take a
different tack. Leave the state variable as is, and make it even more
opaque. Provide properties to encapsulate the change in each subfield. I
don't believe this approach applies to your problem, because you said the
current task is indicated by only bits 0-2.

Remember that an FSM, whether Moore or Mealy model, branches on inputs as
well as current state. Don't complicate the code by storing these inputs in
extra bits of the current state variable. From your description, bits 0-2
are the state, and bits 3-11 contain three different input variables.
>
Thank you.

Nov 10 '06 #3

P: n/a
Hi,

It's not really a state machine if you're just "flagging" state. In other
words, if you're not performing any operations based on the current state then
enums should be just fine (as mentioned by another respondent). Although you
might want to use multiple enums instead of a single integer.

enum BeltDirection { Forward, Backward }

enum BinPosition { Start, Middle, End }

enum BinEnabledState { On, Off } // bool would be much easier ;)

If you want to use a flexible state machine architecture instead that actually
does provide different runtime logic depending on the current "state" of the
transport belt you could code something like this:

class TransportBeltController
{
private TransportBeltState state;
public TransportBeltState State
{
get
{
if (state == null)
state = new TransportBeltForwardState();

return state;
}
set
{
if (value == null)
throw new ArgumentNullException("State");

state = value;
}
}

public BinPosition MoveToNextBin()
{
State.MoveToNextBin();
return state.Position;
}
}

enum BinPosition { Start, Middle, End }

abstract class TransportBeltState
{
protected BinPosition binPos; // Start is default
public BinPosition Position { get { return binPos; } }

public abstract void MoveToNextBin();
}

class TransportBeltForwardState : TransportBeltState
{
public override void MoveToNextBin()
{
if (binPos == BinPosition.End)
binPos = BinPosition.Start;
else
binPos = ((int) binPos)++;
}
}

class TransportBeltBackwardState : TransportBeltState
{
public override void MoveToNextBin()
{
if (binPos == BinPosition.Start)
binPos = BinPosition.End;
else
binPos = ((int) binPos)--;
}
}

--
Dave Sexton

"nyhetsgrupper" <ny***********@gmail.comwrote in message
news:es*************@TK2MSFTNGP02.phx.gbl...
>I have a C++ application which I am porting to C#. This application is
controlling a transport belt. This transport belt is moving bins foreward and
backward. I have a state machine controlling this transport belt. In c++
there is an integer named state which holds the state of the belt. Bit 0-2 is
telling which task is going on (move belt forward, move belt to middle
possition and so on..) Bit 3 is telling if there is a bin in the middle
possition of the belt. Bit 4-7 is telling some thing about the physical state
of the belt. Bit 8-11 is telling something about exceptions. This is working
well, but I think it is a little hard to understand if someone is going to
take over my code someday. I want to make this simpler. Maybe using enums and
the flags attribute? What do you think?

Thank you.

Nov 10 '06 #4

P: n/a
You may even want to look at Windows Workflow for state and workflow.

--
William Stacey [C# MVP]

"nyhetsgrupper" <ny***********@gmail.comwrote in message
news:es*************@TK2MSFTNGP02.phx.gbl...
|I have a C++ application which I am porting to C#. This application is
| controlling a transport belt. This transport belt is moving bins foreward
| and backward. I have a state machine controlling this transport belt. In
c++
| there is an integer named state which holds the state of the belt. Bit 0-2
| is telling which task is going on (move belt forward, move belt to middle
| possition and so on..) Bit 3 is telling if there is a bin in the middle
| possition of the belt. Bit 4-7 is telling some thing about the physical
| state of the belt. Bit 8-11 is telling something about exceptions. This is
| working well, but I think it is a little hard to understand if someone is
| going to take over my code someday. I want to make this simpler. Maybe
using
| enums and the flags attribute? What do you think?
|
| Thank you.
|
|
Nov 11 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.