Adam Lewis
Adam Lewis

Reputation: 7257

Using embedded C library in Python emulation

Short Question
Which would be easier to emulate (in Python) a complex (SAE J1939) communication stack from an existing embedded C library:
1) Full port - meaning manually convert all of the C functions to python modules
2) Wrap the stack in a Python wrapper - meaning call the real c code in Python

Background Information
I have already written small portions of this stack in Python, however they are very non-trival to implement with 100% coverage. Because of this very reason, we have recently purchased an off the shelf SAE J1939 stack for our embedded platforms. To clarify, I know that portions touching the hardware layer will have to be re-created and mapped to the PC's CAN drivers.

I am hoping to find someone here on SO that has or even looked into porting a 5k LOC C library to Python. If there are any C to Python tools that work well that would be helpful for me to look into as well.

Upvotes: 4

Views: 481

Answers (2)

Michael Dillon
Michael Dillon

Reputation: 32392

Definitely wrap it. It might be as easy are running ctypesgen.py and then using it. Check this blog article about using ctypesgen to create a wrapper for libreadline http://wavetossed.blogspot.com/2011/07/asynchronous-gnu-readline.html in order to get access to the full API.

Upvotes: 3

Bruce
Bruce

Reputation: 7132

My advice would be to wrap it.

Reasons for that:

  • if you convert function by function, you'll introduce new bugs (we're just human) and this kind of stuff is pretty hard to test
  • wrapping for python is done easily, using swig or even ctypes to load a dll on the fly, you'll find tons of tutorial
  • if your lib gets updated, you have less impact in the long term.

However, you need to

  • check that the license you purchase allows you to do that
  • know that having same implementation on embedded and PC side, it won't help tracking bugs
  • you might have a bit less portability than a full python implementation (anyway, not much of a point for you as your low layer needs to be rewritten per target)

Upvotes: 3

Related Questions