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

Python ctypes callback function not getting any data

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

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

我能够从这个 setDVRMsgCallback 得到一个 True 这意味着调用 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

你可以下载他们的SDKhere

我已经参考了上面附带的SDK文档。看来您需要在通过 NET_DVR_SetDVRMessageCallBack_V31.

注册回调函数后调用 NET_DVR_SetupAlarmChan_V41 API

我觉得你的代码没问题。

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