Comparing Specific Bits

I have a VFD that I would like to monitor and command via MODBUS serial.

Currently, I receive a “11000000 00000000 00000000 00000000” or a “00000000 00000000 00000000 00000000” or a “01000000 00000000 00000000 00000000” if the VFD is running, off, or decelerating.

I want to know how I can compare the first 2 bits of this 32bit integer to output a status in another variable.

Anyone have ideas?

You said 32 bits, but listed 24 bits. I’ll assume there are 8 bits prior to the bits you listed, so adjust accordingly.

It would be better represent these as hex as it is easier to read and hex to binary conversion is simple. The leading 0x tells us it is hex.
So your three values are:
0x00C00000 - Running
0x00000000 - Off
0x00400000 - Decelerating

If you take the status value and “bitand” it with the above constants, then you will know that those bits are set when the result is non-zero. Do that in Optoscript as follows:

VFDRunning = (VFDStatus bitand 0x00C00000) <> 0;
VFDOff = (VFDStatus bitand 0x00000000) <> 0;
VFDDecel =  (VFDStatus bitand 0x00400000) <> 0;

Yes you’re right, there should be 32 bits not 24. I hastily typed it up as I was leaving the office :sweat_smile:

Thank you! I will try this out tomorrow.

1 Like