Passing argument from user defined m code to python script

More
22 Aug 2019 18:54 #142875 by mmt
I'm sure this has been covered but I can't get it to work.
I am trying to pass a variable from M101 Pxxxx to a python script.
In the python file I am just writing the variable to a file for testing.
I put a pin state change in the M101 just to visually see that it is working with HAL meter.

Attachment not found


Here are my 2 files. The M code file works fine and the python file work fine it just wont pass the parameter.

M101
#!/bin/bash
python /home/kent/linuxcnc/m_codes/current_command.py $1
halcmd setp parport.0.pin-17-out True
exit 0

current_command.py
requested_current = $1
set_current = requested_current
f = open("/home/kent/linuxcnc/write_test.txt", "a")
f.write(set_current)
f.close()

Thanks in advance

Please Log in or Create an account to join the conversation.

More
22 Aug 2019 20:30 #142878 by Todd Zuercher
I think the problem is in your python script, and how you are trying to pass the argument to it.
www.tutorialspoint.com/python/python_command_line_arguments.htm

Please Log in or Create an account to join the conversation.

More
22 Aug 2019 22:02 #142880 by andypugh
Your M101 can be a Python script, no need to go via bash.

Then (as has been said) you get the P and Q with sys.argv.

One reason to use a bash script calling a oython script is if you want the pythin to run in the background, as explained here:
forum.linuxcnc.org/10-advanced-configura...cript?start=10#57807


If you need to pass more than 2 parameters then you can use a Python remap instead. (ie remap an unallocated M or G-code outside the M101 to M199 range, bt that is a little more work)

Please Log in or Create an account to join the conversation.

More
10 May 2021 14:11 #208480 by billskeen63
Did you ever get this to work?

Bill

Please Log in or Create an account to join the conversation.

More
10 May 2021 17:22 - 10 May 2021 17:24 #208490 by andypugh
Yes, I just tried it.

Save this file in the same folder as the INI of your config as "M110"
#! /usr/bin/env python

import sys
import tkMessageBox

print sys.argv
P, Q = float(sys.argv[1]), float(sys.argv[2])
tkMessageBox.showinfo(title="M110", message="P was %d and Q was %d"%(P, Q))

Make the file executable
chmod +x M110

check that is works at the command line first:
./M110 12 44

If that works, then start LinuxCNC and in the MDI window run:
M110 P100 Q231

And you should see the P and Q numbers.
Last edit: 10 May 2021 17:24 by andypugh.
The following user(s) said Thank You: wasausky

Please Log in or Create an account to join the conversation.

Time to create page: 0.068 seconds
Powered by Kunena Forum