kiloleader
Posts: 10
Joined: Tue Oct 14, 2014 9:02 am

Calling C programs from python script

Wed Oct 22, 2014 2:45 pm

Hi All,

I have been trying to get a Python program to, at a definable point in the code, call a separate C program, run the C program and then continue back in Python.

I am trying to communicate over GPIB using the Pi. I have got one way communication (with the Pi talking), but due to the high-level nature of python I cannot get the program to run fast enough to receive. I plan to jump out of the python script into some C code to deal with the response, pass this to the python program and then continue in python.

As a block process, this looks like:
[send GPIO outputs from python script]
|
[call a function in a C program to record the response]
|
[continue with the Python program, after transferring the response from the C code]

My question is how I get the jump in and out of the C code. I have googled it and can get the code to run on the LX terminal, but any attempt to subprocess.call(["./c_program"]) results in Errno 2 no such directory.

I am running Raspbian.

riklaunim
Posts: 265
Joined: Tue Apr 22, 2014 7:34 pm

Re: Calling C programs from python script

Wed Oct 22, 2014 4:48 pm

Did you checked that using C solves the speed problem?

As for calling C using subprocess.call(["./c_program"]) will look for the executable binary in the same folder as the current one. Other option is to create Python bindings to C functions and import/execute them in Python.

Return to “Beginners”