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.

Modbus, getting bits in a register
#1
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
Reply
#2
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
Reply
#3
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')
Reply
#4
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?
Reply
#5
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
Reply
#6
Thanks!
Reply


Forum Jump: