【问题标题】:Python ctypes callback function not getting any dataPython ctypes回调函数没有得到任何数据
【发布时间】:2019-06-04 07:14:40
【问题描述】:

我正在使用 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

【问题讨论】:

    标签: python c++ ctypes


    【解决方案1】:

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

    我觉得你的代码很好。

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

    【讨论】:

      猜你喜欢
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-02
      相关资源
      最近更新 更多