linux中动态库的制作和调用的3个实验---第2课 python调用动态库

📅 2026/7/30 20:48:39
linux中动态库的制作和调用的3个实验---第2课 python调用动态库
参考linux中动态库的制作和调用的3个实验—第1课 动态库的制作.csdnlinux中动态库的制作和调用的3个实验—第2课 python调用动态库.csdnlinux中动态库的制作和调用的3个实验—第3课 nodeJs调用动态库.csdnpython 调用动态库很方便,不用安装其他库pl_hw_test_win32.py# 32位动态库,只能32位python调用D:\Program Files\JetBrains\CLion 2023.2\bin\lldb\win\x86\bin\python.exe.\pl_hw_test_win32.pyimporttimefromctypesimport*# -----------------------------------------------------------------------------# 预加载依赖# -----------------------------------------------------------------------------try:CDLL(rD:\Program Files (x86)\dev-cpp\mingw32\bin\libstdc-6.dll)CDLL(rD:\Program Files (x86)\dev-cpp\mingw32\bin\libwinpthread-1.dll)CDLL(rD:\soft\vcpkg\installed\x86-windows\bin\libcurl.dll)CDLL(rD:\soft\vcpkg\installed\x86-windows\bin\libssl-3.dll)print(依赖库预加载成功)exceptExceptionase:print(依赖加载失败:,e)exit()# -----------------------------------------------------------------------------# 加载DLLMinGW编译dll 使用 CDLL CFUNCTYPE重点改动# -----------------------------------------------------------------------------hwCDLL(rD:\workspace\gitee\3\ming_pl_hw\cmake-build-debug\bin\libplHwServer.dll)hw.pl_hw_get_version.restypec_uint32print(DLL Version : 0x%06X%hw.pl_hw_get_version())# -----------------------------------------------------------------------------# 常量# -----------------------------------------------------------------------------PL_HW_EVENT_NONE0PL_HW_EVENT_IRQ1PL_HW_EVENT_READ_RDY2PL_HW_EVENT_WRITE_DONE3PL_HW_EVENT_ERROR4PL_HW_EVENT_LOG5PL_HW_IOCTL_WRITE_RDY1# -----------------------------------------------------------------------------# 数据结构# -----------------------------------------------------------------------------classPlHwEventData(Structure):_fields_[(event,c_int),(code,c_int32),(data,c_void_p),(size,c_uint32),]# MinGW __cdecl 回调CFUNCTYPE不再使用WINFUNCTYPECALLBACKCFUNCTYPE(None,POINTER(PlHwEventData),c_void_p)# -----------------------------------------------------------------------------# 函数声明# -----------------------------------------------------------------------------hw.pl_hw_open.argtypes[POINTER(c_char_p),POINTER(c_uint32),c_uint32]hw.pl_hw_open.restypec_int32 hw.pl_hw_close.argtypes[]hw.pl_hw_close.restypeNonehw.pl_hw_set_buffer.argtypes[c_void_p,c_uint32,c_uint32]hw.pl_hw_set_buffer.restypec_int32 hw.pl_hw_set_callback.argtypes[CALLBACK,c_void_p]hw.pl_hw_set_callback.restypec_int32 hw.pl_hw_ioctl.argtypes[c_int,c_void_p]hw.pl_hw_ioctl.restypec_int32# -----------------------------------------------------------------------------# 回调【关键优化增加try-except捕获异常】# DLL子线程抛出未捕获Python异常 直接栈破坏崩溃# -----------------------------------------------------------------------------defon_event(event,user):try:evtevent.contentsifevt.eventPL_HW_EVENT_READ_RDY:print([Event] READ_READY)elifevt.eventPL_HW_EVENT_WRITE_DONE:print([Event] WRITE_DONE)elifevt.eventPL_HW_EVENT_IRQ:print([Event] IRQ)elifevt.eventPL_HW_EVENT_ERROR:print([Event] ERROR)else:print([Event],evt.event)exceptExceptionase:print(fCallback inner exception:{e})# 全局持久回调对象防止GC回收callbackCALLBACK(on_event)# -----------------------------------------------------------------------------# Dump工具# -----------------------------------------------------------------------------defdump_buffer(title,buf,offset,size):print(f\n{title})line[]foriinrange(size):valbuf[offseti]line.append(f{val:02X})if(i1)%160:print( .join(line))line.clear()ifline:print( .join(line))# -----------------------------------------------------------------------------# Main# -----------------------------------------------------------------------------defmain():print( pl_hw win32 test )keys(c_char_p*1)()keys[0]bcomPortvalues(c_uint32*1)()values[0]1rethw.pl_hw_open(keys,values,1)print(pl_hw_open ,ret)ifret!0:print(open failed)return# 注册回调hw.pl_hw_set_callback(callback,None)# 共享缓冲区全局生命周期gBuffer(c_uint8*64)()hw.pl_hw_set_buffer(gBuffer,32,64)# 填充发送区foriinrange(32):gBuffer[32i]i dump_buffer(Write Buffer:,gBuffer,32,32)print(\nPL_HW_IOCTL_WRITE_RDY)hw.pl_hw_ioctl(PL_HW_IOCTL_WRITE_RDY,None)dump_buffer(Read Buffer:,gBuffer,0,32)time.sleep(1)time.sleep(0.2)hw.pl_hw_close()print(closed)if__name____main__:main()zynq_hw_test_py2.py# -*- coding: utf-8 -*-importtimefromctypesimport*hwCDLL(./libplHwServer_petalinux_release.so)hw.pl_hw_get_version.restypec_uint32printDLL Version : 0x%06X%hw.pl_hw_get_version()# -----------------------------------------------------------------------------# 常量# -----------------------------------------------------------------------------PL_HW_EVENT_NONE0PL_HW_EVENT_IRQ1PL_HW_EVENT_READ_RDY2PL_HW_EVENT_WRITE_DONE3PL_HW_EVENT_ERROR4PL_HW_EVENT_LOG5PL_HW_IOCTL_WRITE_RDY1# -----------------------------------------------------------------------------# 数据结构# -----------------------------------------------------------------------------classPlHwEventData(Structure):_fields_[(event,c_int),(code,c_int32),(data,c_void_p),(size,c_uint32),]# Linux 使用 CFUNCTYPECALLBACKCFUNCTYPE(None,POINTER(PlHwEventData),c_void_p)# -----------------------------------------------------------------------------# 函数声明# -----------------------------------------------------------------------------hw.pl_hw_open.argtypes[POINTER(c_char_p),POINTER(c_uint32),c_uint32]hw.pl_hw_open.restypec_int32 hw.pl_hw_close.argtypes[]hw.pl_hw_close.restypeNonehw.pl_hw_set_buffer.argtypes[c_void_p,c_uint32,c_uint32]hw.pl_hw_set_buffer.restypec_int32 hw.pl_hw_set_callback.argtypes[CALLBACK,c_void_p]hw.pl_hw_set_callback.restypec_int32 hw.pl_hw_ioctl.argtypes[c_int,c_void_p]hw.pl_hw_ioctl.restypec_int32# -----------------------------------------------------------------------------# 回调函数# -----------------------------------------------------------------------------defon_event(event,user):evtevent.contentsifevt.eventPL_HW_EVENT_READ_RDY:print[Event] READ_READYelifevt.eventPL_HW_EVENT_WRITE_DONE:print[Event] WRITE_DONEelifevt.eventPL_HW_EVENT_IRQ:print[Event] IRQelifevt.eventPL_HW_EVENT_ERROR:print[Event] ERRORelse:print[Event],evt.event callbackCALLBACK(on_event)# -----------------------------------------------------------------------------# Dump缓冲区打印# -----------------------------------------------------------------------------defdump_buffer(title,buf,offset,size):printtitleforiinrange(size):print%02X %buf[offseti],if(i1)%160:printifsize%16!0:print# -----------------------------------------------------------------------------# Main# -----------------------------------------------------------------------------print pl_hw test (Linux/Petalinux Python2) keys(c_char_p*1)()keys[0]bcomPortvalues(c_uint32*1)()values[0]1rethw.pl_hw_open(keys,values,1)printpl_hw_open ,ret hw.pl_hw_set_callback(callback,None)# 64字节缓冲区gBuffer(c_uint8*64)()# [0~31] Read Buffer# [32~63] Write Bufferhw.pl_hw_set_buffer(gBuffer,32,64)# 填充发送区foriinrange(32):gBuffer[32i]i dump_buffer(Write Buffer:,gBuffer,32,32)print\nPL_HW_IOCTL_WRITE_RDYhw.pl_hw_ioctl(PL_HW_IOCTL_WRITE_RDY,None)dump_buffer(Read Buffer:,gBuffer,0,32)time.sleep(1)hw.pl_hw_close()zynq_hw_test_py3.pyimporttimefromctypesimport*# 加载DLL# -----------------------------------------------------------------------------hwCDLL(r./libplHwServer_petalinux_release.so)hw.pl_hw_get_version.restypec_uint32print(DLL Version : 0x%06X%hw.pl_hw_get_version())# -----------------------------------------------------------------------------# 常量# -----------------------------------------------------------------------------PL_HW_EVENT_NONE0PL_HW_EVENT_IRQ1PL_HW_EVENT_READ_RDY2PL_HW_EVENT_WRITE_DONE3PL_HW_EVENT_ERROR4PL_HW_EVENT_LOG5PL_HW_IOCTL_WRITE_RDY1# -----------------------------------------------------------------------------# 数据结构# -----------------------------------------------------------------------------classPlHwEventData(Structure):_fields_[(event,c_int),(code,c_int32),(data,c_void_p),(size,c_uint32),]CALLBACKCFUNCTYPE(None,POINTER(PlHwEventData),c_void_p)# -----------------------------------------------------------------------------# 函数声明# -----------------------------------------------------------------------------hw.pl_hw_open.argtypes[POINTER(c_char_p),POINTER(c_uint32),c_uint32]hw.pl_hw_open.restypec_int32 hw.pl_hw_close.argtypes[]hw.pl_hw_close.restypeNonehw.pl_hw_set_buffer.argtypes[c_void_p,c_uint32,c_uint32]hw.pl_hw_set_buffer.restypec_int32 hw.pl_hw_set_callback.argtypes[CALLBACK,c_void_p]hw.pl_hw_set_callback.restypec_int32 hw.pl_hw_ioctl.argtypes[c_int,c_void_p]hw.pl_hw_ioctl.restypec_int32# -----------------------------------------------------------------------------# 回调# -----------------------------------------------------------------------------defon_event(event,user):evtevent.contentsifevt.eventPL_HW_EVENT_READ_RDY:print([Event] READ_READY)elifevt.eventPL_HW_EVENT_WRITE_DONE:print([Event] WRITE_DONE)elifevt.eventPL_HW_EVENT_IRQ:print([Event] IRQ)elifevt.eventPL_HW_EVENT_ERROR:print([Event] ERROR)else:print([Event],evt.event)callbackCALLBACK(on_event)# -----------------------------------------------------------------------------# Dump# -----------------------------------------------------------------------------defdump_buffer(title,buf,offset,size):print(title)foriinrange(size):print(%02X %buf[offseti],end)if(i1)%160:print()ifsize%16!0:print()# -----------------------------------------------------------------------------# Main# -----------------------------------------------------------------------------print( pl_hw mock )keys(c_char_p*1)()keys[0]bcomPortvalues(c_uint32*1)()values[0]1rethw.pl_hw_open(keys,values,1)print(pl_hw_open ,ret)hw.pl_hw_set_callback(callback,None)# 64字节缓冲区gBuffer(c_uint8*64)()# [0~31] Read Buffer# [32~63] Write Bufferhw.pl_hw_set_buffer(gBuffer,32,64)# 填充发送区foriinrange(32):gBuffer[32i]i dump_buffer(Write Buffer:,gBuffer,32,32)print(\nPL_HW_IOCTL_WRITE_RDY)hw.pl_hw_ioctl(PL_HW_IOCTL_WRITE_RDY,None)dump_buffer(Read Buffer:,gBuffer,0,32)time.sleep(1)hw.pl_hw_close()测试rootant:~# python3 zynq_hw_test_py3.pyDLL Version:0x010100pl_hw mockpl_hw_open0Write Buffer: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F101112131415161718191A 1B 1C 1D 1E 1F PL_HW_IOCTL_WRITE_RDY[Event]WRITE_DONE Read Buffer: 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F101112131415161718191A 1B 1C 1D 1E 1F20