4

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.

2
  • 1
    Definitely option (2), but why not write the PC code in C or C++? Perhaps if you must, hooking into Python or other languages at a higher level of abstraction with a smaller more application specific interface. Commented Aug 2, 2011 at 21:39
  • @Clifford: I am using Python to drive a multitude of functional tests on the embedded plat form. So adding the library to Python made since to me to avoid adding another layer in my test suite and helping it work cross platform. However, being perfectly honest, I never considered leaving it in C and calling a library from Python. I will have to look into this to see if it is a good fit. Thanks for the input. Commented Aug 2, 2011 at 21:58

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

1 Comment

I concur with all of your reasons to wrap it, I just didn't know the best way to approach it. On a note for the library, we paid for a site license which allows for unlimited products. In addition the emulation will primarily be used to test the embedded targets, not as a standalone product line.
3

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.

1 Comment

This looks quite promising. Thanks for the link!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.