Tracking status parameters

I am refactoring software created by my predecessor. The software can communicate via the bus. At the moment, all devices are hardcoded in software and my task is to configure each type of device. (Stored in serialized classes and generated by the model builder). Using the software, the devices can be configured, addressed and parameters set.

It is currently tracking messages with tagged enumerations specified in the uints paramsRequested and paramsUpdated parameters. But this needs to be replaced with something else. As this is not scalable or configurable.

Enumeration example:

public enum FunctionParameters : uint
{
    None = 0,
    StatusInterval = 1 << 0,
    Delay = 1 << 1,
    Time = 1 << 2,
    .....
}

      

The month is sent on the bus and waits for a response asynchronously.

When the message arrives:

_paramsUpdated |= (uint) FunctionParameters.StatusInterval;  

      

Another thread waits for a message to be received to use it and checks if the parameter is accepted.

while (((uint)_paramsUpdated & (uint)param) == 0)
{
    // Do nothing
    Thread.Sleep(threadSleepTimeMS);
    //Thread.Yield();
}

      

If it takes too long, it will result in a timeout exception. This is currently working.

The question is if there are alternatives that don't work with enum flags to keep track of this, because the new situation has a few flexible flags.

I have no problem with timeout, it is more of an architectural problem for replacing the enum flags with another system.

+3


source to share


1 answer


Perhaps I have a solution:

I added a class called ParameterFlag; It looks like this:

    public class ParameterFlag
    {
        public ParameterFlag(ParameterType parameterType, bool flag)
        {           
            ParameterType = parameterType;
            Flag = flag;
        }

        public ParameterType ParameterType
        {
            get;
            set;
        }

        public bool Flag
        {
            get;
            set;
        }
    }

      

_paramsUpdated will become:

    List<ParameterFlag> _paramsUpdated

      



When the message arrives:

    ParameterFlag flag =_paramsUpdated.Find(x=> x.ParameterType == ParameterType);
    flag.Flag = true;

      

Another thread waits until a message is received to use it and checks if the parameter is accepted:

private void WaitParameterReceived(ParameterType param)
{
    while (!_paramsUpdated.Find(x => x.ParameterType == param).Flag)
    {
       // Do nothing
       Thread.Sleep(threadSleepTimeMS);
       //Thread.Yield();
    }
}

      

Thanks a lot for your comments!

+1


source







All Articles