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
#1
Hi,
I have to write bits in a 16-bit register (bitmask) see attachment. This is a control-word for a eaton DC1 freq.converter.
On start I have to set 
Bit2=Fault reset, and Bit8=Control 
Speed reference is set to another register.

I found this thread about reading bit in a bitmask, but can`t figure out how to use the function to write.
https://forum.logicmachine.net/showthrea...50#pid2850

-John

Attached Files Thumbnail(s)
   
Reply
#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
#3
Thank you!
Reply
#4
Hello,

I have also made the inverse resetbit() function that could be useful :


Code:
function resetbit(value, bitnr)
 local mask
 mask = bit.lshift(1, bitnr)
 mask = bit.bnot(mask)
 return bit.band(value, mask)
end
Reply


Forum Jump: