我有一个关于位掩码的问题,我完全不熟悉。在应用程序中,目前有一种方法可以获取地址的字节 [],并从中确定哪些警报(布尔值)true
属于false
.
我正在寻求扭转这种情况。我会有一个属性列表(bool),并根据哪些是真的,我会创建一个地址。
有没有一种简单的内置方法可以做到这一点?
任何帮助将非常感激。下面我附上了我的代码,用于获取地址中启用了哪些标志。
public struct DeviceStatus
{
[Flags]
public enum DeviceStatusMasks
{
None = 0x0,
EmergencyButton = 0x00000001,
StartButton = 0x02,
Door1Interlock = 0x04,
Door2Interlock = 0x08,
AccessPanelInterlock = 0x10,
TubeTemperature = 0x20,
SystemTemperature = 0x40,
DeviceTouchScreenError = 0x80,
FlashValueCorrupt = 0x100,
VoltageRailError3_3V = 0x200,
VoltageRailError5V = 0x400,
VoltageRailError24V = 0x800,
HvPsuWarmUpTimerElapsed = 0x1000,
// ReSharper disable InconsistentNaming
kVValueOutsideRange = 0x2000,
mAValueOutsideRange = 0x4000,
// ReSharper restore InconsistentNaming
PreheatValueError = 0x8000,
FilamentCurrentError = 0x10000,
KeySwitchOff = 0x00020000,
StandbyButtonPressed = 0x40000,
ErrorTrapState = 0x80000,
PcCommunicationFailure = 0x100000
}
public DeviceStatus(byte[] value)
{
RawValue = (DeviceStatusMasks)BitConverter.ToInt32(value.Reverse().ToArray(), 0);
}
#region Public Properties
public bool CriticalError => EmergencyButtonPressed | TubeTemperatureHigh | SystemTemperatureHigh;
public bool EmergencyButtonPressed => RawValue.HasFlag(DeviceStatusMasks.EmergencyButton);
public bool StartButtonPressed => RawValue.HasFlag(DeviceStatusMasks.StartButton);
public bool Door1InterlockOpen => RawValue.HasFlag(DeviceStatusMasks.Door1Interlock);
public bool Door2InterlockOpen => RawValue.HasFlag(DeviceStatusMasks.Door2Interlock);
public bool AccessPanelInterlockOpen => RawValue.HasFlag(DeviceStatusMasks.AccessPanelInterlock);
public bool TubeTemperatureHigh => RawValue.HasFlag(DeviceStatusMasks.TubeTemperature);
public bool SystemTemperatureHigh => RawValue.HasFlag(DeviceStatusMasks.SystemTemperature);
public bool DeviceTouchScreenError => RawValue.HasFlag(DeviceStatusMasks.DeviceTouchScreenError);
public bool FlashValueCorrupt => RawValue.HasFlag(DeviceStatusMasks.FlashValueCorrupt);
public bool VoltageRailError3_3V => RawValue.HasFlag(DeviceStatusMasks.VoltageRailError3_3V);
public bool VoltageRailError5V => RawValue.HasFlag(DeviceStatusMasks.VoltageRailError5V);
public bool VoltageRailError24V => RawValue.HasFlag(DeviceStatusMasks.VoltageRailError24V);
public bool HvPsuWarmUpTimerElapsed => RawValue.HasFlag(DeviceStatusMasks.HvPsuWarmUpTimerElapsed);
// ReSharper disable InconsistentNaming
public bool kVValueOutsideRange => RawValue.HasFlag(DeviceStatusMasks.kVValueOutsideRange);
public bool mAValueOutsideRange => RawValue.HasFlag(DeviceStatusMasks.mAValueOutsideRange);
// ReSharper restore InconsistentNaming
public bool PreheatValueError => RawValue.HasFlag(DeviceStatusMasks.PreheatValueError);
public bool FilamentCurrentError => RawValue.HasFlag(DeviceStatusMasks.FilamentCurrentError);
public bool KeySwitchOff => RawValue.HasFlag(DeviceStatusMasks.KeySwitchOff);
public bool StandbyButtonPressed => RawValue.HasFlag(DeviceStatusMasks.StandbyButtonPressed);
public bool ErrorTrapState => RawValue.HasFlag(DeviceStatusMasks.ErrorTrapState);
public bool PcCommunicationFailure => RawValue.HasFlag(DeviceStatusMasks.PcCommunicationFailure);
public bool HasNoStatus
{
get
{
return !(StandbyButtonPressed | Door1InterlockOpen | Door2InterlockOpen | AccessPanelInterlockOpen | KeySwitchOff | CriticalError);
}
}
#endregion
public DeviceStatusMasks RawValue { get; }