Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

retrieve value from python shell beginner question

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading