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 - Write bits in word
#2
This script will set bit 0 if 1/1/1 is on, bit 1 if 1/1/2 is on etc. Adjust as needed and add writing to ModBus register at the end of the script.

Code:
function setbit(value, bitnr)
  local mask = bit.lshift(1, bitnr)
  return bit.bor(value, mask)
end

value = 0

if grp.getvalue('1/1/1') then
  value = setbit(value, 0)
end

if grp.getvalue('1/1/2') then
  value = setbit(value, 1)
end

if grp.getvalue('1/1/3') then
  value = setbit(value, 2)
end

log(value)
Reply


Messages In This Thread
Modbus - Write bits in word - by JohnTH - 07.12.2018, 10:57
RE: Modbus - Write bits in word - by admin - 07.12.2018, 11:16
RE: Modbus - Write bits in word - by JohnTH - 07.12.2018, 11:32
RE: Modbus - Write bits in word - by iJAF - 12.03.2019, 19:19

Forum Jump: