77
« Last post by SwiftyJ on March 10, 2024, 04:24:35 AM »
I think you should only close the file when you want to stop recording the data i.e. when output 9 turns off. If you close it after every time you write data you will need to open the file each time, but you are only opening it once when output 9 turns on. Something like this...
-------------------------------------------------------
-- position Data
-------------------------------------------------------
local val_x = mc.mcAxisGetPos(inst, mc.X_AXIS) -- get X position
local val_y= mc.mcAxisGetPos(inst, mc.Y_AXIS) -- get Y position
WriteRegister("X_pos",val_x) -- to monitor in register diagnostics ok
WriteRegister("Y_pos",val_y) -- to monitor in register diagnostics ok
local LoadCellSignal = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT9 ) -- monitor output 9 activated by macro
local LoadCellSignalState = mc.mcSignalGetState(LoadCellSignal)
if (LoadCellSignalState == 1) then -- if output 9 is activated
local hregTime_LC = mc.mcRegGetHandle(inst, "iRegs0/Time_LC") -- get register Time_LC = 0
local LoadCellTime= mc.mcRegGetValue(hregTime_LC)
if (LoadCellTime==0) then
local Path = GetRegister(Layer_path) -- get File path from register
local MyFile = wx.wxGetCwd() .. Path -- define the file path in Mach4
file = io.open(MyFile, "a") -- open file in append mode
end
LoadCellTime = LoadCellTime+0.05 - follow PLC cycle time as sample rate
WriteRegister("Time_LC",LoadCellTime) -- monitor in register ok
local text = string.format ("%.4f, %.4f, %.4f \n", val_x, val_y, LoadCellTime );
file:write(text ) --Write the Gcode file
file:flush() --Save written data
else
if (file ~= nil) then
file:close() --Close file
end
end
[/code