我正在读出机器的运行时信息。我的问题是,在我成功连接到机器之后,在我的命令之前,子程序实例被关闭,以执行读取运行时数据。
我已经在一行中写了两个命令:
constr = [program, "connect", "-I", "192.168.0.75", "runinfo", "T", "R"]
导致只有连接在shell关闭之前建立。
我用一个字符串中的代码尝试了它:
constr = [program, "connect -I "192.168.0.75" "runinfo" "T" "R"]
导致shell保持开放状态,但根本没有连接或做任何事情。
我也尝试用分隔符分隔两个命令:
| & ;
没有任何成功。
我的代码现在:
import subprocess
#Path to the programm
program = "../../../../Program Files (x86)/HEIDENHAIN/TNCremo/TNCcmdPlus.exe"
#command to connect
constr = [program, "connect", "-I", "192.168.0.75"]
# command to read the runtime
getstr = ["runinfo", "T", "R"]
# execute subprocess
subprocess.Popen(constr, shell=False)
如你所见,我现在没有使用第二个命令。因为到目前为止我还没有成功。
我希望得到相同的结果,比如使用TNCcmdPlus.exe shell时。
Local:C:\Users\****\Desktop> connect -I 192.168.0.75
Serial cable (P), Ethernet (I) or Local (L): —l
Serial port (e.g. COM2): 192.168.0.75
Connecting with 192.168.0.75...‚ 0 Band
Connection established with iTNC530. NC Software 340422 14 SP5
TNC:\> runinfo I R
Info type: T
Nc up eine (N). Machine up time (M), Machine running tine (R) or
PLC operation times (0..1) : R
Machine running time: 49404289 sec (571 days 19 h 24 min 40 sec)
TNC:\>
如果我把事情做对了,你需要[Python 3]: Popen.communicate(input=None, timeout=None)(或Popen.wait
),以便在产生另一个进程之前等待进程终止,例如:
# ...
# execute subprocess
p = subprocess.Popen(constr)
p.communicate()
p = subprocess.Popen(getstr)
p.communicate()
作为注释,您还可以使用其中一个便利功能(例如subprocess.check_output
)。