Logic Machine Forum
RS485 resident - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Gateway (https://forum.logicmachine.net/forumdisplay.php?fid=10)
+--- Thread: RS485 resident (/showthread.php?tid=4708)



RS485 resident - rw_echo - 12.04.2023

When I press the RS485 panel button, I can use the LM resident script to read the command message of the panel. The message received every time I press the button is the same. I want to control the light switch, and my script has implemented the light on action. How can I turn off the light? Can anyone help me? Thank you in advance. 

button cmd(hex): 02 20 10 11 00 01 00 80 4E E3 02 20 10 11 00 01 00 FF 0F 03

code:
Code:
if not port then
    require('serial')
  port = serial.open('/dev/RS485-1', {
    baudrate = 9600,
    databits = 8,
    stopbits = 1,
    parity = 'none',
    duplex = 'half'
  })
 
    function read()
      local buf = {}
 
      while true do
        local timeout = #buf > 0 and 0.5 or 19
        local char = port:read(1, timeout)
       
        if char then
          buf[ #buf + 1 ] = char:byte()
        else
          return buf
        end
      end
    end
  end
 
  res = read()
  --log(res)
 
  if #res == 20 then
    for key, value in pairs(res) do   
        if res[4] == 0x11 and ( res[8] == 0x80 or res[18] == 0xFF ) then     
            if grp.find("13/1/21")["data"] ~= true then
                grp.write("13/1/21",true)
                log("key1sucessfull")
            end     
        end   
    end
  end



RE: RS485 resident - admin - 12.04.2023

For loop after if is not needed otherwise you write might run 20 times in a row.
To invert the current value use this:
Code:
if #res == 20 and res[4] == 0x11 and (res[8] == 0x80 or res[18] == 0xFF) then
  value = grp.getvalue('13/1/21')
  grp.write('13/1/21', not value)
end