This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Convert HEX value to binary values
#1
Hi

I need to get a hex value presented as individual bits. I have a 2-byte value where each bit indicates a certain status
Bit0 - Alarm class danger (A)
Bit1 - Alarm class critical (A)
Bit2 - Alarm class low (B)
Bit3 - Alarm class warning ©
Bit4 -
Bit5 - Manual control active
Bit6 - Summer mode
Bit7 -
Bit8 - Preheating, heating reg
Bit9 - Preheating, extra heating reg
Bit10 -
Bit11 - Act ctrl mode temp, room
Bit12 - Act ctrl mode temp, exhaust
Bit13 - Act ctrl mode temp, supply
Bit14 - Act ctrl mode hum, room
Bit15 - Act ctrl mode hum, supply

I therefore need to get the hex value and check each bits value. Anyone know a good way to do this?
There are 10 kinds of people in the world; those who can read binary and those who don't  Cool
Reply
#2
Hi 

Looks for me you're reading from a Modbus device, and if thats the case I think you could read the bit like this:
One for each bit.

{ "name": "VAV_1_Excessive util", "bus_datatype": "bool", "type": "register", "address": 5, "datatype": "int16", "value_multiplier":1, "value_bitmask": "0x00"   },

{ "name": "VAV_1_Mech travel incr", "bus_datatype": "bool", "type": "register", "address": 5, "datatype": "int16", "value_multiplier":1, "value_bitmask": "0x01"   }

Please correct me if I'm wrong Smile

(PS: from a profile for a different modbus device)

Eirik
Reply
#3
Bit mask should be 1 for bit 0, 2 for bit 1, 4 for bit 3 and so on.
Reply
#4
Hi,

Or use this function when you don't use modbus:

Code:
function getbit(value, nr)
  value = bit.rshift(value, nr)
  return bit.band(value, 1)
end

bit0 = getbit(0xFFFF,0)
log(bit0)

--...

bit15 = getbit(0xFFFF,15)
log(bit15)

BR,

Erwin
Reply
#5
OK, will test some of this. Will post result...

Thx.
There are 10 kinds of people in the world; those who can read binary and those who don't  Cool
Reply
#6
I ended with Erwin's solution. Worsk nicely.

Eirik, you are right about it beeing a MB device, but since the profile was already implememted into 5 different devices, I wouldn't be bothered to update all the devices with your suggestion this time, but surely, I will consider that the next time as it is a bit more elegant to just extract it directly.

Trond
There are 10 kinds of people in the world; those who can read binary and those who don't  Cool
Reply


Forum Jump: