0

After reading a lot from this wonderful web, I need some help.

I have this function (in a dll):

kvStatus kvSetNotifyCallback  ( const int  hnd, kvCallback_t  callback,  void *  context,  
  unsigned int  notifyFlags);

How to call this function from Python?

I tried:

kvSetNotifyCallback(c_int(hnd1),c_void_p(self.can_rx()),None, c_int(canNOTIFY_RX))

def can_rx(self):

        print("OK")

The function can_rx does execute only once. Any suggestions? Thank you very much. MM

Blender
  • 289,723
  • 53
  • 439
  • 496

1 Answers1

0

first thing:

The argtypes of ctypes WINFUCNTYPE or CFUNCTYPE should be a c_void_p

The argtypes of fucntion which you are defining should be py_object instead of a c_void_p so that you can pass objects of structures and arrays as well.

then in the callback fucntion cast the void_p data as ct.cast(userdata,ct.py_object).value

second thing:

code running only once: put a return value at bottom of the code. return 1 or return TRUE

example:

CALLBACKFUNC = WINFUNCTYPE("returnvalue",c_int,c_void_p)

kvSetNotifyCallback.argtypes = [c_int,CALLBACKFUNC,py_object,c_uint]

then in the fucntion:

def can_rx(arg1,arg2):
     arg3 = cast(arg2,py_object).value #yields object value
     print arg1
     print arg3
     return 1    #for continuing execution

The CALLBACK FUNCTION can_rx should be called at CALLBACKFUNC(can_rx), the data to be passed along with arg1 should be passed at py_object position.

so you should call your callback as:

kvSetNotifyCallback(somevalue,CALLBACKFUC(can_rx),arg2,canNOTIFY_RX)

arg2 is the value to be passed to the callback func can_rx

can_rx can be defined as a global function also.