Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Talking to a 'C' program

35 views
Skip to first unread message

John Pote

unread,
Nov 8, 2013, 9:23:17 AM11/8/13
to pytho...@python.org
Hi all,

I have the task of testing some embedded 'C' code for a small micro-controller. Thought it would be a good idea to test it on the PC first to make sure the algorithm is correct then perhaps test it on the controller via RS232 and an appropriate wrapper round the 'C' functions.

On the PC I can use Python's unit test library module and logging to create a nice and easy to use environment (I like Python). So my question is how to communicate from Python to the C module on the PC. What I'd like is simplicity and ease of setting up. All I can think of myself is to use sockets.

Any ideas on how to do this would be gratefully appreciated.

Also as I don't have any microsoft offerings of a C compiler any suggestions as to a suitable C compiler for a PC appreciated as well. llvm? mingw? gcc?

Thanks a lot everyone,
John

Oscar Benjamin

unread,
Nov 8, 2013, 9:37:32 AM11/8/13
to John Pote, Python List
On 8 November 2013 14:23, John Pote <john...@o2.co.uk> wrote:
> Hi all,
>
> I have the task of testing some embedded 'C' code for a small micro-controller. Thought it would be a good idea to test it on the PC first to make sure the algorithm is correct then perhaps test it on the controller via RS232 and an appropriate wrapper round the 'C' functions.
>
> On the PC I can use Python's unit test library module and logging to create a nice and easy to use environment (I like Python). So my question is how to communicate from Python to the C module on the PC. What I'd like is simplicity and ease of setting up. All I can think of myself is to use sockets.
>
> Any ideas on how to do this would be gratefully appreciated.

Have a look at Cython:
http://cython.org/

This enables you to call C code from Python code within the same process.

> Also as I don't have any microsoft offerings of a C compiler any suggestions as to a suitable C compiler for a PC appreciated as well. llvm? mingw? gcc?

I assume that by "PC" you mean Windows in which case mingw is a fine
choice and is officially supported by Cython.


Oscar

Mark Barton

unread,
Nov 8, 2013, 9:39:21 AM11/8/13
to pytho...@python.org
On 11/8/13 9:23 AM, John Pote wrote:
> Hi all,
>
> I have the task of testing some embedded 'C' code for a small micro-controller. Thought it would be a good idea to test it on the PC first to make sure the algorithm is correct then perhaps test it on the controller via RS232 and an appropriate wrapper round the 'C' functions.
>
> On the PC I can use Python's unit test library module and logging to create a nice and easy to use environment (I like Python). So my question is how to communicate from Python to the C module on the PC. What I'd like is simplicity and ease of setting up. All I can think of myself is to use sockets.
>
> Any ideas on how to do this would be gratefully appreciated.
>
> Also as I don't have any microsoft offerings of a C compiler any suggestions as to a suitable C compiler for a PC appreciated as well. llvm? mingw? gcc?
>
> Thanks a lot everyone,
> John
Hi John,
I know this is not answering you question, but
have you considered using one of the many "C" unit
test programs that are available for testing the
embedded code? I would hazard to guess that it
would be much easier to use something that is
specifically C friendly that trying to adapt
Python unit test. You can however still use Python
as a wrapper or even a slick GUI for invoking the
C unit test using a subprocess and getting the
results. I have used CUnit in the past and I know
that it has several different methods for
returning the results. I'm sure other C unit test
programs are similar.

Mark


Mark Lawrence

unread,
Nov 8, 2013, 9:52:02 AM11/8/13
to pytho...@python.org
On 08/11/2013 14:23, John Pote wrote:
> Hi all,
>
> Also as I don't have any microsoft offerings of a C compiler any suggestions as to a suitable C compiler for a PC appreciated as well. llvm? mingw? gcc?
>
> Thanks a lot everyone,
> John
>

Visual Express C++ is fine, provided that its version matches your
version of Python. Failing that mingw is gcc for windows, then perhaps
cygwin. I know nothing about llvm.

--
Python is the second best programming language in the world.
But the best has yet to be invented. Christian Tismer

Mark Lawrence

Grant Edwards

unread,
Nov 8, 2013, 10:00:31 AM11/8/13
to
On 2013-11-08, John Pote <john...@o2.co.uk> wrote:
> Hi all,
>
> I have the task of testing some embedded 'C' code for a small
> micro-controller. Thought it would be a good idea to test it on the
> PC first to make sure the algorithm is correct then perhaps test it
> on the controller via RS232 and an appropriate wrapper round the 'C'
> functions.
>
> On the PC I can use Python's unit test library module and logging to
> create a nice and easy to use environment (I like Python). So my
> question is how to communicate from Python to the C module on the PC.
> What I'd like is simplicity and ease of setting up. All I can think
> of myself is to use sockets.

Sockets are nice and simple. Depending on what you're doing,
stdin/stdout may be even simpler.

For the RS232 part of the problem, don't forget about pyserial:

http://pyserial.sourceforge.net/pyserial.html

> Any ideas on how to do this would be gratefully appreciated.
>
> Also as I don't have any microsoft offerings of a C compiler any
> suggestions as to a suitable C compiler for a PC appreciated as well.
> llvm? mingw? gcc?

I've occasionaly used mingw (which _is_ gcc), and it worked well.
Cygwin (also gcc) works well, but it's a bit more involved.

I do all my embedded development on a Linux host. I find Linux to be
far more suitable for the task -- the entire Unix system basically
evolved as a software development platform. I've yet to figure out
what MS-Windows is suited for other than lining Bill Gates' pockets.

Before Linux, I used Solaris/SunOS, and before that I used Unix V7.
Everytime I've been involved in a Microsoft-hosted embedded
development project, I just end up walking a way afterwards shaking my
head in puzzlement.

--
Grant Edwards grant.b.edwards Yow! I'm having a
at tax-deductible experience!
gmail.com I need an energy crunch!!

Grant Edwards

unread,
Nov 8, 2013, 10:04:03 AM11/8/13
to
On 2013-11-08, Oscar Benjamin <oscar.j....@gmail.com> wrote:
> On 8 November 2013 14:23, John Pote <john...@o2.co.uk> wrote:
>> Hi all,
>>
>> I have the task of testing some embedded 'C' code for a small
>> micro-controller. Thought it would be a good idea to test it on the
>> PC first to make sure the algorithm is correct then perhaps test it
>> on the controller via RS232 and an appropriate wrapper round the 'C'
>> functions.
>>
>> On the PC I can use Python's unit test library module and logging to
>> create a nice and easy to use environment (I like Python). So my
>> question is how to communicate from Python to the C module on the PC.
>> What I'd like is simplicity and ease of setting up. All I can think
>> of myself is to use sockets.
>>
>> Any ideas on how to do this would be gratefully appreciated.
>
> Have a look at Cython:
> http://cython.org/
>
> This enables you to call C code from Python code within the same
> process.

You can also use c-types for that

http://docs.python.org/2/library/ctypes.html#module-ctypes

I've used Python/ctypes to exercise libraries written in C, but I've
never done it under Windows. However, others have:

http://hakantiftikci.wordpress.com/2009/11/14/generating-dll-using-mingw-and-using-them-in-python-via-ctypes/

--
Grant Edwards grant.b.edwards Yow! Give them RADAR-GUIDED
at SKEE-BALL LANES and
gmail.com VELVEETA BURRITOS!!

John Pote

unread,
Nov 8, 2013, 11:08:54 AM11/8/13
to pytho...@python.org
Thanks everyone for the advice, some good ideas to keep me busy. Will try and look at over weekend/next week as tied up the rest of today.

I've used pyserial several times  - many thanks to Chris Liechti for that module

Hmmmmm must be loosing it, forgot about stdin/out!

I've also used CUnit before and it's nice an easy and small. Problem is I've only 500 bytes code space left on the micro-controller so by the time CUnit gone in with the various tests I'm gonna run of room. I have to keep the RS232 driver in as well as it's the only way to talk to the controller.

Python + pyserial enables me to run the tests with the C compiled and run on the PC as well as compiled to run on the micro-controller. Python is a great environment for doing this sort of thing and, as has been mentioned, a GUI can be added easily (time permitting).

Thanks again all,
John
 
0 new messages