Keeping track what the state parameters are

141 views Asked by At

I'm refactoring software that is created by my predecessor. The software can communicate over the can-bus. At this moment all devices are hardcoded in the software and my job is to make every deviceType configurable. (Saved in serialized classes and creatable with a Model creator). With the software, the devices can be configured, addressed and parameter set.

At the moment, it keeps track of the messages with flagged enums set in the uints paramsRequested and paramsUpdated. But this needs to be replaced with something else. Because this is not scaleable and configurable.

Example enum:

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

A mesage is sent over the can-bus and is waiting asynchronosly on a reply.

When message comes in:

_paramsUpdated |= (uint) FunctionParameters.StatusInterval;  

Another thread waits till message is arrived, to use it and checks if parameter isreceived.

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

When this takes too long, it will give a timeout exception. This works as intended at the moment.

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

I do not have time-out issues, it's more a architectual problem to replace enum flags with another system.

1

There are 1 answers

0
Gertjan Gielen On BEST ANSWER

I maybe have the solution myself:

I added a class named ParameterFlag; This looks linke 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 a message comes in:

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

Another thread waits till message is arrived, to use it and checks if parameter isreceived:

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

Thank you very much for your comments!