I am working on an Embedded application which uses some precompiled binaries from the CANBOAT repository.
There are two binaries available for usage:
actisense-serialanalyzer
In a shell, one requires to execute actisense -r /dev/ttyUSB0 | analyzer -json to obtain information from a device connected to the USB port. The above mentioned command dumps JSON information to STDOUT.
Sample output:
{"timestamp":"2018-08-30T16:27:23.629Z","prio":2,"src":3,"dst":255,"pgn":128259,"description":"Speed","fields":{"SID":106,"Speed Water Referenced Type":"Paddle wheel"}}
{"timestamp":"2018-08-30T16:27:23.629Z","prio":2,"src":6,"dst":255,"pgn":128259,"description":"Speed","fields":{"SID":106,"Speed Water Referenced Type":"Paddle wheel"}}
The above mentioned values keep being displayed on the STDOUT.
I wish to use the above mentioned shell commands in a python script to obtain the JSON values, to parse them and save them to a database.
Initially I want to start out with subprocess.check_output.
I tried:
import subprocess
if __name_ == "__main__":
while True:
value = subprocess.check_output(['actisense-serial -r /ttyUSB0',
'|',
'analyzer -json'],
shell=True)
print(value)
But there is no output available. I am not sure how do I route the output of the STDOUT to the check_output.
How do I achieve this, where the continuous JSON information coming from the shell commands can be parsed and used further in the application?
shell=Truedoesn't work the way you expect here -- only the first list element is parsed as a shell script.['''actisense-serial -r "$1" | analyzer -json''', '_', "/ttyUSB0"]is an example of an argument list that would work withshell=True(the first list entry is the shell script to run, the second is the$0that script is run with, the third is the$1it's run with).