I have a C++ program that I want to run continuously that gathers sensor data on a Raspberry Pi. I'm trying to use Python's subprocess feature to launch the C++ program, and then occasionally read output from the C++ program. However, I am having trouble reading in data from the C++ program to Python. The Python program always returns nothing. What am I doing wrong? Is there a more reliable way to read in data to a Python program from a continously running C++ program?
Here's the C++ program:
#include <stdio.h>
#include <signal.h>
#include <pigpio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <math.h>
#include <iostream>
int main(int argc, char *argv[])
{
std::cout<< "Test of disturbance input\n";
// Calculations are performed once every 0.25 seconds.
time_sleep(0.25);
}
And here's the Python program
import os
import signal
from subprocess import Popen, PIPE, STDOUT
process = Popen('./testSubprocess', stdin=PIPE,stdout = PIPE)
while True:
output = process.stdout.readline()
print(output)