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.

Checksum XOR 4 bytes
#6
(22.12.2021, 08:00)admin Wrote: Can you describe your task in more detail? Protocol description would be helpful as well.
The main rule with serial ports is that only a single script can access the port. For reading you can set read length to -1 to skip waiting and read only the data that is already buffered by the driver.

Good afternoon. I need to constantly poll the room presence sensors through the rs232 port, on average, all sensors are polled once every 2.5 seconds. I transfer the result of the survey to group addresses. My script is hovering at the bottom
Code:
if not port then require('serial')     port, err = serial.open('/dev/RS232', {baudrate = 38400, databits = 8, stopbits = 1, parity = 'none', duplex = 'full'})     if port then     port:flush()   else     log('Error open port in: ' .. err)   end end function readline(port, timeout)   local char, buf   buf = {}   while true do     char = port:read(1, timeout or 0.2)     if char == nil or char == '\n' then       break      elseif char ~= '\r' then       table.insert(buf, char)     end   end   return table.concat(buf) end function writetoport(port, timeout, command)   port:write(command)   line = readline(port, timeout)   if #line > 0 then       return line   else     return "No reply received"   end end function str2hex(str)     raw_len = string.len(str)     i = 1       value = ''   while i <= raw_len do     if value then       if i == 4 then               value = string.byte(str, i)       end     else       value = string.byte(str, i)     end     i = i + 1   end   return value end function encode(...)   local checksum = bit.bxor(...)   return string.char(...) .. string.char(checksum) end addrs = {0x01,0x03,0x05,0x07,0x09,0x0b,0x0d,0x0f,0x11,0x13,0x15,0x17,0x19,0x1b,0x1d,0x1f,0x21,0x23} local sum = 0 for _, addr in ipairs(addrs) do   command = encode(0x04,0x04,addr,0xc8)   reply = writetoport(port, 0.1, command)   decoded = str2hex(reply)   if grp.getvalue('37/3/'.. sum)~=decoded then         grp.update('37/3/'.. sum, decoded)   end   sum = sum + 1 end value = event.getvalue() if value == true then     grp.update(event.dst, false) elseif value == false then     grp.update(event.dst, true) end

At the end, I check the state of the address where the script is and run it anew when the request is received.
Reply


Messages In This Thread
Checksum XOR 4 bytes - by sashhun - 21.12.2021, 13:22
RE: Checksum XOR 4 bytes - by admin - 21.12.2021, 13:49
RE: Checksum XOR 4 bytes - by sashhun - 21.12.2021, 14:52
RE: Checksum XOR 4 bytes - by sashhun - 21.12.2021, 20:23
RE: Checksum XOR 4 bytes - by admin - 22.12.2021, 08:00
RE: Checksum XOR 4 bytes - by sashhun - 22.12.2021, 08:24
RE: Checksum XOR 4 bytes - by admin - 22.12.2021, 08:45
RE: Checksum XOR 4 bytes - by sashhun - 22.12.2021, 09:51

Forum Jump: