Python,Ctypes - WindowsError:访问冲突

时间:2015-09-28 13:41:49

标签: python c ctypes

我正在开发用于测试工作的仪器驱动程序。它是第一个开发的 C使用IVI / Visa标准。然后我使用python包装器为C的DLL来使用python环境中的函数。然而,对于当前的一个,我收到一个错误:

Traceback (most recent call last):
  File "D:/CAS/Development/LvDrivers/Python/Python_wrapper/ADIag81110a.py", line 1362, in <module>
      PGEN.ConfigureTransitionCoupling(1, "1")
  File "D:/CAS/Development/LvDrivers/Python/Python_wrapper/ADIag81110a.py", line 1305, in ConfigureTransitionCoupling
      ADIag81110aLib().ADIag81110a_ConfigureTransitionCoupling(  self.vi,  transitionEnabling ,  channelName    )
WindowsError: exception: access violation reading 0x00000031

我在C中使用的任何函数都会发生这种情况。当它们直接在C中运行时,所有函数都能正常工作。他是该函数的C定义:

ViStatus _VI_FUNC ADIag81110a_ConfigureTransitionCoupling (ViSession vi,
                                              ViInt32 transitionEnabling,   ViChar channelName[])
{
   ViStatus error = VI_SUCCESS;
   checkErr( Ivi_LockSession (vi, VI_NULL));

   viCheckParm(Ivi_SetAttributeViInt32 (vi, channelName, ADIAG81110A_ATTR_TRANSITION_COUPLING, 
                                     0, transitionEnabling), 2, "Transition Coupling");  

Error:
   Ivi_UnlockSession (vi, VI_NULL);
   return error;
}

python可能看起来有点复杂,但我只会发布我认为与我正在使用的ConfigureTransitionCoupling函数的声明相关的代码以及ctypes声明:

import os
import visa_exceptions
from visa_messages import completion_and_error_messages
from vpp43_constants import *
from vpp43_types import *
from ctypes import cdll
if os.name == 'nt':
   from ctypes import windll
else:
   from ctypes import CFUNCTYPE as FUNCTYPE
import warnings

class Singleton(object):
    def __new__(cls, *args, **kwds):
      it = cls.__dict__.get("__it__")
      if it is not None:
        return it
    cls.__it__ = it = object.__new__(cls)
    it.init(*args, **kwds)
    return it


class ADIag81110a_lib(Singleton):
    def __call__(self, force_cdecl=False):
    if self.__lib is None or self.__cdecl_lib is None:
        self.load_library()
    if force_cdecl:
        return self.__cdecl_lib
    return self.__lib
def init(self):
    self.__lib = self.__cdecl_lib = None

def load_library(self, path=None):
    if os.name == 'nt':
        path = "C:\Program Files (x86)\IVI Foundation\IVI\Bin\ADIag81110a_32.dll"
        self.__lib = windll.LoadLibrary(path)
        self.__cdecl_lib = cdll.LoadLibrary(path)
    elif os.name == 'posix':
        if not path:
            path = "please put path to unix/linix here"
        self.__lib = self.__cdecl_lib = cdll.LoadLibrary(path)
    else:
        self.__lib = self.__cdecl_lib = None
        raise visa_exceptions.OSNotSupported, os.name
    self.__initialize_library_functions()
    return None

def __initialize_library_functions(self):
   self.__set_argument_types("ADIag81110a_ConfigureTransitionCoupling", [   ViSession ,  ViInt32 ,  ViChar ])

def __set_argument_types(self, inst_function, types, force_cdecl=False,   may_be_missing=True):
    if force_cdecl:
        library = self.__cdecl_lib
    else:
        library = self.__lib
    try:
        getattr(library, inst_function).argtypes = types
    except AttributeError:
        if not may_be_missing:
            raise

 ADIag81110aLib = ADIag81110a_lib()

 class ADIag81110a():

      def ConfigureTransitionCoupling(self ,  transitionEnabling ,  channelName    ):
            ADIag81110aLib().ADIag81110a_ConfigureTransitionCoupling(  self.vi,  transitionEnabling ,  channelName    )

if __name__ == '__main__':
   #This initilises the communication with the instrument
   PGEN = ADIag81110a("GPIB0::10::INSTR")

   test = PGEN.ReadOutputImpedance("1")

   print test

我看过其他人发布有关他们的错误,我觉得他们以不同的方式使用C类型,所以我无法应用他们的解决方案。

感谢任何帮助。这也是我关于堆栈溢出的第一篇文章,所以请随意指出我的帖子的任何问题:)

0 个答案:

没有答案