关于python:Python调用C类库-踩坑日志

5次阅读

共计 2769 个字符,预计需要花费 7 分钟才能阅读完成。

Python 调用 C ++ 类库 (踩坑日志)

起因

开发自动化工具过程中,须要应用业余的测试射频参数的仪器 IQmeasure,厂家提供的 API 只用 C ++ 版本。
客户端应用 python(wxpython)开发,所以遇到了 python 调用 C ++ 类库的问题。

环境

  • PyCharm2020
  • python3.6.12  32 位(python3.8 无奈应用,64 位 python 无奈应用;其余版本未测试)
  • IQmeasure_SCPI.dll(供应商给的 dll 包)
  • IQmeasure.chm(dll 对应的文档)

参考资料

ctypes 的 python 官网文档

应用

应用前请先浏览 ctypes 官网文档

  1. 创立一个新我的项目(python 应用 3.6.12 32 位版本)
  2. 将 dll 文件放入根目录
  3. 根目录创立 main.py

目录构造如下

test
├── IQmeasure_SCPI.dll
├── venv
└── main.py

根底应用办法如下(main.py):

from ctypes import *

# 加载 dll 包
iq = cdll.LoadLibrary('./IQmeasure_SCPI.dll')
# 调用办法
init_result = iq.LP_Init(c_int(0), c_int(1))
print('init_result:', init_result)  # init_result: 0
# 该办法中,返回 0 为胜利

# 原 C ++ 文档中,该办法如下:# int LP_Init(int IQtype = IQTYPE_XEL,int testerControlMethod = 1);

知识点

  • 应用 ctypes.cdll.LoadLibrary() 加载 dll 包
  • 参数须要转换为 c 的类型(如 c_int(),不了解的先看一下参考资料 ctypes 的 python 官网文档)
  • 在 C ++ 中的可选参数,在应用 python 调用时不可省略,请传入办法中的默认值

有返回值

from ctypes import *

# 加载 dll 包
iq = cdll.LoadLibrary('./IQmeasure_SCPI.dll')

# 有返回值
# 设置返回值类型
iq.LP_GetErrorString.restype = c_char_p
# 设置初始值类型
iq.LP_GetErrorString.argtypes = [c_int]

msg = iq.LP_GetErrorString(c_int(10))
print(msg)  # b'Invalid analysis type'
# 转换为 string
str_msg = msg.decode("utf-8")
print(str_msg) # VSA number is out of range. Try 1-4.

# 原 C ++ 文档中,该办法如下:# char* LP_GetErrorString(int err) 

知识点

  • 有返回值时,应用 . 办法名.restype 设置返回值类型
  • 参数值也可应用相似办法设置,应用 . 办法名.argtypes 设置参数类型

参数值为 * 类型

简略得查了材料,带 如同是指针类型吧,开发焦急所以没有深刻学习了,相似 int ,char * 这样的

# ... 省略加载 dll

# 参数值为 * 类型
# 应用 byref(),包装对应类型即可
iq.LP_SetTesterMode(c_int(0), byref(c_int(1)), c_int(1))
# 原 C ++ 文档中,该办法如下:# int LP_SetTesterMode(int signalMode = UP_TO_80MHZ_SIGNAL, int *selectedModules = NULL, int numOfSelectedModules = 1);

知识点

  • 参数为 * 类型的参数,应用 byref 包装

援用类型参数

# ... 省略加载 dll

version = create_string_buffer(4096)
iq.LP_GetVersion(version, 4096)
version_result = version.value.decode("utf-8")
# 原 C ++ 文档中,该办法如下:# 该办法会扭转 *buffer,python 中须要读取 *buffer 的值
# bool LP_GetVersion(char *buffer, int buf_size);

知识点

  • 办法参数传入后,办法会扭转原参数,须要读取改参数的新值
  • 先创立一个 buffer
  • 调用办法
  • 将 buffer 转换回来

残缺测试代码

from ctypes import *

# 加载 dll 包
iq = cdll.LoadLibrary('./IQmeasure_SCPI.dll')
# 调用办法
init_result = iq.LP_Init(c_int(0), c_int(1))
print('init_result:', init_result)  # init_result: 0
# 该办法中,返回 0 为胜利

# 原 C ++ 文档中,该办法如下:# int LP_Init(int IQtype = IQTYPE_XEL,int testerControlMethod = 1);

# 有返回值
# 设置返回值类型
iq.LP_GetErrorString.restype = c_char_p
# 设置初始值类型
iq.LP_GetErrorString.argtypes = [c_int]

msg = iq.LP_GetErrorString(c_int(10))
print(msg)  # b'Invalid analysis type'
# 转换为 string
str_msg = msg.decode("utf-8")
print(str_msg)  # VSA number is out of range. Try 1-4.

# 原 C ++ 文档中,该办法如下:# char* LP_GetErrorString(int err)

# 参数值为 * 类型
# 应用 byref(),包装对应类型即可
iq.LP_SetTesterMode(c_int(0), byref(c_int(1)), c_int(1))
# 原 C ++ 文档中,该办法如下:# int LP_SetTesterMode(int signalMode = UP_TO_80MHZ_SIGNAL, int *selectedModules = NULL, int numOfSelectedModules = 1);

version = create_string_buffer(4096)
iq.LP_GetVersion(version, 4096)
version_result = version.value.decode("utf-8")
# 原 C ++ 文档中,该办法如下:# 该办法会扭转 *buffer,python 中须要读取 *buffer 的值
# bool LP_GetVersion(char *buffer, int buf_size);

总结

因为对 C ++ 不甚了解,对 C ++ 局部的解释、名词有偏差,程度限度勿怪,欢送私信纠正。

内容为自己和骆小萍同学在开发实际中失去

以上

正文完
 0