Hello Guest it is July 08, 2020, 08:06:56 PM

Author Topic: M Code "Handshake" – Need Help  (Read 2287 times)

0 Members and 1 Guest are viewing this topic.

M Code "Handshake" – Need Help
« on: October 05, 2015, 03:42:12 PM »
I need help getting an Mcode set up, it would function something like this:

   function m100()
   Check INPUT1
   IF Active (LaserReady) THEN
                   Activate OUTPUT1 (FireLaser, Note: Signal must be active for at least 50msec.)
         Wait for INPUT1 to go Inactive
         Deactivate OUTPUT1
                   Wait for INPUT1 to return to Active
                   Return to Gcode
                   Display Error 'Laser Not Ready'

As you can probably see I only need to check the INPUT1 the first time M100 is called in the Gcode. I’m not sure how to write this and was hoping someone could help.
Re: M Code "Handshake" – Need Help
« Reply #1 on: October 05, 2015, 04:11:17 PM »

You should be able to find a "Scripting Manual.pdf" within your Mach4 installation directory (Mach4Hobby/Docs/Scripting Manual.pdf). In that manual, a good example can be found on page 18.

Additionally, a "LuaCalls.txt" file can be found in this same directory which contains all the available scripting commands.

local inst = mc.mcGetInstance()

local hsig = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEFWD)

local spinstate = mc.mcSignalGetState(hsig)

if (spindstate == 0) then
      mc.mcSignalSetState(hsig, 1)
      mc.mcSignalSetState(hsig, 0)


In the case of your "Laser Ready", you can use one of the generic signals (e.g. ISIG_INPUT0 etc.). The same goes for the output that you'd want to activate (e.g. OSIG_OUTPUT0 etc.)

For waiting, you can keep checking the state of your input under a  while loop and using a sleep.

input1State = mc.mcSignalGetState(input1Handle)

while input1State <> 0 do

     input1State = mc.mcSignalGetState(input1Handle)


 mc.mcSignalSetState(output1Handle, 0)

I do recommend that you go through the documents that I had mentioned above, otherwise my explanation may make little sense without prior knowledge of how lua scripting works in Mach4.

Vital System Inc.
« Last Edit: October 06, 2015, 03:47:43 PM by Vital System Support »

Offline simpson36

  •  1,371 1,371
    • View Profile
Re: M Code "Handshake" – Need Help
« Reply #2 on: October 30, 2015, 09:06:56 AM »
Mach4CoreAPI is the document you want to use. The LuaCalls.txt is just a listing of the functions with zero explanation of what they do or how to use them.

Using loops in your code is risky business and can easily lead to the system hanging.

The PLC has very fast loop times so additional loops inside the PLC are generally not needed except in extremely critical time sensitive events.

You can monitor 'state changes' (first time thru M100, etc) by adding variables to hold a previous state. If you create variables in the screen load script, they are global (perpetually available from anywhere) and will maintain the last value assigned, so adding to the previous example, you can do something like

M100FirstPass = true   <= put these in the Screen Load script

function M100()
  if  (M100FirstPass == true) then
        M100FirstPass = false       <= this line will prevent any more passes thru this routine


« Last Edit: October 30, 2015, 09:11:39 AM by simpson36 »