function m6() package.path = package.path .. ";./Modules/?.lua;" package.cpath = package.cpath .. ";./Modules/?.dll;" local inst = mc.mcGetInstance() local out = io.stderr local selectedtool = mc.mcToolGetSelected(inst) local currenttool = mc.mcToolGetCurrent(inst) rs232 = require("luars232") socket = require("socket") port_name = "COM3" local timeout = 5 local count = 0 if selectedtool == currenttool then return mc.mcCntlSetLastError(inst, "Desired tool in place.") else mc.mcCntlGcodeExecuteWait(inst, "G4 P1000 ") mc.mcCntlGcodeExecuteWait(inst, "G53 G0 X-2.0 Z-2.0 ") e, p = rs232.open(port_name) wx.wxSleep(1) if e ~= rs232.RS232_ERR_NOERROR then mc.mcCntlSetLastError(inst,"Cannot Open Serial Port") return end assert(p:set_baud_rate(rs232.RS232_BAUD_9600) == rs232.RS232_ERR_NOERROR) mc.mcCntlGcodeExecuteWait(inst, "G4 P1500 ") assert(p:set_data_bits(rs232.RS232_DATA_8) == rs232.RS232_ERR_NOERROR) assert(p:set_parity(rs232.RS232_PARITY_NONE) == rs232.RS232_ERR_NOERROR) assert(p:set_stop_bits(rs232.RS232_STOP_1) == rs232.RS232_ERR_NOERROR) assert(p:set_flow_control(rs232.RS232_FLOW_OFF) == rs232.RS232_ERR_NOERROR) p:write(tostring(selectedtool),timeout) if selectedtool>currenttool then count = (selectedtool - currenttool) else count = (8 + selectedtool - currenttool) end mc.mcCntlGcodeExecuteWait(inst, "G4 P"..count) mc.mcToolSetCurrent(inst, selectedtool) p:close() mc.mcCntlSetLastError(inst,"Toolchange completed") end if(mc.mcInEditor() == 1) then m6() end end