如何解决C DLLunitybridge.dll异步回调到Python函数
我正在基于Golang(https://git.bug-br.org.br/bga/robomasters1/src/master/app/internal/dji/unity/bridge/wrapper)中的代码重新设计Python中的DJI Robomaster S1 App。在此代码中,他们使用了unitybridge.dll来完成与机器人的大部分通信。与该dll的通信主要通过(异步)回调进行。我使用ctypes访问C中的代码。
我使用了一个C库“ libffcall”,它返回一个指向回调的指针(在Go中也使用)。由于代码非常复杂,因此我将在此处创建一个小示例,该示例应该有效,但无效。通常,这应该创建,初始化网桥并开始连接(通过发送特定事件)。这些似乎都起作用。当在网桥中发生特定事件(由我选择,因为它每隔几秒钟发生一次)时设置回调时,什么也不会发生。 \
alloc_helper.c :使用libffcall进行回调。为了使事情尽可能简单,回调仅打印出一些内容:
#include <stdio.h>
#include "alloc_helper.h"
#include "event_callback_windows.h"
#include <stdlib.h>
callback_t alloc_callback_python(void* data) {
return alloc_callback(&event_callback,data);
}
event_callback_windows.c :应由unitybridge.dll调用,并最终将调用python函数(在此示例中未完成)
void event_callback(void* context,va_alist alist) {
prinf("This should print");
}
example.py :参数中的值与Go中的相同
from ctypes import *
import time
# load the C-code
dll = CDLL("./unitybridge.dll")
alloc_helper = CDLL("./alloc_helper.so")
# create bridge args: str name,bool debuggable
dll.CreateUnityBridge(c_char_p(b"Robomaster",c_int(1))
# init bridge (returns True if successfull)
b = dll.UnityBridgeInitialize()
print("BOOL after init: {}".format(b))
# send event that starts connection
# args: event_code,data,sub_type
dll.UnitySendEvent(c_unint64(100 << 32),None,0)
# set returntype
alloc_helper.alloc_callback_python.restype = POINTER(c_int)
# make callback
cb = alloc_helper.alloc_callback_python(None) # None to make is as simple as possible
# set event callback
# args: event_code (when this events occurs: call cb),callback
dll.UnitySetEventCallback(c_uint(304 << 32),cb)
time.sleep(3) # to give the bridge some time to make callbacks
# uninit and destroy bridge
dll.UnityBridgeUninitialze() # typo is not a mistake -> defined that way in dll
dll.DestroyUnityBridge()
time.sleep(5)
这是我的第一篇文章,请随意询问更多信息,因为我可能忘记了一些信息。 我几乎尝试了所有事情,真的很想解决这个问题。
预先感谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。