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 sent single value bit
#1
Hello, maybe you can help me, how can I send the value only to 8bit, because if I use this script everything works, but it also activates 1bit as an example.
I want to send the value only as an example 8bit

require('luamodbus')
mb = luamodbus.tcp()
mb:open('10.30.0.242', 8000)
mb:connect()
mbConfusedetslave(1)

bendra_reiksme = mb:readregisters(1000)

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

value = setbit(1, 8)

mb:writeregisters(1000, value)

mb:close()

Attached Files Thumbnail(s)
   
Reply
#2
Try this:
Code:
require('luamodbus')
mb = luamodbus.tcp()
mb:open('10.30.0.242', 8000)
mb:connect()
mb:setslave(1)

value = mb:readregisters(1000)

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

value = setbit(value, 8)

mb:writeregisters(1000, value)

mb:close()

The recommended approach is to use profiles.
Reply
#3
This script is the same as mine. How can I send a value of only 8bit, because now 1bit is momentary, and when I send a value of 8bit, it also activates 1bit
Reply
#4
(12.07.2024, 11:33)TomasIha Wrote: This script is the same as mine.

It isn't the same (left is original, right is modified):
   
Reply
#5
Hi, I tried this script. Everything works, but when this value is sent to the modbus register, the other bits of the modbus address also receive values ​​and trigger other device functions. How can change only 8bit value
Reply
#6
Check if your device supports function code #22 (Mask Write Register).
Reply
#7
If I find out that it supports it, maybe you can give an example of the script
Reply


Forum Jump: