1
Mach4 General Discussion / Re: PLC script to write to a text file
« on: March 12, 2024, 04:38:48 PM »
Try this:
Code: [Select]
-------------------------------------------------------
-- 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
file = nil
end
end
[/code