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?
ctypesmodule (look at the documentation from the standard library).