【问题标题】:Python AttributeError: function 'Search' not foundPython AttributeError:找不到函数“搜索”
【发布时间】:2016-09-07 18:16:53
【问题描述】:

我正在尝试使用 API 控制 Tektronix RSA306 频谱分析仪。程序找到了 RSA300API.dll 文件,但在搜索和连接到设备时抛出错误。我正在运行的程序是 Tektronix 的一个示例。我目前使用的设置是 64 位 Windows 7 上的 Python 2.7.12 x64(Anaconda 4.1.1)。

from ctypes import *
import numpy as np
import matplotlib.pyplot as plt

我正在查找 .dll 文件:

rsa300 = WinDLL("RSA300API.dll")

执行搜索功能时出现错误:

longArray = c_long*10
deviceIDs = longArray()
deviceSerial = c_wchar_p('')
numFound = c_int(0)
serialNum = c_char_p('')
nomenclature = c_char_p('')
header = IQHeader()

rsa300.Search(byref(deviceIDs), byref(deviceSerial), byref(numFound))
if numFound.value == 1:
   rsa300.Connect(deviceIDs[0])
else:
   print('Unexpected number of instruments found.')
   exit()

运行时出现以下错误信息:

C:\Anaconda2\python.exe C:/Tektronix/RSA_API/lib/x64/trial
<WinDLL 'RSA300API.dll', handle e47b0000 at 3ae4e80>
Traceback (most recent call last):
  File "C:/Tektronix/RSA_API/lib/x64/trial", line 44, in <module>
    rsa300.Search(byref(deviceIDs), byref(deviceSerial), byref(numFound))
  File "C:\Anaconda2\lib\ctypes\__init__.py", line 376, in __getattr__
    func = self.__getitem__(name)
  File "C:\Anaconda2\lib\ctypes\__init__.py", line 381, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'Search' not found

我遇到的问题是找不到“搜索”功能。有什么办法可以解决这个问题?

【问题讨论】:

  • 您确定打开 dll 的方式吗?也许你应该这样试试:stackoverflow.com/a/3173926/5920310 你确定 dll 里面有一个Search 函数吗?
  • 我假设 dll 中有一个“搜索”功能。该 dll 直接从 Tektronix 网站下载,示例程序也从那里下载。 @XavierC。
  • 你能提供dir(rsa300)help(rsa300)的输出吗?这会有所帮助。

标签: python windows anaconda ctypes


【解决方案1】:

这里是泰克应用工程师。

这里的问题是 API 版本不匹配。您的代码引用了旧版本的 API (RSA300API.dll),错误消息引用了更新版本的 API (RSA_API.dll)。确保您已安装最新版本的 API,并且您在代码中引用了正确的 dll。

这里是下载最新版本 RSA API 的链接(截至 2016 年 11 月 1 日): http://www.tek.com/model/rsa306-software

这里是下载 API 文档的链接(截至 2016 年 11 月 1 日)。本文档附有一个 Excel 电子表格,概述了旧函数和新函数之间的区别: http://www.tek.com/spectrum-analyzer/rsa306-manual-6

为了清晰和一致,在新版本中更改了函数名称。旧版本的 API 大部分函数都没有前缀,仅通过读取函数名称就不清楚哪些函数是组合在一起的。新版本的 API 将前缀应用于所有函数,现在只需阅读其声明即可更轻松地判断给定函数位于哪个功能组中。例如,旧的搜索和连接函数被简单地称为 Search() 和 Connect(),而新版本的函数被称为 DEVICE_Search() 和 DEVICE_Connect()。

注意:我使用 cdll.LoadLibrary("RSA_API.dll") 加载 dll 而不是 WinDLL()。

DEVICE_Search() 的参数与 Search() 略有不同。由于不同的参数数据类型,新的 DEVICE_Search() 函数在 ctypes 上的表现不如旧的 Search() 函数,但我找到了一个有效的方法(参见下面的代码)。

这是我在 RSA 控制脚本开头使用的 search_connect() 函数:

from ctypes import *
import os

"""
################################################################
C:\Tektronix\RSA306 API\lib\x64 needs to be added to the 
PATH system environment variable
################################################################
"""
os.chdir("C:\\Tektronix\\RSA_API\\lib\\x64")
rsa = cdll.LoadLibrary("RSA_API.dll")


"""#################CLASSES AND FUNCTIONS#################"""
def search_connect():
    #search/connect variables
    numFound = c_int(0)
    intArray = c_int*10
    deviceIDs = intArray()
    #this is absolutely asinine, but it works
    deviceSerial = c_char_p('longer than the longest serial number')
    deviceType = c_char_p('longer than the longest device type')
    apiVersion = c_char_p('api')

    #get API version
    rsa.DEVICE_GetAPIVersion(apiVersion)
    print('API Version {}'.format(apiVersion.value))

    #search
    ret = rsa.DEVICE_Search(byref(numFound), deviceIDs, 
        deviceSerial, deviceType)

    if ret != 0:
        print('Error in Search: ' + str(ret))
        exit()
    if numFound.value < 1:
        print('No instruments found. Exiting script.')
        exit()
    elif numFound.value == 1:
        print('One device found.')
        print('Device type: {}'.format(deviceType.value))
        print('Device serial number: {}'.format(deviceSerial.value))
        ret = rsa.DEVICE_Connect(deviceIDs[0])
        if ret != 0:
            print('Error in Connect: ' + str(ret))
            exit()
    else:
        print('Unexpected number of devices found, exiting script.')
        exit()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    相关资源
    最近更新 更多