Logic Machine Forum
Modbus, getting bits in a register - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Gateway (https://forum.logicmachine.net/forumdisplay.php?fid=10)
+--- Thread: Modbus, getting bits in a register (/showthread.php?tid=522)



Modbus, getting bits in a register - espen.sorensen@jmhansen.no - 21.12.2016

Hi,
How can I read out single bits from a 16-bit register? And write the bit to a group address? The modbus addresses in the readcoils function are not the same as for the readinputs addresses.
I also want a tip how I can read signed 16-bit register? (out side air temperature)

Espen Big Grin


RE: Modbus, getting bits in a register - Erwin van der Zwart - 22.12.2016

Hi,


If you read a 16 bits integer register you can get back value 0 to 65535, but when this register is used as single bit (not coil) you probably get back only value 0 or 1 out of this register.

To use that value for a 1 bit KNX value you can simply transfer it to boolean by using this:

Code:
value = mb:readregisters(123)
if value == 0 or value == 1 then
 value = toboolean(value)
 grp.write('1/1/1', value)
end
To set 16 bits register value to 2 byte signed you can use this code:
Code:
r1 = mb:readregisters(124)
value = lmcore.inttohex(r1, 2)
value = knxdatatype.decode(value, dt.int16)

BR,
Erwin


RE: Modbus, getting bits in a register - admin - 22.12.2016

If your register is not a bitmask but a single bit you can write it directly to a boolean object, any numeric value other than 0 will be treated as true. As for reading int16, you can use this:
Code:
value = mb:readregistervalue(123, 'int16')



RE: Modbus, getting bits in a register - espen.sorensen@jmhansen.no - 22.12.2016

My 16-bit register is a bitmask, e.g.:
Input register 3x0006 consists of:
Bit 0 = Dampers open
Bit 1 = Fire dampers open
Bit 2 = Fire dampers closed
Bit 3 = Status feedback pump
Etc.

I'm interested in getting out the status of a single bit within a register. Not the whole register acting as a bit, 0 or 1.
Is it possible?


RE: Modbus, getting bits in a register - admin - 22.12.2016

You can use this helper function:
Code:
function getbit(value, nr)
  value = bit.rshift(value, nr)
  return bit.band(value, 1)
end

value = mb:readregisters(123)
if value then
  dampers_open = getbit(value, 0)
  fire_dampers_open = getbit(value, 1)
  ...
end



RE: Modbus, getting bits in a register - espen.sorensen@jmhansen.no - 22.12.2016

Thanks!


RE: Modbus, getting bits in a register - Simuzer - 10.11.2024

(22.12.2016, 10:47)admin Wrote: You can use this helper function:
Code:
function getbit(value, nr)
  value = bit.rshift(value, nr)
  return bit.band(value, 1)
end

value = mb:readregisters(123)
if value then
  dampers_open = getbit(value, 0)
  fire_dampers_open = getbit(value, 1)
  ...
end

Its work for read any bit of 16bit register.

i also write any bit of 16bit register this like..

Code:
function updatebit(bit_value, ModbusAddress, bit_no)
  ????
  ????
  ????
end



For example;

Code:
 updatebit (true, 123, 0)
 updatebit (false, 123, 5)
1. --
before modbus 123 register: 1010 1101 0110 1011 (=44.395)
after modbus 123 register: 1010 1101 0110 1010 (=44.394)
2.--
before modbus 123 register: 1010 1101 0110 1010 (=44.394)
after modbus 123 register: 1010 1101 0100 1010 (=44.362)


RE: Modbus, getting bits in a register - admin - 10.11.2024

https://forum.logicmachine.net/showthread.php?tid=1777&pid=11025#pid11025