从 python shell 初学者问题中检索值

retrieve value from python shell beginner question

我可以使用库中的这个 python file 通过 BACnet 协议发出温度传感器值的读取请求,方法是 运行 从终端执行以下命令:

echo 'read 12345:2 analogInput:2 presentValue' | py -3.9 ReadProperty.py

我可以在控制台中看到 67.02999114990234 的值按​​预期返回。

如果这个问题看起来很愚蠢并且是入门级别,我深表歉意,但我可以调用这个脚本并为传感器读数分配一个值吗?非常感谢任何提示。

例如,如果我 运行 这个:

import subprocess
read = "echo 'read 12345:2 analogInput:2 presentValue' | python ReadProperty.py"

sensor_reading = subprocess.check_output(read, shell=True)

print("sensor reading is: ",sensor_reading)

它只会打印 0,但希望找到一种方法来打印 67.02999114990234 的传感器读数。我认为幕后发生的事情是 BACnet 库带来了某种使用 std in/out/flush.

的 shell 脚本

os.system 不是 return stdout 的输出,而是 program/code.

执行的退出代码

有关详细信息,请参阅 docs

On Unix, the return value is the exit status of the process encoded in the format specified for wait(). On Windows, the return value is that returned by the system shell after running command.

要将 stdout 的输出输入到您的程序中,您必须使用 subsystem 模块。关于如何使用subsystem,外面有很多教程,但这是一个简单的方法:

import subprocess

read = "echo 'read 12345:2 analogInput:2 presentValue' | py -3.9 ReadProperty.py"
sensor_reading = subprocess.check_output(read, shell=True)
print(sensor_reading)