Hello Guest it is May 22, 2024, 11:20:12 AM

Author Topic: M6 macro with serial connection  (Read 309 times)

0 Members and 1 Guest are viewing this topic.

M6 macro with serial connection
« on: February 21, 2024, 11:35:40 AM »
Hi all

I am currently working on the M6 macro to interface with my toolerator3000. There are multiple problems with the code I wrote so far apparently, when I run the code in the debugger my toolchanger will rotate to the correct position and the tool number will update correctly in mach. However, when I try to run this trough the M6 command, the selected tool number in mach changes but my tool changer does nothing. Also, I want to make sure that the toolchanger is very close to the home position so I added a line of code to send it there but also that doesn't seem to work.

Any help on this would be appreciated  :)


This is the code i'm running:

function m6()
   local inst = mc.mcGetInstance()
   local out = io.stderr
   local selectedtool = mc.mcToolGetSelected(inst)
   local currenttool = mc.mcToolGetCurrent(inst)
   local out = io.stderr
   local timeout = 5
   local count =0
   rs232 = require("luars232")
   port_name = "COM3"

if selectedtool == currenttool then
   return
else
   mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G00 X-1.0 Z-1.0")
   e, p = rs232.open(port_name)
   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)
   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)

   mc.mcToolSetCurrent(inst, selectedtool)
   p:close()
   
   mc.mcCntlCycleStart(inst)

end

if(mc.mcInEditor() == 1) then
   m6()
end
end


Thanks in advance!
Rob