1

我使用枚举作为标志

 [Flags]
public enum SystemStatusEnum : long
{
    Line_seizure = 1 << 0,
    Off_hook = 1 << 1,
    Initial_handshake_received = 1 << 2,
    Download_in_progress = 1 << 3,
    Dialer_delay_in_progress = 1 << 4,
    Using_backup_phone = 1 << 5,
    Listen_in_active = 1 << 6,
    Two_way_lockout = 1 << 7,

    Ground_fault = 1 << 8,
    Phone_fault = 1 << 9,
    Fail_to_communicate = 1 << 10,
    Fuse_fault = 1 << 11,
    Box_tamper = 1 << 12,
    Siren_tamper_trouble = 1 << 13,
    Low_Battery = 1 << 14,
    AC_fail = 1 << 15,

    Expander_box_tamper = 1 << 16,
    Expander_AC_failure = 1 << 17,
    Expander_low_battery = 1 << 18,
    Expander_loss_of_supervision = 1 << 19,
    Expander_auxiliary_output_over_current = 1 << 20,
    Auxiliary_communication_channel_failure = 1 << 21,
    Expander_bell_fault = 1 << 22,

    _6_digit_PIN_enabled = 1 << 24,
    Programming_token_in_use = 1 << 25,
    PIN_required_for_local_download = 1 << 26,
    Global_pulsing_buzzer = 1L << 27,
    Global_Siren_on = 1 << 28,
    Global_steady_siren = 1 << 29,
    Bus_device_has_line_seized = 1 << 30,
    Bus_device_has_requested_sniff_mode = 1 << 31,

    Dynamic_battery_test = 1 << 32,
    AC_power_on = 1L << 33,
    Low_battery_memory = 1L << 34,
    Ground_fault_memory = 1L << 35,
    Fire_alarm_verification_being_timed = 1L << 36,
    Smoke_power_reset = 1L << 37,
    _50_Hz_line_power_detected = 1L << 38,
    Timing_a_high_voltage_battery_charge = 1L << 39,

    Communication_since_last_autotest = 1L << 40,
    Power_up_delay_in_progress = 1L << 41,
    Walk_test_mode = 1L << 42,
    Loss_of_system_time = 1L << 43,
    Enroll_requested = 1L << 44,
    Test_fixture_mode = 1L << 45,
    Control_shutdown_mode = 1L << 46,
    Timing_a_cancel_window = 1L << 47,

    Call_back_in_progress = 1L << 55,

    Phone_line_faulted = 1L << 56,
    Voltage_present_interrupt_active = 1L << 57,
    House_phone_off_hook = 1L << 58,
    Phone_line_monitor_enabled = 1L << 59,
    Sniffing = 1L << 60,
    Last_read_was_off_hook = 1L << 61,
    Listen_in_requested = 1L << 62,
    Listen_in_trigger = 1L << 63,
}

在这里我声明我的标志 =

SystemStatusEnum SystemStatusFlags = 
            SystemStatusEnum.Off_hook | 
            SystemStatusEnum.Phone_fault |
            SystemStatusEnum.Expander_bell_fault |
            SystemStatusEnum._6_digit_PIN_enabled |
            SystemStatusEnum.Timing_a_high_voltage_battery_charge |
            SystemStatusEnum.Loss_of_system_time |
            SystemStatusEnum.Call_back_in_progress |
            SystemStatusEnum.Phone_line_faulted;

在这里,我将枚举转换为 8 个字节

 // System Status Flags 
        Byte[] SystemStatusBytes = new Byte[8];

        SystemStatusBytes[0] = (byte)((long)SystemStatusFlags & 0xff);
        SystemStatusBytes[1] = (byte)(((long)SystemStatusFlags >> 8) & 0xff);
        SystemStatusBytes[2] = (byte)(((long)SystemStatusFlags >> (8 * 2)) & 0xff);
        SystemStatusBytes[3] = (byte)(((long)SystemStatusFlags >> (8 * 3)) & 0xff);
        SystemStatusBytes[4] = (byte)(((long)SystemStatusFlags >> (8 * 4)) & 0xff);
        SystemStatusBytes[5] = (byte)(((long)SystemStatusFlags >> (8 * 5)) & 0xff);
        SystemStatusBytes[6] = (byte)(((long)SystemStatusFlags >> (8 * 6)) & 0xff);
        SystemStatusBytes[7] = (byte)(((long)SystemStatusFlags >> (8 * 7)) & 0xff);

        for (int i = 0; i < SystemStatusBytes.Count(); i++)
        {
            MessageBytes[Index++] = SystemStatusBytes[i];
        }

在这里我将它们转换回列表

    private void ParseSystemStatusFlags(int Flag1, int Flag2, int Flag3, int Flag4, int Flag5, int Flag6, int Flag7, int Flag8)
    {
        long flagsRaw = (long)(Flag1 | Flag2 << 8 | Flag3 << (8 * 2) | Flag4 << (8 * 3) | Flag5 << (8 * 4) | Flag6 << (8 * 5) | Flag7 << (8 * 6) | Flag8 << (8 * 7));
        SystemStatusEnum flags = (SystemStatusEnum)flagsRaw;
        SystemStatus_List = flags.GetFlags().ToList();
    }

但是我得到了一个意想不到的结果,我得到 Off_hook,Two_way_lockout,Phone_fault,Fuse_fault,Expander_bell_fault,_6_digit_PIN_enabled了结果。

所以有些标志是正确的,但是例如 Two_way_lockout 没有在我的枚举中声明。

我究竟做错了什么?

4

2 回答 2

1

我怀疑这可能部分是<<和之间的优先问题|,部分是转移int值而不是long值的问题。试试这个:

long flagsRaw = ((long) Flag1 << 0) |
                ((long) Flag2 << 8) |
                ((long) Flag3 << (8 * 2)) |
                ((long) Flag4 << (8 * 3)) |
                ((long) Flag5 << (8 * 4)) |
                ((long) Flag6 << (8 * 5)) | 
                ((long) Flag7 << (8 * 6)) |
                ((long) Flag8 << (8 * 7));

我还没有检查优先规则是否是问题所在,但至少上面的意图更清楚。

于 2013-06-21T08:53:49.167 回答
0

Long 是有符号类型,因此您实际上只能使用 64 - 1 位,Listen_in_trigger = 1L << 63 这是不正确的

于 2013-06-21T08:59:00.087 回答