How to connect a Python and C program?

I have a python based program that reads serial data from a port connected to an rs232 cable. I want to transfer the data that I get here to a C program that will handle the intensity of the calculations. I checked the network and everything I found is based on Linux.

+3
c python
source share
7 answers

Use the handset and enter

The easiest way to handle this is to simply use popen(3) . The popen function is available in both Python and C and will connect a program of any language to another using a channel.

 >>> import subprocess >>> print args ['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"] >>> p = subprocess.Popen(args) 

Once you have the pipe, you should probably send yaml or json through it, although I never tried to read in C. If this is a really simple stream, just analyze it yourself. If you like XML, I suggest that this is also available.

+4
source share

My suggestion would be the inline function of the instant module, although this only works if you can do everything you need in one c function. You simply pass the function c to it and compile the extension c at runtime.

  from instant import inline sieve_code = """ PyObject* prime_list(int max) { PyObject *list = PyList_New(0); int *numbers, *end, *n; numbers = (int *) calloc(sizeof(int), max); end = numbers + max; numbers[2] = 2; for (int i = 3; i < max; i += 2) { numbers[i] = i; } for (int i = 3; i < sqrt(max); i++) { if (numbers[i] != 0) { for (int j = i + i; j < max; j += i) { numbers[j] = 0; } } } for (n = numbers; n < end; n++) { if (*n != 0) { PyList_Append(list, PyInt_FromLong(*n)); } } free(numbers); return list; } """ sieve = inline(sieve_code) 
+7
source share

There are several ways to do this.

  • The easiest and easiest way is to use the Python C API and write a wrapper for your C library, which can be called from Python. This links your module with CPython.

  • The second way is to use ctypes , which is FFI for Python, which allows you to load and call functions in C libraries directly. Theoretically, this should work on Python realities.

  • The third way is to use Pyrex or the next-generation version of Cython , which allows you to annotate your Python code with type information that the compiler can convert to compiled code. It can also be used to write wrappers. AFAIK, tied to CPython.

  • Another way is to use SWIG , which is a tool that generates glue code that helps you wrap C libraries for use with Python. This is basically the first approach with an auxiliary tool.

  • Another way is to use the Boost Python API , which is an object-oriented wrapper over the raw Python C API.

All of the above allows you to do your job in the same process.

If this is not a limitation, such as Digital Ross , you can just spawn a subprocess and pass arguments (either as a command line or through standard input) and you have an external process for you.

+6
source share

How many bits per second do you get through this RS-232 cable? Do you experience results that show that Python won't make crispy bits fast enough? If the C program has yet to be written, consider writing the intense computing side of things in Python, with a slight drop in Cython in case Python is not fast enough.

+2
source share

Indeed, this question is not particularly relevant to C ++. Having said that, you can try SWIG - it is multi-platform and allows functional calls from Python to C / C ++.

+1
source share

I would use a standard form of IPC, such as a socket.

A good start would be the Bay Guide.

Also, do not put the question in C ++ unless you specifically use c. c and c ++ are different languages.

0
source share

I would use ctypes: http://python.net/crew/theller/ctypes/tutorial.html
It allows you to call c (and C ++) code from python.

0
source share

All Articles