Python ctypes回调函数未获取任何数据

时间:2019-06-04 07:14:40

标签: python c++ ctypes

我正在使用Python开发C ++ SDK。其中一种API看起来像这样。我指的是SO post

我的目标是注册一个回调函数并通过一个回调函数接收警报消息,但是我没有任何数据。我怀疑我处理回调函数的方式是错误的。

我能够从此True获得一个setDVRMsgCallback,这意味着调用API是成功的,但是我没有从msgCallback获得任何价值

代码

class NET_DVR_ALARMER(Structure):

    _fields_ = [
        ("byUserIDValid", c_byte),
        ("bySerialValid", c_byte),
        ("byVersionValid", c_byte),
        ("byDeviceNameValid", c_byte)
    ]

def msgCallback(lCommand, pAlarmer, pAlarmInfo, dwBufLen, pUser):
    print("callback")
    print(lCommand)
    print(pAlarmer)
    print(pAlarmInfo)
    return True

def setDVRMsgCallback(sdk, callback):
    sdk.NET_DVR_SetDVRMessageCallBack_V31.restype = c_bool
    result = sdk.NET_DVR_SetDVRMessageCallBack_V31(callback, None)
    print(result)

callback_t = CFUNCTYPE(c_bool, c_long, POINTER(NET_DVR_ALARMER), c_void_p, c_ulong, c_void_p)
callback = callback_t(msgCallback)

setDVRMsgCallback(sdk, callback)

API

BOOL NET_DVR_SetDVRMessageCallBack_V31(
  MSGCallBack_V31    fMessageCallBack,
  void               *pUser
);

参数

fMessageCallBack
  [in] Callback function
pUser
  [in] User data

回调函数参数

lCommand
  [out]Uploaded message type, and the types vary with alarm information. You can distinguish the alarm information according to the types,see details in the "Remarks" table.
pAlarmer
  [out] Alarm device information, including device series No., IP address, user login ID handle.
pAlarmInfo
   [out] Alarm information, judge the pAlarm structure via the lCommand value, see details in the "Remark" table.
dwBufLen
  [out] Alarm information buffer size
pUser
  [out] User data

您可以下载其SDK here

1 个答案:

答案 0 :(得分:0)

我已参考上面随附的SDK文档。通过NET_DVR_SetupAlarmChan_V41注册回调函数后,您似乎需要调用NET_DVR_SetDVRMessageCallBack_V31 API。

您的代码对我来说似乎很好。

由于API的异步特性,您需要确保程序始终处于侦听/等待模式,以便从回调函数获取结果。