开发自动化工具过程当中,须要使用专业的测试射频参数的仪器IQmeasure,厂家提供的API只用C ++版本。
客户端使用python(wxpython)开发,因此遇到了python调用C ++类库的问题。html
ctypes的python官方文档python
使用前请先浏览ctypes官方文档工具
test
├── IQmeasure_SCPI.dll
├── venv
└── 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 ++部分的解释、名词有误差,水平限制勿怪,欢迎私信纠正。指针
内容为本人和骆小萍同窗在开发实践中获得rest
以上日志