带poin的回调函数

2024-05-15 20:49:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是C中的一个库,它以回调作为参数,请看这个超级简单的示例:

#include <stdio.h>
#include <stdlib.h>
#include "axgmb.h"

    void callback(interrupt_handler pointer) {
        printf("*****[APP]%lx , %lx\n", pointer.data, pointer.value); 
    }

    int main(void)
        {
            AXGMB_DIO_Open(); 
            AXGMB_DIO_interruptHandler(callback); 
            sleep(1);
            AXGMB_DIO_Close();
            return ERR_Success;
        };

中断处理程序的结构如下:

typedef struct interrupt_handler{
    unsigned long
    unsigned long
    unsigned long
    unsigned long
}interrupt_handler;

我正在尝试将其与Python2.7结合使用,到目前为止,我编写了以下内容:

mydll = windll.LoadLibrary("AGP2_API.dll")

@c.CFUNCTYPE(None, c.c_char_p)
def callBackTest(a):
    print("Test call back "+str(a))

iRet=mydll.AXGMB_DIO_Open()
mydll.AXGMB_DIO_interruptHandler(callBackTest)
time.sleep(10)
mydll.AXGMB_DIO_Close()

从正确调用回调的意义上讲,它是有效的,但我无法获得所需的正确数据,我只编写了str(a)来查看是否有结果。 我必须使用struct来读取数据,但我不知道,我想出了这样的方法,但不确定,也不确定如何使用它:

class testStruct(Structure):
    _fields_ = [('val1', c_longdouble),
                ('val2', c_longdouble),
                ('val3', c_longdouble),
                ('val4', c_longdouble)]

Tags: includecallbackopenlonghandlerlxpointervoid