FAQ about M-Code


Our FAQ about G-Code page is here.

 



What is M-Code?  (Plus M11/M10 and M62/M63)

M-Code is similar to G-Code, other than it allows you to run a Macro.   Macros allow you to create logic that is specific to your hardware and system.

Here is a good article by MachMotion about Mach3 and Mach4's use of M-Codes.

Here is another MachMotion manual documenting G-Codes and M-Codes in Mach3.

Here is another MachMotion manual documenting G-Codes and M-Codes in Mach4.

Here is the Wikipedia article, about G-Code and M-Code. Not all information in this Wikipedia article is implemented Mach3 or Mach4.

 



M11/M10 and M62/M63

Macros have a delay associated with them, that can be up to 500 ms in Mach3, and cannot be relied upon for precise timing.  For most milling operations this delay will not be an issue for activating and deactivating outputs.  However, for laser or plasma cutting systems this extra 500 ms of delay, or imprecise timing, can cause burn through, excess cutting, or continued cutting in areas where the laser should be off.   

For real time modification of digital outputs, or more precisely synchronizing the change of digital outputs with motion, we have a pair of commands that we can use in Mach3 and Mach4. 

In Mach4 these two special real time digital output control macros are M62 and M63, which turn an output on or off,  synchronized with subsequent motion command. 

In Mach3 these two special real time digital output control macros are M10 (axis clamp or axis lock) and M11 (axis unclamp or axis unlock). 

Even though M10/M11 were commonly being used as digital outputs synchronized with motion in Mach3, they are technically for axis clamping.  Since M62/M63 is the current standard for digital outputs synchronized with motion, Mach4 made that change away from M10/M11, resulting in two sets of macro numbers.

The M10/M11 and M62/M63 Macros are loaded and then executed at the beginning of the next motion command (so call a G0, G1, G2 or G3 motion command right away after your M10/M11/M62/M63 command).  

To make sure the M10/M11 or M62/M63 works as desired, it is best to follow these guidelines:

  • The M10Px, M11PX, M62PX or M63PX command should be by itself on the line immediately before the subsequent G0, G1, G2 or G3 command.  Don't mix other commands in between!
  • If the G0, G1, G2 or G3 motion command following the M62Px M63Px command doesn't cause motion of at least one axis, Mach4 will silently ignore the M62Px M63Px command. 
  • If the G0, G1, G2 or G3 motion command following the M10Px M11Px command doesn't cause motion of at least one axis, Mach3 may insert up to a 500ms delay instead of generating a real time ON or OFF.

The commands are as follows, with x referring to digital output 1, 2, 3, 4, 5 or 6.

  • M10Px      Turns Output x OFF, synchronized with subsequent motion command.  Used in Mach3.  (Logically backwards.)
  • M11Px      Turns Output x ON, synchronized with subsequent motion command.  Used in Mach3.  (Logically backwards.)
  • M62Px      Turns Output x ON, synchronized with subsequent motion command.  Used in Mach4.
  • M63Px      Turns Output x OFF, synchronized with subsequent motion command.  Used in Mach4.

 

If you issue a M62/M63 command and the next GCode command generates no movement from the current position, the M62/M63 command will be ignored/dismissed by Mach4. You NEED to have NEW movement specified immediately after a M62 or M63 command, for the M62/M63 command to be processed and acted upon.

 

To set this up in Mach3, go to Mach3's Menu -> Plugin Control -> Main Config ESS-v*****

  • Check the box for Output Mode
  • Assign the correct output number
  • Make sure your G-Code includes  M11P3 or M10P3 (the 3 indicates output #3) commands followed by a move command, like this Mach3 Example or this Mach4 Example.

 

M11 and M10 outputs

 

 



Example Macros

Here are my macros for Mach3.  Unzip them and place all of the .m1s files in your "C:\Mach3\Macros\YOUR PROFILE NAME\" folder.  

  • M101 through M120 Turn ON Ooutput1 through Output20
  • M201 through M220 Turn OFF Ooutput1 through Output20
  • This also includes my script for the Mach3 Minimum Spindle Speed macro.
  • Here is a video that shows how to make Macros that turn output7 on and off in Mach3. Originally I used M107 to turn Output7 ON and M157 to turn Output7 OFF.  To make this match the way I am handling my macros in Mach4, I now use M107 to turn Output7 ON and M207 to turn Output7 OFF.  I will be remaking this video.
  • Mach3 macros are written in VB (Visual Basic)

 

Here are my macros for Mach4. Unzip them and place all of the .mcs files in your "C:\Mach4Hobby\Profiles\YOUR PROFILE NAME\Macros\" folder. 

  • M100 through M163 Turn ON OUTPUT0 through OUTPUT63
  • M200 through M263 Turn OFF OUTPUT0 through OUTPUT63

 

Important things to keep in mind with Mach4:

  • You MUST have a macros folder inside your profile!  i.e. "C:\Mach4Hobby\Profiles\YOUR PROFILE NAME\Macros\".  If you don't your spindle will not work and other weird things happen to Mach4.
  • Mach4 Macros are written in lua, which is way more powerful than VB.




Mach4 lua function Error return codes are as follows:

 0 = MERROR_NOERROR
-1 = MERROR_INVALID_INSTANCE
-2 = MERROR_INVALID_ARG
-3 = MERROR_INVALID_DIR
-4 = MERROR_INVALID_PROFILE
-5 = MERROR_FILE_EXCEPTION
-6 = MERROR_FILE_EMPTY
-7 = MERROR_FILE_SHARING
-8 = MERROR_FILE_INVALID
-9 = MERROR_FILE_BADSIZE
-10 = MERROR_REGEN_DONE
-11 = MERROR_NODATA
-12 = MERROR_GRID_ACTIVE
-13 = MERROR_NOT_IMPLEMENTED
-14 = MERROR_MOTOR_NOT_FOUND
-15 = MERROR_INVALID_PARAM
-16 = MERROR_AXIS_NOT_FOUND
-17 = MERROR_API_INIT
-18 = MERROR_NOT_NOW
-19 = MERROR_NOT_CREATED
-20 = MERROR_SIGNAL_NOT_FOUND
-21 = MERROR_IO_NOT_FOUND
-22 = MERROR_SPIN_RANGE_NOT_FOUND
-23 = MERROR_PLUGIN_NOT_FOUND
-24 = MERROR_DEVICE_NOT_FOUND
-25 = MERROR_INVALID_STATE
-26 = MERROR_AXIS_NOT_ENABLED
-27 = MERROR_REG_NOT_FOUND
-28 = MERROR_IPC_NOT_READY
-29 = MERROR_NO_ROOM_AVALABLE
-30 = MERROR_NOT_COMPILED
-31 = MERROR_NOT_ENABLED
-32 = MERROR_SCRIPT_KILLED
-33 = MERROR_FILE_NOT_FOUND
-34 = MERROR_LIC_FEATURE_NOT_FOUND
-35 = MERROR_LIC_REQUIREMENT_NOT_FOUND
-36 = MERROR_LIC_EXPIRED
-37 = MERROR_LIC_BAD_ID
-38 = MERROR_LIC_BAD_KEY
-39 = MERROR_TOOLPATH_NOT_FOUND
-40 = MERROR_TIMED_OUT
-41 = MERROR_SOFTLIMITS
-42 = MERROR_FILE_BADFORMAT
-43 = MERROR_INVALID_TYPE
-44 = MERROR_TLM_ALLOC_FAILED
-45 = MERROR_TLM_TOOL_UNMANAGED
-46 = MERROR_TLM_GROUP_EXPIRED
-47 = MERROR_TLM_GROUP_NOT_FOUND
-48 = MERROR_TLM_GROUP_ADDED
-49 = MERROR_TLM_GROUP_CHANGED
-50 = MERROR_TLM_TOOL_NOT_FOUND
-51 = MERROR_TLM_TOOL_ADDED
-52 = MERROR_TLM_TOOL_CHANGED
-53 = MERROR_TLM_TOOL_EXPIRED

 



Mach3 Macro Pump

In Mach3 the Macro Pump runs 10 times every second, performing whatever task(s) you want it to do. Keep in mind that the macro pump should:

  • NOT use any delays, because it must complete in less than 1/10th of a second
  • NOT command machine movement

 

To use the Macro Pump, you will need to:

  • Create your macropump.m1s script and place it in "C:\Mach3\Macros\YOUR PROFILE NAME\" folder.
  • In Menu -> Config -> General Config... Check the button for "Run Macro Pump" in the 3rd column.
    Run Macro Pump
  • Restart Mach3 once (or twice).

 

Here is a very simple Macro Pump macropump.m1s script that will look for Input1 to go active, and then Activate or Deactivate Output1 accordingly.

' Activate this Macro Pump Macro by placing it in
' "C:\Mach3\Macros\YOUR PROFILE NAME\" Folder
'Then in Menu -> Config -> General Config...
' Check the button for "Run Macro Pump" in the 3rd column
'After you restart Mach3, it will run 10 times a second.
'If Input1 goes active, Output1 will go active.
'Modify to suit your needs.

If IsActive(Input1) Then
    ActivateSignal(Output1)
Else
    DeactivateSignal(Output1)
End If 


 


Mach3 script modification for minimum spindle RPM

If you go into the Mach3 VB Script editor, and change your "spindlespeed.m1s" macro file, you can make sure that your Spindle will never run below a minimum RPM that you want it to run at. 

Make sure that you are editing the correct Spindle Speed macro located in "C:\Mach3\Macros\YOUR PROFILE NAME\" since it will only affect that profile.


b1.png



b2.png




If you edit it so it looks like this, it will not let you command a speed less than 6000 RPM.  Use whatever value suits your needs.


rpm = GetRPM()

If rpm < 1 Then

rpm = 0

ElseIf rpm < 6000 Then

rpm = 6000

End If

SetSpinSpeed( rpm )

 

 

I added in the value of less than 1 shutting the spindle off, in case there ever was some rounding error wanting the spindle to turn at 0.0001 or something like that.

 

 

Go to top