Python,Ctypes - WindowsError: access violation

0

I'm developing instrument drivers for testing at work. It's first developed in C using the IVI/Visa standard. I am then using a python wrapper for a dll of the C to use the functions in the python environment. However with the current one I am getting an error:

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

It happens with any function I use from the C. All of the functions work correctly when they are ran directly in C. He is is the C definition of that function:

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

The python might seem a little complicated but i'll only be posting the code that I feel is relevant to declaration of the ConfigureTransitionCoupling function that I'm using as an example and the ctypes declarations:

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

I have looked at other people posting about their error and I feel they are using C-types in a different way to me so I am unable to apply their solutions.

Any help is appreciated. This is also my first post on stack overflow so feel free to point out any issues with my post :)

python
c
ctypes
asked on Stack Overflow Sep 28, 2015 by Trevor

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0