0

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.

bbartling
  • 3,288
  • 9
  • 43
  • 88
  • Does this answer your question https://stackoverflow.com/questions/4760215/running-shell-command-and-capturing-the-output? – Edward Ji May 28 '22 at 14:40

1 Answers1

1

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)
  • Thanks for the answer, am learning a bit about this process. So I tested this on Ubuntu (changed my testing script to include `print("sensor reading is: ",sensor_reading)` which ultimately prints `sensor reading is: b'66.12999725341797\n'`. Would I just need to format this from I think a string into a float? Much appreciation here.... – bbartling May 28 '22 at 14:53
  • Well this works, ha. `print("sensor reading is: ",float(sensor_reading))` – bbartling May 28 '22 at 14:55