01.07.2022, 12:05
With RS485 , Im trying to control a blind.
Controlling works fine but when the bilnd gives LM the status string, It seems to be returning the same stirng repeatedly.
In the log image that I attached, at 20:35:43 I get a string which tells me that bilnd is at 65% position
and when the bilnd reached at 90% position at 20:35:49, I get the same string again along with the string tells me that blind is at 90% position.
Is there any problem with my script or is it seems to be a specification of the blind itself?
Controlling works fine but when the bilnd gives LM the status string, It seems to be returning the same stirng repeatedly.
In the log image that I attached, at 20:35:43 I get a string which tells me that bilnd is at 65% position
and when the bilnd reached at 90% position at 20:35:49, I get the same string again along with the string tells me that blind is at 90% position.
Is there any problem with my script or is it seems to be a specification of the blind itself?
Code:
if not port then
require('serial')
port = serial.open('/dev/RS485-1', {
baudrate = 9600,
databits = 8,
stopbits = 1,
parity = 'even',
duplex = 'half'
})
line = ''
end
function split(str, delim)
-- Eliminate bad cases...
if string.find(str, delim) == nil then
return { str }
end
local result = {}
local pat = "(.-)" .. delim .. "()"
local lastPos
for part, pos in string.gfind(str, pat) do
table.insert(result, part)
lastPos = pos
end
table.insert(result, string.sub(str, lastPos))
return result
end
function str2hex(str)
raw_len = string.len(str)
i = 1
while i <= raw_len do
current_hexvalue = '0x' .. string.format("%02x", string.byte(str, i))
if value then
value = value .. ', ' .. current_hexvalue
else
value = current_hexvalue
end
i = i + 1
end
return value
end
res, err = port:read(30, 6)
--port:flush()
if res then
decoded = str2hex(res)
-- log(decoded)
t = split(decoded, "0x10, 0x02")
--log(t)
for k,v in pairs(t) do
log(v .. 'STATUS RETURNED')
end
end