Logic Machine Forum
Write in a Single Bit of a Word - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Write in a Single Bit of a Word (/showthread.php?tid=2475)



Write in a Single Bit of a Word - UfficioTecnicoEE - 14.02.2020

Hello Everyone,
I have to write a single bit of a register with a boolean value.
Can you help me with a script?
In the attachment you can find the register that I have to use.
For example I have to command the Bit0



Thank you so much at all!

Luigi


RE: Write in a Single Bit of a Word - admin - 14.02.2020

See this: https://forum.logicmachine.net/showthread.php?tid=2464&pid=15619#pid15619


RE: Write in a Single Bit of a Word - UfficioTecnicoEE - 14.02.2020

(14.02.2020, 12:10)admin Wrote: See this: https://forum.logicmachine.net/showthread.php?tid=2464&pid=15619#pid15619

Thanks for your reply, but I'm a beginner in this things.
Why I have to calculate the value of 3 object together? The post that you send me is good for 1 object also?
Thank you so much for your patience


RE: Write in a Single Bit of a Word - admin - 14.02.2020

Example can be changed to accept any number of input objects.
If you want to convert boolean value to a number you can do this via an event script (1/1/2 is output object):
Code:
value = event.getvalue()
out = value and 1 or 0
grp.write('1/1/2', out)



RE: Write in a Single Bit of a Word - UfficioTecnicoEE - 14.02.2020

(14.02.2020, 12:15)admin Wrote: Example can be changed to accept any number of input objects.
If you want to convert boolean value to a number you can do this via an event script (1/1/2 is output object):
Code:
value = event.getvalue()
out = value and 1 or 0
grp.write('1/1/2', out)

Sorry, I explain all my situation so I can understand all better.
I have created a script that read all the bit of a register (in this case the register 1280):
Code:
require("user.GetBit")

local value
local Status
local Byte        -- Matrice di valori

--- Modbus ---
mb = require('mbproxy').new()
mb:setslave(1)

-- Lettura variabili ——
Status= mb:readregisters(1280) ---- Byte
--log (Status)
if Status ~= nil then
  Byte = {getbit(Status, 0),
    getbit(Status, 1),
    getbit(Status, 2),
    getbit(Status, 3),
    getbit(Status, 4),
    getbit(Status, 5),
    getbit(Status, 6),
    getbit(Status, 7),
    getbit(Status, 8),
    getbit(Status, 9),
    getbit(Status, 10),
    getbit(Status, 11),
    getbit(Status, 12),
    getbit(Status, 13),
    getbit(Status, 14),
    getbit(Status, 15)
  }
  log (Status)
    for i=0, 16, 1 do
      grp.update ('Stato.reg.1280_Bit' .. i ,Byte[i+1])
  end
end

mb:close()
so at my address 32/1/1 there is the boolean value of the Bit0 of the register 1280.
Now I have to command the Bit0 of the register n°1280 with a boolean value.
Can you please write me all the script, so I can write it for the other commands.
Thank you very much and sorry for bothering you.


RE: Write in a Single Bit of a Word - Daniel - 14.02.2020

Read this thread https://forum.logicmachine.net/showthread.php?tid=1922&pid=11915#pid11915
This should explain you all what Admin has in mind.


RE: Write in a Single Bit of a Word - UfficioTecnicoEE - 14.02.2020

Thank you very much both!

So this script is correct if I wanna set Bit0 of register 1280 to 0 when 32/1/1's value is 1
Code:
function setbit(value, bitnr)
  local mask = bit.lshift(1, bitnr)
  return bit.bor(value, mask)
end

value = 0

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

log(value)

mb = require('mbproxy').new()
mb:setslave(1)

-- set register at address 1000 to 123
mb:writeregisters(1280, 0)


mb:close()
And another question the 123 near the code below what value is??
Code:
-- set register at address 1000 to 123



RE: Write in a Single Bit of a Word - Daniel - 14.02.2020

This line is just the comment 123 is the value you write. in line 18 instead of 0 you should have value


RE: Write in a Single Bit of a Word - UfficioTecnicoEE - 14.02.2020

Now I have to set bit 3 of register 1280 to 1

This is the right code?
Code:
require("user.SetBit")

mb = require('mbproxy').new()
mb:setslave(1)

value = 1

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

log(value)

mb:writeregisters(1280, value)

mb:close()
Thanks