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.

Time to create page: 0.076 seconds
Powered by Kunena Forum