Reputation: 71
I made use of a server file which was written in Python to build connection between my Raspberry Pi and my iPhone. And i wrote a simple C program that help translate morse-code. I want to call the translate()
function in the C program from the Python server program.
I found a tutorial online and followed its instructions to write my C program and edit the netio_server.py
file
In my C program morseCodeTrans.c
it's like
#include <Python.h>
#include <stdio.h>
static PyObject* py_translate(PyObject* self, PyObject* args)
{
char *letter;
PyArg_ParseTuple(args, "s", &letter);
if(strcmp(letter, ".-") == 0)
return Py_BuildValue("c", 'A');
else if(strcmp(letter, "-...") == 0)
return Py_BuildValue("c", 'B');
...
}
static PyMethodDef morseCodeTrans_methods[] = {
{"translate", py_translate, METH_VARARGS},
{NULL, NULL}
};
void initmorseCodeTrans()
{
(void)Py_InitModule("morseCodeTrans", morseCodeTrans_methods);
}
And in the server file netio_server.py it's like:
# other imports
import morseCodeTrans
...
tempLetter = ''
if line == 'short':
tempLetter += '.'
elif line == 'long':
tempLetter += '-'
elif line == 'shortPause':
l = morseCodeTrans.translate(tempLetter)
print "The letter is", l
Above is the only place that I would call the C translate()
function
Then I tried to compile morseCodeTrans.c
file like this:
gcc -shared -I/usr/include/python2.7/ -lpython2.7 -o myModule.so myModule.c
The compile was successful. But when I ran the Python server program, whenever it reached the line
l = morseCodeTrans.translate(tempLetter)
The server program just terminated without any error message.
I'm very new to Python programming, so I couldn't figure out where the problem is. Any help?
Upvotes: 4
Views: 1306
Reputation: 4530
You just got some minor mixups in the interface. I modified the code as follows to make it work:
#include <Python.h>
#include <stdio.h>
static PyObject* py_translate(PyObject* self, PyObject* args)
{
char *letter;
PyArg_ParseTuple(args, "s", &letter);
if(strcmp(letter, ".-") == 0)
return Py_BuildValue("c", 'A');
else if(strcmp(letter, "-...") == 0)
return Py_BuildValue("c", 'B');
/* ... */
else
Py_RETURN_NONE;
}
static PyMethodDef morseCodeTrans_methods[] = {
{"translate", py_translate, METH_VARARGS, ""},
{0}
};
PyMODINIT_FUNC initmorseCodeTrans(void)
{
Py_InitModule("morseCodeTrans", morseCodeTrans_methods);
}
It's much safer to build with distutils
, since it prevents you from accidentally linking against wrong versions of Python. Use the following setup.py
:
from distutils.core import setup, Extension
module = Extension('morseCodeTrans', sources=['morseCodeTrans.c'])
setup(ext_modules=[module])
Simply use as python setup.py install
.
Edit
Despite the mixups in the interface, it appears your code still works. Then your problem is most likely during linking. As mentioned above, using distutils
should remedy this. If you absolutely want to build manually, use python-config --cflags
, python-config --ldflags
etc. to make sure you link against the correct version of Python.
Upvotes: 1