Published Monday 25 November 2013
Categories: DIY Pick and Place | General |

Share this Share on Facebook Share on Twitter Share on Google Plus
Post View Counter 108

This demo app allows the user to send Gcode commands to Mach 3 to control a CNC mill, lathe or other cnc hardware.

The application was made using Visual Studio 2013 and the demo app can be downloaded here.

First create a new Windows Forms Application:

Under the Solutions Explorer expand the References entry, right click and select Add Reference:

Click the Browse button to open the file explorer:

Browse to the folder where you have installed Mach3, normally c:\Mach3 and select the Mach3.exe file and click Add, then on the next screen click OK.

Back in Visual Studio add a textbox and a button to your form. Use the default names for the textbox and button. Double click the form header to add the form load event handler and double click the button to add the button click handler.

In the top section of the code add:

using System.Runtime.InteropServices;

At the top of your Form1 class add:

private Mach4.IMach4 Mach = null;
private Mach4.IMyScriptObject Script = null;

In the Form1_Load handler add:

Mach = (Mach4.IMach4)Marshal.GetActiveObject("Mach4.Document");
Script = (Mach4.IMyScriptObject)Mach.GetScriptDispatch();

In the button1_Click event handler add:

Script.Code(textBox1.Text);

Now you need to launch Mach3 using Run as Administrator.

Build the application and go to the build folder and Run as Administrator. You will now be able to enter gcode commands in the text box and they will be sent to Mach3 when the button is pressed.

If you need to update the GUI while the command is running you will need to use Threading in the application and run the Script.Code commands under their own thread.

Use this code at your own risk! I accept no responsibility if your home blows up, computer crashes or cnc mill drives away as a result of using this demo.

It appears that some installations of Mach3 do not correctly set the registry keys for the Mach4 objects. I have exported all the registry keys which Mach3 has installed on my computer into a text file which will need to be renamed from .txt to .reg to install the missing registry keys. Download reg file.

Also ensure that you have the latest version of Mach3 installed as the older versions may not compile correctly.

Permalink