21.12.2021, 14:52 (This post was last modified: 21.12.2021, 20:22 by sashhun.)
(21.12.2021, 13:49)admin Wrote: Use bit.bxor() and don't use quotes around numbers because not all functions will convert strings to numbers automatically.
Code:
function encode(...)
local checksum = bit.bxor(...)
return string.char(...) .. string.char(checksum)
end
Please tell me more, how can I make a script that sends a request to rs232, after execution it starts up again, I tried to make a resident script with an interval of 0 so it does not have time to read data from the port
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.
22.12.2021, 08:24 (This post was last modified: 22.12.2021, 08:25 by sashhun.)
(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
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.
This should be done via a resident script. Maybe add a small delay before each poll. You can also add port:flush() before writing and reading to clear the buffered data if there is anything left there.
(22.12.2021, 08:45)admin Wrote: This should be done via a resident script. Maybe add a small delay before each poll. You can also add port:flush() before writing and reading to clear the buffered data if there is anything left there.
I did thanks as advised, created a resident script with an interval of 3 seconds, and made the difference as a delay so that the script could finish the request, although I think I will remove the delay. The skrp is executed in an average of 2.5 seconds, and the difference in the interval of 3 seconds minus the query execution time will compensate for the pause after the execution of the script