Help using flags

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am monitoring some electronic equipment. Every few seconds i poll for
equipment status. The results are returned in 6 groups - like 0A 40 08 00 00
00. Currently only the first 3 bytes contain meaningful info

each bit reports either normal operation (a 0) or an error condition (a 1)
for a component of the equipment.

I set up <flag> Enum for each byte (just the first 3 bytes for now)
Enum Byte1
None=0
component1=1
component2 =2
component3 = 4
.... to 128
end enum

Enum Byte2
None=0
component1=1
component2 =2
component3 = 4
.... to 128
end enum

i need a routine that will quickly anaylyze and let me test the status of
each flag.

Thanks.
 
Hi Bob,

You can use an Enumeration to check flags, but the values of the
enumerations must match the values of the flags. Flags are stored as bits,
that is binary values:

1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, etc.

If you give these values to your enumerations, you can use Bitwise operators
to check them.

The following is an article about VB.Net enumerations, including Flags
enumerations:

http://aspnet.4guysfromrolla.com/articles/042804-1.aspx

For more information on VB.Net bitwise operators, see:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vaopror.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vaidxbitshift.asp
http://msdn.microsoft.com/library/d...7/html/vaconcombiningoperatorsefficiently.asp

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.
 
I suggest taking a look at the BitArray class. It will take an integer
or array of bytes in its constructor. You can then test the status in
individual bits.
 
I ended up returning the bytes and doing a routine like this

dim rslt as integer
For ii as integer = 0 to 2
Select case ii
case 0
result= byte(ii) and equipment.flagbyte0.test1 '(test one is
value=1)
if result=equipment.flgbyte0.test1 then
doAlarmIcon
else
donormalicon
end if

....code for flags 2,4,8,16,32,64,128
Case 1 'for net status byte
......
 
Back
Top