Skip to content
Advertisement

Pipe between a C and a Python program

I am currently trying to write a C program that interacts with hardware, I have chosen so since the manufacturer of the hardware supplies with a C SDK for this hardware. However, I want this C program to output the data it receives from the hardware (a sensor for example) to a pipe so that I can write a Python program to pick up the data and parse it and do some analytical things with it. I am running the programs on a Raspberry Pi (Linux). How can I create a pipe between these two different programs (Python and C)? Can I do this by having a pipe location?

Advertisement

Answer

Since you only want to pipe data in one direction, you don’t need to create your pipe in code.

The easiest way is to let the OS pipe the data for you by writing it to stdout in the C program and reading it from stdin in Python.

You would run the programs like this:

mycprog | python myscript.py

In the C code, you would use something like:

#include <stdio.h>

unsigned char buffer[BUFFER_SIZE];
/* read from sensor to buffer */
fwrite(sensor_buffer, sizeof(usigned char), BUFFER_SIZE,  stdout);

On the Python side, the code would look something like this:

import sys

sensor_bytes = sys.stdin.read(BUFFER_SIZE)

You could also use popen to launch the Python script directly from C code, but this would make your solution less flexible and if you ever want to send the data to another script or program you would have to change the C code.

Finally, if you do not want to rely on the shell, or you want to run your two programs in different shells, you can create a named pipe using mkfifo.

Here is a detailed example.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement