0

I would like to obtain a temperature and a pressure reading using Python from a barometer that resides on my network. I would then like to assign these values to variables. Currently, a batch file is setup with the following contents:

ECHO OFF
L:\Destination_Folder\httpget.exe -r -S "*SRTC\r" 172.24.48.67:1000 echo. #echo the temperature.
L:\Destination_Folder\httpget.exe -r -S "*SRHm\r" 175.24.48.67:1000 echo. #echo the pressure.
PAUSE

I need a simple way to obtain these values and assign them to variables so that they can be used for various calculations.

Thank-you

  • You could run the programs while piping the output back to a variable that you can then parse through, like [this answer](http://stackoverflow.com/questions/13332268/python-subprocess-command-with-pipe)...But if that executable is just doing a simple HTTP request, you could do that directly in Python almost as easily. – Evil Genius Jun 26 '15 at 17:57

3 Answers3

2

To run the batch file and collection output, you can just use subprocess.check_output if you are running python2.7+ (you could also just call httpget twice directly without the wrapper batch script):

import os
from subprocess import check_output
output = check_output(['/path/to/batch_file.bat'])
# parse the output, depending on what it is exactly, could be something like
temp, pressure = [int(l.strip()) for l in output.split(os.linesep) if l.strip()]

If you want a little more control or are running python<=2.6, you can get the equivalent behavior by doing something like

from subprocess import Popen
process = Popen(['/path/to/batch_file.bat'], stdout=PIPE)
output, _ = process.communicate()
retcode = process.poll()
if not retcode:
    # success! output has what you need
    temp, pressure = [int(l.strip()) for l in output.split(os.linesep) if l.strip()]
    # ...
else:
    # some non-zero exit code... handle exception

However, as Evil Genius points out, it may be better to just make the http requests directly from python. The requests api is great for this:

import requests
response = requests.get('http://172.24.48.67:1000', params=...)
print(response.text)
lemonhead
  • 5,328
  • 1
  • 13
  • 25
0

I found another post which used something similar to this:

import os
fh = os.popen("tempPres.bat")
output = fh.read()
print output
fh.close()

However, I need to parse the data so that I am only saving the second and third lines to variables. How would I go about doing this?

The output of the batch file appears like this:

TEXT...>ECHO OFF 
0022.60
0710.50
Press any key to continue . . . 
  • Change first line of batch file to `@ECHO OFF` and `ECHO OFF` line will disappear from `output` stream. And remove `PAUSE` from batch file and last line will disappear from `output` stream. – Mofi Jun 27 '15 at 20:06
0

I'm not too worried about the "ECHO OFF" or the "Press any key to continue . . ." lines.

Using Spyder to test my Python code, I was able to use:

import os
fh = os.popen("tempPres.bat")
output = fh.read()
data = output.splitlines()
temp = data[2]
pres = data[3]
print (temp)
print (pres)

with output:

0022.50
0710.90

This works fine WITH SPYDER. However, when I try to run the exact same code in my program, the batch file does not seem to produce the correct output. All I can obtain for output is the first line "ECHO OFF". It appears that the httpget.exe executable is not returning the second and third lines.

My program comes preset with the Python math module, numpy, and scipy, and I have installed the os module library. Is there someway that I can test if httpget.exe is being executed properly? Or is there a different module that I may need to import for my external program to run my code properly?

I hope this makes sense.

  • I ended up using subprocess.check_output with the individual lines of code from within the batch file. I apologize for my inexperience with Python. Thanks for the help everyone! – Matthew Strugari Jun 29 '15 at 20:04