从Python Shell初学者问题中检索值

发布于 2025-02-02 04:35:53 字数 812 浏览 3 评论 0原文

我可以使用此 python file 来自图书馆的文件通过BACNET协议读取温度传感器值的请求,通过从终端运行此命令:

echo'read读取12345:2模拟函数:2 PresentValue'| py -3.9 readproperty.py

,我可以在控制台中看到67.029999114990234的值。

如果这个问题看起来真的很愚蠢且入门级,我深表歉意,但是我可以将此脚本称为并为传感器阅读分配一个值吗?任何技巧都非常感谢。

例如,如果我运行此操作:

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.0299991149990234的传感器读数的方法。我认为引擎盖下发生的事情是BACNET库带来了一种使用STD In/Out/Flush的外壳脚本。

I can use this python file from a library to make a read request of a temperature sensor value via BACnet protocol by running this command from terminal:

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

And I can see in the console the value of 67.02999114990234 is returned as expected.

I apologize if this question seems real silly and entry level, but could I ever call this script and assign a value to the sensor reading? Any tips greatly appreciated.

for example if I run this:

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)

It will just print 0 but hoping to figure out a way to print the sensor reading of 67.02999114990234. I think what is happening under the hood is the BACnet library brings up some sort of shell scripting that is using std in/out/flush.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

末骤雨初歇 2025-02-09 04:35:53

OS.System不会从Stdout返回输出,而是执行程序/代码的退出代码。

请参阅 docs 有关更多信息:

在UNIX上,返回值是指定为WAIT()的格式编码的过程的退出状态。
在Windows上,返回值是在运行命令后由System Shell返回的。

要将输出从Stdout进入程序中,您必须使用子系统模块。外面有很多有关如何使用子系统的教程,但这是一种简单的方法:

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)

os.system does not return the output from stdout, but the exit code of the executed program/code.

See the docs for more information:

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.

For getting the output from stdout into your program, you have to use the subsystem module. There are plenty of tutorials outside on how to use subsystem, but this is an easy way:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文