Hello Guest it is April 21, 2021, 02:30:35 PM

Author Topic: Connecting to Mach4  (Read 3523 times)

0 Members and 1 Guest are viewing this topic.

Connecting to Mach4
« on: February 05, 2017, 04:39:51 PM »
Does anyone have an example of an application connecting to Mach4?  Like telling Mach4 to load a gcode file?


Re: Connecting to Mach4
« Reply #1 on: February 05, 2017, 06:15:06 PM »
Hi rhtuttle,
not quite sure what you mean....

An 'application' do you mean another program like Notepad++ or Excel?

Getting Mach to load a Gcode file is reasonably straight forward. This comes from the API.chm:
      number mInst,
So if you have a string for the file you wish to load execute the API call and it loads...easy

If this is what you mean let me know and I will post some examples. In fact if you look in Mach4Hobby/LuaExamples folder you will find plenty.

My wife left with my best friend...
     and I miss him!
Re: Connecting to Mach4
« Reply #2 on: February 06, 2017, 11:34:09 AM »
Craig,  thank you for taking time to help with this question.

I program mostly in Delphi Object Pascal and have written a few apps that access Mach3 and control it externally.  If you have time to waste:



I have a database that I draw off of to create some parts.  It would be simply for me to write an app that generates the gcode and then programmatically tell mach to load and run that file.  Mach4, afaik, does not expose a com interface at this time.  I was wondering if some other tool or sdk is available.



Re: Connecting to Mach4
« Reply #3 on: February 07, 2017, 02:57:15 AM »
had a look at the vids, very interesting. Need to ask the question...why? Seems like you've just made a personalised
copy of Mach3, in these vids. Is there something that you can do with these things that Mach cant do?

I never understood the 'com' setup in Mach3 so wont be of any help for Mach4 either. The scripting manual has a description
of LUA panels but that would require you code in LUA. I read in one of the LUA manuals that its possible to have LUA run
compiled code fragments written in whatever. Would that achieve the same result?

I note also that Mach4 plugins tend to be written in C/C++ but would any compiled code in a plugin not work provided
it calls the API functions correctly?

Even if either or both concepts could be made to work still have to ask the question...why? Is there something about your
database that means it can only be used by a particular language?

My wife left with my best friend...
     and I miss him!
Re: Connecting to Mach4
« Reply #4 on: February 07, 2017, 12:10:26 PM »
Good morning Craig,

Yes it is a personalized copy, plus a lot more.  What wasn't shown was that one of the icons on the top starts another application with several tabbed notebook pages with probably a hundred fields to do various tasks that I use in making bamboo fly rods, reel seats and other hardware.  When I started this project altering any screen set in mach3 required using screen4 (nick named scream4 since it crashed often and lost all of the changes you made) require bitmaps for everything visual and wasn't sizable.  It would have taken ten times as long to develop the screens in Mach3.  My modules are all sizable, can be hidden or shown, and buttons customizeable without closing down and reopening the app like you have to do even now in Mach4.  The MDI is always available, the Gcode is run a line at a time so I didn't have to put in a bunch of while isMoving Wend statements.  I could load and unload a sets of buttons with predefined Gcode or scripts (think probing), create leds and user DROs to show variable values on the fly which was great for testing scripts.  Too many more benefits to name here.    

Mach4 has certainly gone a long way to eliminate some of the shortcomings especially in screen set development and making it event driven like mine.  Yes, I could now create lua panels to replicate what I have but that is redundant, would take 100's of hours to design and code and should not be necessary.

A plugin could be made to work but my reading of the Mach4 core API you must be a 'licensed developer' to be able to install one.

I could write my database to a csv file and load that data but then any changes made to the database requires that I export the data for each change.  That's bad system management, just asking for trouble.  I can create a dll that a lua script should be able to load and call functions in the dll, I haven't gone down that raod yet, I would rather use what I have already poured hundreds of hours of time into by just being able to instantiate a COM interface to Mach4 and tell it to run a file.  

And this is just a hobby!

Re: Connecting to Mach4
« Reply #5 on: February 08, 2017, 09:04:11 AM »
A plug-in (MSVC DLL) compiled against the Mach4 API should be working. But you are required to sign your plug-in with a special developer key. I'm not sure if the M4 team currently hands those keys to any other than hardware devs (e.g. motion plugins).

Using the Lua code should also be fine. You can add your own wizards. Havent tried building a TCP client/server app in a wizard, but the socket library (see subfolder "Modules") provides anything you need.

Offline smurph

  • *
  •  1,384 1,384
  • "That there... that's an RV."
    • View Profile
Re: Connecting to Mach4
« Reply #6 on: February 11, 2017, 08:20:08 PM »
Delphi can run time link to DLLs, right?  Just pull in the functions from MachIPC.dll.  The API docs are in the docs folder.  It will be the same as pulling in C functions from WINAPI.  There is no COM in Mach4 as it is designed to be platform independent. 

Two functions that a remote app should use that are not documented in the API documentation:

MACHAPI int MACH_APIENTRY mcIpcInit(const char *ipAddr)
MACHAPI int MACH_APIENTRY mcIpcCleanup(void)

mcIpcInit() is the first thing that should be called by the application.  The parameter is the IP address of the Mach instance to control. for app running on the same machine as Mach. 

mcIpcCleanup() is the last thing your app should call before shutting down. 

Re: Connecting to Mach4
« Reply #7 on: February 12, 2017, 02:19:23 PM »

Thanks for the socket idea, I will look some more at lua sockets.
Re: Connecting to Mach4
« Reply #8 on: February 12, 2017, 02:23:53 PM »
Steve, thanks for taking time to respond and for the two non-documented functions.  Delphi can run time link to dll's.  I have been unable to find the machAPI.h file.  I have searched the Mach4Hobby directory and it is not there.  Is this another one where you have to be a licensed developer to get it?
Re: Connecting to Mach4
« Reply #9 on: February 12, 2017, 02:29:17 PM »
Spent a little time to see if I could create a DLL in Delphi that Mach4 could communicate with.

From the Delphi side:

Code: [Select]
library MyLuaTest;


{$R *.res}

  function Func1(L:pointer):integer; cdecl;
     lua_pushstring(L,pchar('test My open'));

  function luaopen_MyLuaTest(L:pointer):integer; cdecl;
//    showMessage('open call');
    Result := 1;



from Mach4, Button left up script:

Code: [Select]
local inst = mc.mcGetInstance() -- Get the instance of Mach4

--wx.wxMessageBox("before require");

wx.wxMessageBox("before func1");



Can be done.