Running a .exe from M Codes

Moderators: TomKerekes, dynomotion

Post Reply
PBandJacob
Posts: 21
Joined: Thu Sep 02, 2021 6:02 pm

Running a .exe from M Codes

Post by PBandJacob » Mon Jul 25, 2022 6:33 pm

Hello!
I'm able to call an MCode set to run a PC application. I want to run this application whenever the device powers up, but I don't want it to run under other circumstances. Right now I have a "handler" .exe that checks to see if the other .exe is running. If the other .exe isn't running, it calls it and exits. If it is running it prints out a status message and exits. When I run the .exe's from the command line or by clicking on their icons, they behave as expected. When I try to call them as an MCode, I either get a "timeout waiting on action" message or I get an error stating "Error Executing PC Program: C:\\pathtofile\\, Return code = -1 Abort?"
I know that some processes when called generate multiple instances of themselves in the tasklist. I haven't had any trouble trying to call these programs multiple times in other cases, just when calling it from KMotionCNC's M codes.
Is the problem that I'm running an executable that calls another executable?
Any help is welcome.

User avatar
TomKerekes
Posts: 2529
Joined: Mon Dec 04, 2017 1:49 am

Re: Running a .exe from M Codes

Post by TomKerekes » Mon Jul 25, 2022 7:45 pm

Hi,

The issue might be that KMotionCNC launches the App with a hidden window. Unless your App changes to be shown then you won't see your Message Box.

Another workaround is to have KMotionCNC launch a .bat file (hidden) which then starts your App in a separate Process.

You might see this Thread.

HTH
Regards,

Tom Kerekes
Dynomotion, Inc.

PBandJacob
Posts: 21
Joined: Thu Sep 02, 2021 6:02 pm

Re: Running a .exe from M Codes

Post by PBandJacob » Mon Jul 25, 2022 8:20 pm

I switched it to a .bat file for execution and now it has:
"Error Executing PC Program:
C:\\Users\\pathtofile
Return code = 1
Abort?"
It got that same error when I put it in the KMotion CNC start up action.

User avatar
TomKerekes
Posts: 2529
Joined: Mon Dec 04, 2017 1:49 am

Re: Running a .exe from M Codes

Post by TomKerekes » Mon Jul 25, 2022 8:34 pm

Did you use the start command to start the App as a separate process?

Is the pathtofile correct? Looks like you have double slashes?

post your batch file.
Regards,

Tom Kerekes
Dynomotion, Inc.

PBandJacob
Posts: 21
Joined: Thu Sep 02, 2021 6:02 pm

Re: Running a .exe from M Codes

Post by PBandJacob » Mon Jul 25, 2022 9:00 pm

So the .bat file is already on the Desktop so that ends up being the path it opens to initially so I'm able to call the .exe which is also on the Desktop

I made the .exe files in Python and used pyinstaller to turn the scripts into .exe files.

The contents of the .bat file are just: testhandler.exe

In Prog Start I selected "Execute PC" from the drop down menu.
I had to convert the attachments to .txt files because the .py extension wasn't supported.
Attachments
testhandler.txt
(984 Bytes) Downloaded 30 times
test.txt
(231 Bytes) Downloaded 26 times

User avatar
TomKerekes
Posts: 2529
Joined: Mon Dec 04, 2017 1:49 am

Re: Running a .exe from M Codes

Post by TomKerekes » Tue Jul 26, 2022 12:29 am

You didn't use the 'start' command in the batch file to spawn your App as a separate process.

But anyway your App doesn't seem to display anything on the screen so would probably be simpler running hidden (no batch file).

Your App returns with exit code -1. KMotionCNC considers return code of 0 for success and non-zero for failure. Try changing your App to 0 exit codes.

I think a batch file will return non-success if it calls an App that return non-success. btw using the 'start' command is different as the batch file will return to KMotionCNC before the App begins execution in a new process.

HTH
Regards,

Tom Kerekes
Dynomotion, Inc.

User avatar
TomKerekes
Posts: 2529
Joined: Mon Dec 04, 2017 1:49 am

Re: Running a .exe from M Codes

Post by TomKerekes » Tue Jul 26, 2022 12:43 am

Also shouldn't this be a 1 not 2?

Code: Select all

if tasks.count("testhandler.exe") > 2
Regards,

Tom Kerekes
Dynomotion, Inc.

PBandJacob
Posts: 21
Joined: Thu Sep 02, 2021 6:02 pm

Re: Running a .exe from M Codes

Post by PBandJacob » Tue Jul 26, 2022 1:13 pm

For some reason two processes were generated every time it ran. I'm looking into pyinstaller to see if there's some configuration I can use to have it only generate one process in Windows tasklist

PBandJacob
Posts: 21
Joined: Thu Sep 02, 2021 6:02 pm

Re: Running a .exe from M Codes

Post by PBandJacob » Tue Jul 26, 2022 1:33 pm

Thank you so much Tom!
I ended up doing this for the .bat file which did exactly what I wanted:
start /d C:\pathtodesktop\ /b testhandler.exe

On the Python end, using exit code 0 also worked and I was able to just run the .exe file without a .bat and had no error messages pop up.
Last edited by PBandJacob on Tue Jul 26, 2022 1:53 pm, edited 2 times in total.

PBandJacob
Posts: 21
Joined: Thu Sep 02, 2021 6:02 pm

Re: Running a .exe from M Codes

Post by PBandJacob » Tue Jul 26, 2022 1:41 pm

On an unrelated note, sorry about not responding on some previous posts I've made. There was some hiccup with my network where I could make initial posts but not reply to them. Thankfully it seems like that is fixed now.

Post Reply