I'm running a java command using a jar file that streams live sensor data and it sends that data to /dev/ttyUSB3 every 15s or so. I'm trying to determine the best way to handle this. I'm by no means an expert at python but I can poke around at it comfortably enough. Unfortunately, this means I can essentially do trial and error to make it work but I might not be thinking about the problem/approaching it the right way.
I have a few problems at this point:
- How should I approach this? All signs online seem to point to subprocess being the way to go
- How do I setup the subprocess for this approach?
- I want to ensure that the java code that is handling the jar is closed when the python command is closed
If I run the java jar from bash, I get an output that looks like the following:
$ java -jar myjavajar.jar -port /dev/ttyUSB3 -baud 115200 -flow hardware
Output:
Opened serial port /dev/ttyUSB3 at 115200
SensorData [1,2,3]
SensorData [4,5,6]
SensorData [7,8,9]
I am now trying to put it into the subprocess and have tried all kinds of variations (subprocess.Popen, subprocess.run, subprocess.check_output, I've tried shell=true, stdout=subprocess.PIPE and several other different approaches, I've tried all different scripts that I've been googling, etc). Below is one of my recent attempts:
import subprocess
process = subprocess.run(['java', '-jar', 'myjavajar.jar', '-port', '/dev/ttyUSB3', '-baud', '115200', '-flow', 'hardware'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(process)
I assume it's running because it never exits out (which makes sense as it's likely continually receiving data). Unfortunately, I need to be able to have it receive data and process/print it as data comes in.
This means either:
- I have the subprocess configured incorrectly
- I am taking the wrong approach altogether and need to handle it differently than subprocess
- This isn't doable and I need to find another approach/solution to this issue
Any help pointing me in the right direction would be greatly appreciated!