Logic Machine Forum
Convert HEX value to binary values - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Convert HEX value to binary values (/showthread.php?tid=1134)



Convert HEX value to binary values - Trond Hoyem - 13.12.2017

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?


RE: Convert HEX value to binary values - eirik - 14.12.2017

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


RE: Convert HEX value to binary values - admin - 14.12.2017

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


RE: Convert HEX value to binary values - Erwin van der Zwart - 14.12.2017

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


RE: Convert HEX value to binary values - Trond Hoyem - 19.12.2017

OK, will test some of this. Will post result...

Thx.


RE: Convert HEX value to binary values - Trond Hoyem - 19.12.2017

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