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.

RS485 resident
#1
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
Reply
#2
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
Reply


Forum Jump: