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

2020-12-03  本文已影响0人  言午日尧耳总

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

起因

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

环境

参考资料

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);

知识点

有返回值

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) 

知识点

参数值为 *类型

简单得查了资料,带 * 好像是指针类型吧,开发着急所以没有深入学习了,类似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 );

知识点

引用类型参数

# ...省略加载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);

知识点

完整测试代码

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 ++部分的解释、名词有偏差,水平限制勿怪,欢迎私信纠正。

内容为本人和骆小萍同学在开发实践中得到

以上

上一篇 下一篇

猜你喜欢

热点阅读