Hello Guest it is April 28, 2024, 03:25:54 AM

Author Topic: Help with C# DLL usage development  (Read 698 times)

0 Members and 1 Guest are viewing this topic.

Help with C# DLL usage development
« on: January 18, 2022, 04:04:53 PM »
I'm trying to move code from Mach3 to Mach4. We've got the DLL and SDK installed. Mach4 control is working within the Mach4 interface and our license is installed. Now...

We're using the C# interface via MachAPI.cs. The issue I'm having is I cannot figure out the sequence to perform a reset/ready on the machine. I've used the trace log to help a bit, however I get a "MERROR_NOT_NOW" error. Here's the general sequence:

Code: [Select]
int error = mc.mcCntlReset(mInst); // result 0

UIntPtr hSig = new(0);
_ = mc.mcSignalGetHandle(mInst, mc.OSIG_MACHINE_ENABLED, ref hSig);
int error3 = mc.mcSignalSetState(hSig, true); // result 0

int rc = mc.mcCntlEnable(mInst, 1); // result -18 MERROR_NOT_NOW

Is there something fundamental I'm missing? What else can I tell you? Calls such as mcMotorGetInfoStruct(...) work, but I cannot command the beast.

Thank you.
Re: Help with C# DLL usage development
« Reply #1 on: January 18, 2022, 05:37:51 PM »
Hi,
what is it that your trying to do? Mach4 has a perfectly good scripting language/environment in Lua.....so why use C#?

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Help with C# DLL usage development
« Reply #2 on: January 19, 2022, 11:28:21 AM »
I need to interact with sensors that affect the tool path planning in real time. Can Lua make DLL calls, particularly at at least millisecond timing precision?
« Last Edit: January 19, 2022, 11:31:16 AM by Cybernet »
Re: Help with C# DLL usage development
« Reply #3 on: January 19, 2022, 01:17:27 PM »
Hi,
if you require toolpath planning in realtime then I doubt Mach4 is the solution.

Mach4, in fact ALL Windows platform based CNC software, is a buffered motion control. That is to say the the trajectory planner
issues numeric P(osition) V(elocity)over T(ime) data to the motion controller in a buffer. For example the default buffer length of the ESS
is 180ms.

If you have a sensor that changes state even if Mach responds instantly with a Lua script or DLL call any alteration in tool path will
still have to propagate through the motion buffer.

I have heard that users with high power and well sorted PCs have reduced the motion buffer to 20-30ms.......but its still far from
realtime.

The PLC script in Mach4 runs at about 10ms intervals. The PLC script is programmed in Lua.

The PMC module is an emulated ladder logic module that runs at ms intervals. The ladder is enacted in C.....you don't code C directly.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Help with C# DLL usage development
« Reply #4 on: January 19, 2022, 01:35:20 PM »
Hi,
how are you connecting your sensor to Mach?

If you connect it to your motion controller/breakout board combination then you'll have to wait for the motion controller
reporting cycle before Mach gets to hear about the change in sensor state.

Using the ESS as an example, it has a deafult cycle frequency of 40Hz or 25ms interval. Thus if you have a switch connected to the
controller/BoB it could take as long as 25ms for Mach to get the changed signal state depending on when the event ocurred within
the cycle.

I have heard people increasing the cycle rate to 200Hz or 5ms intervals, but even 5ms is not realtime.

You might ask then 'how is it that Mach can respond instantly to Limit and Home switches'. The answer is that Mach does NOT respond instantly,
the motion controller DOES, and it does so autonomously. The events gets reported to Mach, using the same reporting cycle I described above,
but not instantaneously.

Clearly the motion controller manufacturer must program/hardwire his motion controller to handle certain events in realtime, and in a manner
which is consistent with Mach's expectations. For this reason the motion control plugin, written by the motion control manufacturer, is a technically
demanding but absolutely vital part of any Mach4 installation.

If you choose to write your own plugin to manage your sensor suite for instance you may. It requires that you sign an NDA with NFS and then you get
the inside gen on Machs core enough for you to write a plugin in C/C++.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Help with C# DLL usage development
« Reply #5 on: January 19, 2022, 01:50:01 PM »
c# its unmange you need extern:
like this:
DllImport(@"C:\Mach4Hobby\Mach4IPC.dll", CallingConvention = CallingConvention.StdCall)]//its delete the file from mach so can load new file       
        public static extern int mcToolGetCurrent(
        [param: MarshalAs(UnmanagedType.I4)] int mInst,
        [param: MarshalAs(UnmanagedType.I4), In(), Out()] ref int ret);
note: yours c# app must run 32 not 64 bit
Re: Help with C# DLL usage development
« Reply #6 on: January 20, 2022, 04:04:33 PM »
All other comments aside (and thank you!), the issue was the working directory. It needed to be the mach4hobby directory so that all the plugins, profiles, and other stuff was available during init. For anyone's future reference, this can be configured in the project properties -> Debug -> Open Debug launch profiles UI (as of VS Pro 2022).