Antivirusvivente
Posts: 130
Joined: Fri Dec 07, 2012 4:01 pm
Location: Bari
Contact: Website

Stuck with C/C++ library and my project is Python

Thu Sep 19, 2013 1:14 pm

Hello everyone,

after a full day of attemps I got my nRF24L01+ completely work between my Arduino and my Raspberry Pi. They exchange messages pretty well, but now there's a big problem.

I'm not a GURU in C/C++ and my house automation project is fully written in Python and it includes a lot of libraries. Someone could say.. easy man, let's rewrite everything in C, but i don't know where to start because I hate this language and the libraries are very very heavy. So.. is there a way to use these libraries written in C/C++

https://github.com/stanleyseow/RF24/

with my project written in python?

I need to exchange messages.. so i appreciate if someone would tell me a solution to send a command from the python main to the library, then the library sends the message back with the answer.
DomoHome is coming! - RaspberryPi ~ Raspbian ~ Android
https://www.facebook.com/pages/Domohome/155791057916884

User avatar
topguy
Posts: 5935
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Stuck with C/C++ library and my project is Python

Thu Sep 19, 2013 1:59 pm

Isn't that what SWIG ( http://www.swig.org/ ) is for ?

I'm no Python expert, just noticed SWIG was used in another Python wrapper module I was checking out the other day.

User avatar
joan
Posts: 14470
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Stuck with C/C++ library and my project is Python

Thu Sep 19, 2013 2:06 pm

All I can suggest is to have a look at the swig package.

sudo apt-get install swig

It automates (or at least simplifies) the creation of C/C++ bindings for languages like Python. Give the library + header file as input and it should create an interfacing Python module.

DrMag
Posts: 63
Joined: Sat Jun 01, 2013 7:59 pm

Re: Stuck with C/C++ library and my project is Python

Thu Sep 19, 2013 2:09 pm

Read up on the ctypes module in the standard library--it will let you use your C/C++ libraries within Python.

An example:

Code: Select all

import ctypes
libc=ctypes.cdll.LoadLibrary("libc.so.6")
message=ctypes.create_string_buffer(b"Hello, World!!")
libc.printf(message)

Antivirusvivente
Posts: 130
Joined: Fri Dec 07, 2012 4:01 pm
Location: Bari
Contact: Website

Re: Stuck with C/C++ library and my project is Python

Thu Sep 19, 2013 2:26 pm

DrMag wrote:Read up on the ctypes module in the standard library--it will let you use your C/C++ libraries within Python.

An example:

Code: Select all

import ctypes
libc=ctypes.cdll.LoadLibrary("libc.so.6")
message=ctypes.create_string_buffer(b"Hello, World!!")
libc.printf(message)
But is it possible to use external libraries?
DomoHome is coming! - RaspberryPi ~ Raspbian ~ Android
https://www.facebook.com/pages/Domohome/155791057916884

DrMag
Posts: 63
Joined: Sat Jun 01, 2013 7:59 pm

Re: Stuck with C/C++ library and my project is Python

Thu Sep 19, 2013 3:16 pm

Of course. Here's a similar example using a custom library:

test.c

Code: Select all

#include <stdio.h>

void greet(void) {
        printf("Here's a message from a shared C library.\n");
}
Compiling to a shared library:

Code: Select all

$ gcc -c -Wall -Werror -fpic test.c
$ gcc -shared -o libtest.so test.o
Now in Python:

Code: Select all

>>> import ctypes
>>> libtest=ctypes.cdll.LoadLibrary("/path/to/library/libtest.so")
>>> output=libtest.greet()
Here's a message from a shared C library.
>>> output
42
The return value of libtest.greet() is forwarded from the return value of printf(). If you built greet() with a return statement, the value in output would be whatever the function returned.

Antivirusvivente
Posts: 130
Joined: Fri Dec 07, 2012 4:01 pm
Location: Bari
Contact: Website

Re: Stuck with C/C++ library and my project is Python

Fri Sep 20, 2013 6:44 pm

Ok guys, i'm trying with ctypes functions.. i've built my own shared library, which is RF24 library.. but it doesn't work

Code: Select all

[email protected] ~ $ sudo python prova.py
Traceback (most recent call last):
  File "prova.py", line 3, in <module>
    testlib = ctypes.cdll.LoadLibrary("/home/pi/gitwork/RF24/librf24-rpi/librf24/rf24lib.so")
  File "/usr/lib/python2.7/ctypes/__init__.py", line 443, in LoadLibrary
    return self._dlltype(name)
  File "/usr/lib/python2.7/ctypes/__init__.py", line 365, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: /home/pi/gitwork/RF24/librf24-rpi/librf24/rf24lib.so: undefined symbol: _ZN4GPIO4openEii
[email protected] ~ $



EDIT: Problem solved, is there a way to call the constructor from the ctype?
DomoHome is coming! - RaspberryPi ~ Raspbian ~ Android
https://www.facebook.com/pages/Domohome/155791057916884

DrMag
Posts: 63
Joined: Sat Jun 01, 2013 7:59 pm

Re: Stuck with C/C++ library and my project is Python

Fri Sep 20, 2013 8:39 pm

Hard to know what happened-- it may depend on how you've compiled it.

A quick look at the repository has a link to a blog that explains how to install the library on your Pi. If you follow all the way through the make install, you can load the library from that directory. If you stop at make, you'll have a file called librf24.so.1.0 that you can put wherever needed.

I downloaded the repository and compiled it to check; after I've done the make I can load the librf24.so into Python without error. However, a little digging tells me that to use C++ libraries, you need to add a wrapper to a C function for each C++ function. (See for example these pages.) This particular library does already have a few c functions (found in compatibility.c), and those functions do work.

Code: Select all

>>> import ctypes
>>> librf24=ctypes.cdll.LoadLibrary("./librf24.so.1.0")
>>> librf24.__msleep(500)
0
>>> %timeit librf24.__msleep(500)
1 loops, best of 3: 500 ms per loop
The last command is part of IPython-- if you've never used it, just ignore it, since it only works in IPython. Basically it confirmed that the called function was doing exactly what it was designed to do-- sleep for 500 ms.

If you pursue the ctypes route, it looks like the next step will be to build the C wrappers. I'm not sure how to do that, but it looks like there are only three .cpp files with a few functions each to worry about.

Antivirusvivente
Posts: 130
Joined: Fri Dec 07, 2012 4:01 pm
Location: Bari
Contact: Website

Re: Stuck with C/C++ library and my project is Python

Fri Sep 20, 2013 9:00 pm

DrMag wrote:Hard to know what happened-- it may depend on how you've compiled it.

A quick look at the repository has a link to a blog that explains how to install the library on your Pi. If you follow all the way through the make install, you can load the library from that directory. If you stop at make, you'll have a file called librf24.so.1.0 that you can put wherever needed.

I downloaded the repository and compiled it to check; after I've done the make I can load the librf24.so into Python without error. However, a little digging tells me that to use C++ libraries, you need to add a wrapper to a C function for each C++ function. (See for example these pages.) This particular library does already have a few c functions (found in compatibility.c), and those functions do work.

Code: Select all

>>> import ctypes
>>> librf24=ctypes.cdll.LoadLibrary("./librf24.so.1.0")
>>> librf24.__msleep(500)
0
>>> %timeit librf24.__msleep(500)
1 loops, best of 3: 500 ms per loop
The last command is part of IPython-- if you've never used it, just ignore it, since it only works in IPython. Basically it confirmed that the called function was doing exactly what it was designed to do-- sleep for 500 ms.

If you pursue the ctypes route, it looks like the next step will be to build the C wrappers. I'm not sure how to do that, but it looks like there are only three .cpp files with a few functions each to worry about.
You didn't read my last UPDATE :lol:

I solved the problem of the libraries, i needed to do a simply make install in the folder to compile the shared library.
Anyway, here's my simple code in Python:

Code: Select all

import ctypes
libtest=ctypes.cdll.LoadLibrary("/usr/local/lib/librf24.so.1.0")
RF24 = libtest.RF24("/dev/spidev0.0",8000000,24)
RF24 should be the constructor, i can't find how to call it. By the way, i've tried calling different functions, but the prompt always says:

Code: Select all

[email protected] ~ $ sudo python prova.py
Traceback (most recent call last):
  File "prova.py", line 4, in <module>
    RF24 = libtest.RF24("/dev/spidev0.0",8000000,24)
  File "/usr/lib/python2.7/ctypes/__init__.py", line 378, in __getattr__
    func = self.__getitem__(name)
  File "/usr/lib/python2.7/ctypes/__init__.py", line 383, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: /usr/local/lib/librf24.so.1.0: undefined symbol: RF24
[email protected] ~ $
And these are the RF24 cpp and h files:
https://github.com/stanleyseow/RF24/blo ... r/RF24.cpp
https://github.com/stanleyseow/RF24/blob/master/RF24.h

Can't figure out how to solve this problem, i'm getting crazy to let these modules work :cry:
DomoHome is coming! - RaspberryPi ~ Raspbian ~ Android
https://www.facebook.com/pages/Domohome/155791057916884

Antivirusvivente
Posts: 130
Joined: Fri Dec 07, 2012 4:01 pm
Location: Bari
Contact: Website

Re: Stuck with C/C++ library and my project is Python

Sun Sep 22, 2013 6:00 pm

No one can help? :cry:
DomoHome is coming! - RaspberryPi ~ Raspbian ~ Android
https://www.facebook.com/pages/Domohome/155791057916884

User avatar
paddyg
Posts: 2422
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Stuck with C/C++ library and my project is Python

Tue Sep 24, 2013 8:25 am

@Anti, RF24 is the cpp class. From @DrMag's comment, "If you pursue the ctypes route, it looks like the next step will be to build the C wrappers" how did that go?
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
paddyg
Posts: 2422
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Stuck with C/C++ library and my project is Python

Tue Sep 24, 2013 12:04 pm

like this maybe?
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”