Passing argument from user defined m code to python script
22 Aug 2019 18:54 #142875
by mmt
Passing argument from user defined m code to python script was created 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.
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
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.
- Todd Zuercher
- Offline
- Platinum Member
Less
More
- Posts: 5007
- Thank you received: 1441
22 Aug 2019 20:30 #142878
by Todd Zuercher
Replied by Todd Zuercher on topic Passing argument from user defined m code to python script
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
www.tutorialspoint.com/python/python_command_line_arguments.htm
Please Log in or Create an account to join the conversation.
22 Aug 2019 22:02 #142880
by andypugh
Replied by andypugh on topic Passing argument from user defined m code to python script
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)
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.
- billskeen63
- Offline
- New Member
Less
More
- Posts: 3
- Thank you received: 0
10 May 2021 14:11 #208480
by billskeen63
Replied by billskeen63 on topic Passing argument from user defined m code to python script
Did you ever get this to work?
Bill
Bill
Please Log in or Create an account to join the conversation.
10 May 2021 17:22 - 10 May 2021 17:24 #208490
by andypugh
Replied by andypugh on topic Passing argument from user defined m code to python script
Yes, I just tried it.
Save this file in the same folder as the INI of your config as "M110"
Make the file executable
check that is works at the command line first:
If that works, then start LinuxCNC and in the MDI window run:
And you should see the P and Q numbers.
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