Hello,
maybe you know how do the data buffering in Lua and the LM. Maybe you have better solution than my.
Task is:
I must send a commands 1 after 1(not all in the same time) because device which accepts this commands is slow and there is no feedback.
E.g.
1. Pushbutton "Blind up" -> 2. GA script -> 3. Command(RS-485) -> 4. Command(Somfy RTS)
Command in Somfy RTS is very slow, normally 0,7s. So if someone want change slat angle this can produce 3 or more commands in 1 second. I want prepare the buffer for this commands because if I won't prepare the buffer a lot of commands will be not executed.
This is what I've prepared at this moment:
GA script (pushbutton up/down), it sends command to the buffer:
This is Somfy.buffer function (reads the buffer from the table 'bufor' from the storage, sends command and delete this command):
I have also Resident script for sending commands over RS485. Perfectly would be if I could sending the commands with 0.7s break between the commands. But at this moment I can't. I've tried with Resident with time=0s but there was a big CPU usage or strange operation(sometimes it sends the commands from the buffer, sometimes not). At this moment it works but with Resident with time=1s and this code(and unfortunately this is not 100% reliable because when I send 10 commands this mechanism send only 7 or 8):
And this is the Somfy.send function which I use for sending commands via RS485:
I must improve this solution, because I want have reliable(100%) solution and I want prepare faster solution (at this moment this is 1 command per second, and it will be good if this would be e.g. 0,7s).
Maybe you can give me some advice how improve this solution? Especially what are the principles of the resident script with time = 0s. Is this script is executed parallel with the other scripts or serial? In my script I am using storage for saving the buffer. Buffer is updated in this way:
- read from the storage, add new command to the buffer, save to storage(with event script),
- read from the storage, delete executed command, save to storage(with resident script 1s).
I think that in this solution there is a possibility that in a resident script I open the buffer table(from storage) and before saving by the resident script the table is changed by the event script but after this resident script save its calculated value and maybe this is the problem.
Maybe do you have some better mechanism for such bufferring commands or some advice how improve this scripts?
Thanks for all help
I've checked more precisely that the minimum break time between 2 commands can be 0.42 so theoretically in 1 second I could send 2 commands. This would be very comfortable for user.
maybe you know how do the data buffering in Lua and the LM. Maybe you have better solution than my.
Task is:
I must send a commands 1 after 1(not all in the same time) because device which accepts this commands is slow and there is no feedback.
E.g.
1. Pushbutton "Blind up" -> 2. GA script -> 3. Command(RS-485) -> 4. Command(Somfy RTS)
Command in Somfy RTS is very slow, normally 0,7s. So if someone want change slat angle this can produce 3 or more commands in 1 second. I want prepare the buffer for this commands because if I won't prepare the buffer a lot of commands will be not executed.
This is what I've prepared at this moment:
GA script (pushbutton up/down), it sends command to the buffer:
Code:
value = event.getvalue()
channel = Somfy.t0.ch00
if value then
Somfy.buffer(channel.down)
else
Somfy.buffer(channel.up)
end
This is Somfy.buffer function (reads the buffer from the table 'bufor' from the storage, sends command and delete this command):
Code:
buffer = function(command)
bufor = storage.get('bufor')
if type(bufor) == "table" then
table.insert(bufor, command)
else
-- buffer initialisation
bufor = {}
end
storage.set('bufor', bufor)
end
I have also Resident script for sending commands over RS485. Perfectly would be if I could sending the commands with 0.7s break between the commands. But at this moment I can't. I've tried with Resident with time=0s but there was a big CPU usage or strange operation(sometimes it sends the commands from the buffer, sometimes not). At this moment it works but with Resident with time=1s and this code(and unfortunately this is not 100% reliable because when I send 10 commands this mechanism send only 7 or 8):
Code:
main = function()
bufor = storage.get('bufor')
if type(bufor) == "table" then
if bufor[1] then
Somfy.send(bufor[1].command)
os.sleep(bufor[1].time)
table.remove(bufor)
else
--bufor pusty
os.sleep(1)
end
else
-- buffer initialisation
bufor = {}
end
bufor = storage.set('bufor', bufor)
end
And this is the Somfy.send function which I use for sending commands via RS485:
Code:
send = function (command)
require('serial')
port = serial.open('/dev/RS485', { baudrate = 4800, parity = 'odd', duplex = 'half', databits = 8, stopbits = 1 })
port:flush()
port:write(command)
port:close()
sleep(0.1)
end
I must improve this solution, because I want have reliable(100%) solution and I want prepare faster solution (at this moment this is 1 command per second, and it will be good if this would be e.g. 0,7s).
Maybe you can give me some advice how improve this solution? Especially what are the principles of the resident script with time = 0s. Is this script is executed parallel with the other scripts or serial? In my script I am using storage for saving the buffer. Buffer is updated in this way:
- read from the storage, add new command to the buffer, save to storage(with event script),
- read from the storage, delete executed command, save to storage(with resident script 1s).
I think that in this solution there is a possibility that in a resident script I open the buffer table(from storage) and before saving by the resident script the table is changed by the event script but after this resident script save its calculated value and maybe this is the problem.
Maybe do you have some better mechanism for such bufferring commands or some advice how improve this scripts?
Thanks for all help
I've checked more precisely that the minimum break time between 2 commands can be 0.42 so theoretically in 1 second I could send 2 commands. This would be very comfortable for user.