我是ctypes dll使用的新手,在这里,我试图使用关联的USBaccessx64.dll控制cleware USB Mux。 当我使用anaconda Python版本(3.6.4)运行以下代码时,出现OSError:如下所述,但是使用Python 3.4.5和更早版本运行时,我可以运行相同的代码而没有错误。我不确定这两个python版本在ctypes用法上的区别。
import ctypes
import logging
import os.path
class USBSwitchBox( object ) :
_clelib = None
_cl_handle = None
_boxid = None
USBtype_SWITCH8_DEVICE = 0x30
def __init__( self , initdata = "00000000", mapping = {}) :
dll_name = "USBaccessx64.dll"
dllabspath = os.path.dirname(os.path.abspath(__file__)) + os.path.sep + dll_name
self._connected = 0
self._clelib = ctypes.windll.LoadLibrary(dllabspath)
if not self._clelib :
raise Exception('Unable to Load USBaccess.dll')
self._cl_handle = self._clelib.FCWInitObject()
if not self._cl_handle :
raise Exception('Unable to Initialize USBaccess.dll')
self._boxid = None
devCnt = self._clelib.FCWOpenCleware( self._cl_handle )
for i in range(0, devCnt) :
devType = self._clelib.FCWGetUSBType(self._cl_handle, i)
if int(self.USBtype_SWITCH8_DEVICE) == devType :
self._boxid = i
break
self._data = int (initdata,2)
result = self._clelib.FCWSetMultiSwitch( self._cl_handle, self._boxid, self._data )
if result < 0 :
raise Exception('Communication Error')
if __name__ == "__main__":
switch = USBSwitchBox()
使用Python 3.6.4版| Anaconda,Inc.我收到以下错误。
Traceback (most recent call last):
File "usbswitch.py", line 39, in <module>
switch = USBSwitchBox()
File "usbswitch.py", line 26, in __init__
devCnt = self._clelib.FCWOpenCleware( self._cl_handle )
OSError: exception: access violation reading 0x0000000027F15060