473,548 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

State machine in C#

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
4 7142
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

"nyhetsgrup per" <ny***********@ gmail.comwrote in message
news:es******** *****@TK2MSFTNG P02.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

"nyhetsgrup per" <ny***********@ gmail.comwrote in message
news:es******** *****@TK2MSFTNG P02.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
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 TransportBeltCo ntroller
{
private TransportBeltSt ate state;
public TransportBeltSt ate State
{
get
{
if (state == null)
state = new TransportBeltFo rwardState();

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

state = value;
}
}

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

enum BinPosition { Start, Middle, End }

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

public abstract void MoveToNextBin() ;
}

class TransportBeltFo rwardState : TransportBeltSt ate
{
public override void MoveToNextBin()
{
if (binPos == BinPosition.End )
binPos = BinPosition.Sta rt;
else
binPos = ((int) binPos)++;
}
}

class TransportBeltBa ckwardState : TransportBeltSt ate
{
public override void MoveToNextBin()
{
if (binPos == BinPosition.Sta rt)
binPos = BinPosition.End ;
else
binPos = ((int) binPos)--;
}
}

--
Dave Sexton

"nyhetsgrup per" <ny***********@ gmail.comwrote in message
news:es******** *****@TK2MSFTNG P02.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
You may even want to look at Windows Workflow for state and workflow.

--
William Stacey [C# MVP]

"nyhetsgrup per" <ny***********@ gmail.comwrote in message
news:es******** *****@TK2MSFTNG P02.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4307
by: Robert | last post by:
In Web.config file is a setting for sessionState. If I want to maintain a users' session state (ie use the Session object) in a web farm that is being load balanced by a Cisco CSS 11501 Switch. How do I setup sessionState so anything saved to the Session object stays with that session no matter what web server they get bounced around to?
0
800
by: Maciek | last post by:
Hi When I set Session state mode to StateServer (IIS 6.0; windows2003; .NET 2.0) in my application, I have recived this message: ----------------------------------------------------------------------------------------------------------------- Unable to make the session state request to the session state server. Please ensure that the ASP.NET...
6
12516
by: nilavya | last post by:
HI, I have got a C++ application involving State machine. Now I have got too many states and too many events which changes the state of the Staet machine. So my code is somewhat unmanageable. It is : switch(nState) { case state1:
4
4372
by: Shawnk | last post by:
This post is intended to verify that true value semantics DO NOT EXIST for the Enum class (relative to boolean operations). If this is true then (thus and therefore) you can not design state machines in C# where the machine can use the enumerated 'state' to execute (thus the term 'machine' as in 'state machine') the machine operations as...
67
7309
by: Rui Maciel | last post by:
I've been delving into finite state machines and at this time it seems that the best way to implement them is through an intense use of the goto statement. Yet, everyone plus their granmother is constantly cursing at the goto statement, accusing it of being some sort of spawn of satan. So it seems we have a pickle here. The thing is, at...
11
1887
by: tuom.larsen | last post by:
Dear list, I'm writing very simple state machine library, like this: _state = None def set_state(state): global _state _state = state
2
1671
by: tat | last post by:
Hi all, Sorry if this question is off-topic. I would like have your suggestion . I need to write one application which control a machine via serial port. There are 5 states in the following sequence: active, set, check, command, and clear. In each state, there are two sub-states: do and wait. For example, in active state: do() sub-state...
3
2399
by: Pallav singh | last post by:
Hi All i am getting Error while writing following code for state design Pattern kindly let me know How to Correct this Error ?? Thanks Pallav Singh +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
2394
by: laktofil | last post by:
This may seem like an abstract question on behavioral inheritance. Anyway, I'm building a hierarchical state machine in C++ (with gcc for target platform Gentoo Linux). More precisely, I'm using this framework: Practical Statecharts in C/C++: Quantum Programming for Embedded Systems (hopefully, you don't have to know it to answer my question)....
0
7512
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7438
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7951
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7803
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3495
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
751
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.