Ich versuche, eine Python-Funktion von meinem C-Skript aufzurufen. Ich habe Erfolg, eine doppelte Variable von der Python-Funktion zurückzugeben und sie in meinem C-Skript zu drucken. Das Problem, das ich jetzt habe, ist, dass ich mehrere Variablen (Tupel) von Python zurückgeben muss. Wie kann ich das schaffen? Mein Code mit einer einzigen Variablen Rückkehr ist wie folgt:Aufruf der Funktion von Python mit Tuple-Rückgabe in c
#include <python2.7/Python.h>
int main(){
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
int i;
double read;
Py_Initialize();
PyObject* sysPath = PySys_GetObject((char*)"path");
PyList_Append(sysPath, PyString_FromString("."));
pName = PyString_FromString("eadata"); //Primer posible error
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if(pModule != NULL){
pFunc = PyObject_GetAttrString(pModule,"lectura");
if(pFunc && PyCallable_Check(pFunc)){
pValue = PyObject_CallObject(pFunc,NULL);
if(pValue != NULL){
printf("Result of call: %lf \n", PyFloat_AsDouble(pValue));
Py_DECREF(pValue);
}
else{
Py_DECREF(pFunc);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr,"Call failed\n");
return 1;
}
}
}
else{
PyErr_Print();
fprintf(stderr, "Error al cargar eadata.py \n");
}
}
Mein Python-Skript ist:
def lectura():
device = '/dev/ttyUSB0'
baudrate = 115200
mt=mtdevice1.MTDevice(device, baudrate)
ax=mtdevice1.acx
ay=mtdevice1.acy
az=mtdevice1.acz
wx=mtdevice1.wx
wy=mtdevice1.wy
wz=mtdevice1.wz
mx=mtdevice1.mx
my=mtdevice1.my
mz=mtdevice1.mz
roll = mtdevice1.roll
pitch = mtdevice1.pitch
yaw = mtdevice1.yaw
pc = mtdevice1.pc
return ax
Dies funktioniert, aber ich mag andere Variablen zur gleichen Zeit zurückzukehren wie ay, az, und so weiter. Ich verwende Embedded Python zum ersten Mal und weiß wirklich nicht, wie ich das machen soll.
Vielleicht wird _ [diese Diskussion] (http://stackoverflow.com/a/13942236/645128) _ einen Hinweis geben? – ryyker
Oder _ [dieser] (http://stackoverflow.com/questions/29929920/how-to-pass-a-list-array-of-structs-from-python-to-c) _ – ryyker