5

我正在研究一个解析从家庭自动化模块获得的二进制数据的 dll。

但我需要一些关于我拥有的代码的建议。
所以我收到一条带有一些字节的消息,在这种情况下,每个位都表示一个特定的条件。

在此处输入图像描述

在我目前的代码中,每个条件都是一个枚举,我将枚举放在一个数组中并检查是否设置了相应的位。

private void ParseZoneConditionFlag1(int Flag1)  // Flag1 = Hex represenation of byte
{
    Zone_Status_ZoneConditionFlagEnum[] FlagArray = new Zone_Status_ZoneConditionFlagEnum[8];
    FlagArray[0] = Zone_Status_ZoneConditionFlagEnum.Faulted;
    FlagArray[1] = Zone_Status_ZoneConditionFlagEnum.Tampered;
    FlagArray[2] = Zone_Status_ZoneConditionFlagEnum.Trouble;
    FlagArray[3] = Zone_Status_ZoneConditionFlagEnum.Bypassed;
    FlagArray[4] = Zone_Status_ZoneConditionFlagEnum.Inhibited;
    FlagArray[5] = Zone_Status_ZoneConditionFlagEnum.Low_Battery;
    FlagArray[6] = Zone_Status_ZoneConditionFlagEnum.Loss_Supervision;
    FlagArray[7] = Zone_Status_ZoneConditionFlagEnum.Reserved;

    base.CheckBitsSet(FlagArray, Flag1, ZoneConditionFlags_List);
 }

 private void ParseZoneConditionFlag2(int Flag2)
 {
    Zone_Status_ZoneConditionFlagEnum[] FlagArray = new Zone_Status_ZoneConditionFlagEnum[8];
    FlagArray[0] = Zone_Status_ZoneConditionFlagEnum.Alarm_Memory;
    FlagArray[1] = Zone_Status_ZoneConditionFlagEnum.Bypass_Memory;
    FlagArray[2] = Zone_Status_ZoneConditionFlagEnum.Reserved;
    FlagArray[3] = Zone_Status_ZoneConditionFlagEnum.Reserved;
    FlagArray[4] = Zone_Status_ZoneConditionFlagEnum.Reserved;
    FlagArray[5] = Zone_Status_ZoneConditionFlagEnum.Reserved;
    FlagArray[6] = Zone_Status_ZoneConditionFlagEnum.Reserved;
    FlagArray[7] = Zone_Status_ZoneConditionFlagEnum.Reserved;

    base.CheckBitsSet(FlagArray, Flag2, ZoneConditionFlags_List);
 }

方法是我检查实际位

 protected void CheckBitsSet<T>(T[] ConstantArray, int HexValue, List<T> DestinationList)
 {
     byte b = (byte) HexValue;

     for (int i = 0; i < Mask.Length; i++)
     {
        if(IsBitSet(b, i))
        {
           DestinationList.Add(ConstantArray[i]);
        }
     }
 }

 public bool IsBitSet(byte b, int pos)
 {
    return (b & (1 << pos)) != 0;
 }

这行得通,但我想知道是否有更清洁的方法来做到这一点。
使用清洁器,我的意思是不必每次都将正确的枚举添加到数组中。

4

2 回答 2

12

怎么样:

[Flags]
enum MyFlags : short
{
    None = 0,
    Faulted = 1 << 0,
    Tampered = 1 << 1,
    Trouble = 1 << 2,
    Bypassed = 1 << 3,
    Inhibited = 1 << 4,
    LowBattery = 1 << 5,
    LossOfSupervision = 1 << 6,
    AlarmMemory = 1 << 8,
    BypassMemory = 1 << 9
}
static bool IsSet(MyFlags value, MyFlags flag)
{
    return ((value & flag) == flag);
}

并将值读取为 2 字节值(short,注意字节顺序),然​​后转换为MyFlags.

要检查任何标志,只需:

MyFlags value = ...
bool isAlarmMemory = IsSet(value, MyFlags.AlarmMemory);

当您谈论复合标志时,它会变得更狡猾,即

bool memoryProblem = IsSet(value, MyFlags.AlarmMemory | MyFlags.BypassMemory);

因为您需要弄清楚您的意思是“是否设置了这些标志中的任何一个?” vs“所有这些标志都设置了吗?”

归根结底是考验;

return ((value & flag) == flag); // means "are all set"
return ((value & flag) != 0); // means "is any set"

阅读:

// this is just some garbage that I'm pretending is a message from
// your module; I'm assuming the byte numbers in the image are
// zero-based, so the two that we want are: \/\/\/ (the 6,3)
byte[] data = { 12, 63, 113, 0, 13, 123, 14, 6, 3, 14, 15 };

// and I'm assuming "byte 7" and "byte 8" (image) are zero-based;
// MyFlags uses byte 7 *first*, so it is little-endian; we can get that
// via:
short flagsRaw = (short)(data[7] | (data[8] << 8));
MyFlags flags = (MyFlags)flagsRaw;
// flags has value Tampered | Trouble | AlarmMemory | BypassMemory,
// which is what we expect for {6,3}
于 2013-06-20T08:46:41.953 回答
2

用这个:

[Flags]
public enum MyEnum
{
    Value1 = 1,
    Value2 = 2,
    Value3 = 4,
    Value5 = 8
}

(...)

void Func(int flag)
{
    MyEnum @enum = (MyEnum)flag;

    // Testing, whether a flag is set

    if ((@enum & MyEnum.Value1) != 0) // sth
}
于 2013-06-20T08:45:55.307 回答