211
Mach4 General Discussion / Re: Script with a short pause in the middle (non-blocking)
« on: March 14, 2019, 01:59:25 PM »
Wow, that is powerful.
Here is what I am thinking, simplified for readability
So I can simply call the coroutine/function by adding the code below to anything like a signal scrpt, plc script, button script, etc. The coroutine should be created in a suspended state at the top of the function.
It sounds like once you reach the end of the coroutine, it becomes dead and cannot be called again. This is not ideal since I want to periodically call this as long as it is not already running. How do you restart a dead coroutine? Online references aren't yet clear to me about this.
Thanks!
Here is what I am thinking, simplified for readability
Code: [Select]
---------------------------------------------------------------
-- Screen Load Script --
---------------------------------------------------------------
--Create a Timer which resumes the co-routine when expired
Panel = wx.wxPanel (wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize ) --Starts a Timer to handle drive response
Timer = wx.wxTimer(Panel)
Panel:Connect(wx.wxEVT_TIMER, function(event)
coroutine.resume(DriveComm) --Restart Communications without giving new parameters, we want the old ones to persist
end)
DriveComm = coroutine.create(DriveCommunications(a, b, c, d)
local saveA = a --We want to save the parameters passed into the function so they persist after the Yield and Resume
local saveB = b
local saveC = c
local saveD = d
Calculate transmission
Send Transmission
Timer:Start(5) --Start a 5ms timer
coroutine.yield(U3K) --pause script execution, coroutine becomes suspended
Read Drive Response
return data1
return data2
end )
So I can simply call the coroutine/function by adding the code below to anything like a signal scrpt, plc script, button script, etc. The coroutine should be created in a suspended state at the top of the function.
Code: [Select]
done, data1, data2 = coroutine.resume(DriveComm, a, b, c, d)
It sounds like once you reach the end of the coroutine, it becomes dead and cannot be called again. This is not ideal since I want to periodically call this as long as it is not already running. How do you restart a dead coroutine? Online references aren't yet clear to me about this.
Thanks!