2012-03-23 34 views
0

我是一個初學者python和ctypes,我正在寫一個腳本來控制一個PCMCIA設備,供應商提供的驅動程序是一個dll文件。我只是想調用它的功能,但我只是不知道爲什麼我一直得到一個[「0x7c9108d3」的指令引用「0xfffffff8」的內存。內存不能被「讀取」]。下面是文件中稱:Python ctypes - 調用DLL中的函數時發生內存讀取錯誤

//declaration 
XLstatus xlGetDriverConfig(XLdriverConfig *pDriverConfig) 

typedef struct s_xl_driver_config { 
    unsigned int  dllVersion; 
    unsigned int  channelCount; 
    unsigned int  reserved[10]; 
    XLchannelConfig channel[XL_CONFIG_MAX_CHANNELS]; 
} XLdriverConfig; 

//types 
typedef struct s_xl_channel_config { 

    char   name [XL_MAX_LENGTH + 1]; 
    unsigned char hwType; 
    unsigned char hwIndex; 
    unsigned char hwChannel; 
    unsigned short transceiverType; 
    unsigned int transceiverState; 
    unsigned char channelIndex;  

    XLuint64  channelMask;  //here 
    unsigned int channelCapabilities; 
    unsigned int channelBusCapabilities; 
    unsigned char isOnBus; 
    unsigned int connectedBusType; 
    XLbusParams busParams; 
    unsigned int driverVersion; 
    unsigned int interfaceVersion; 
    unsigned int raw_data[10]; 
    unsigned int serialNumber; 
    unsigned int articleNumber; 
    char   transceiverName [XL_MAX_LENGTH + 1]; 
    unsigned int specialCabFlags; 
    unsigned int dominantTimeout; 
    unsigned int reserved[8]; 
} XLchannelConfig; 

typedef unsigned __int64 XLuint64; 

typedef struct {                   
    unsigned int busType; 
    union { 
    struct { 
     unsigned int bitRate; 
     unsigned char sjw; 
     unsigned char tseg1; 
     unsigned char tseg2; 
     unsigned char sam; // 1 or 3 
     unsigned char outputMode; 
    } can; 
    struct { 
     unsigned int activeSpeedGrade; 
     unsigned int compatibleSpeedGrade; 
    } most; 
    unsigned char raw[32]; 
    }data; 
} XLbusParams; 

下面有我的Python腳本:

from ctypes import * 
vxlapi = WinDLL("vxlapi.dll") 
PyxlGetDriverConfig = vxlapi.xlGetDriverConfig 

class PyXLchannelConfig(Structure): 
    _fields_ = [("Pyname",c_char*32), 
       ("PyhwType",c_ubyte), 
       ("PyhwIndex",c_ubyte), 
       ("PyhwChannel",c_ubyte), 
       ("PytransceiverType",c_ushort), 
       ("PytransceiverState",c_ushort), 
       ("PyconfigError",c_ushort), 
       ("PychannelIndex",c_ubyte), 
       ("PychannelMask",c_longlong), 
       ("PychannelCapabilities",c_uint), 
       ("PychannelBusCapabilities",c_uint), 

       ("PyisOnBus",c_ubyte), 
       ("PyconnectedBusType",c_uint), 
       ("PybusParams",c_uint), 

       ("PydriverVersion",c_uint), 
       ("PyinterfaceVersion",c_uint), 
       ("Pyraw_data",c_uint*10), 

       ("PyserialNumber",c_uint), 
       ("PyarticleNumber",c_uint), 

       ("PytransceiverName",c_char*32), 

       ("PyspecialCabFlags",c_uint), 
       ("PydominantTimeout",c_uint), 
       ("PydominantRecessiveDelay",c_ubyte), 
       ("PyrecessiveDominantDelay",c_ubyte), 
       ("PyconnectionInfo",c_ubyte), 
       ("PycurrentlyAvailableTimestamps",c_ubyte), 
       ("PyminimalSupplyVoltage",c_ubyte), 
       ("PymaximalSupplyVoltage",c_ubyte), 
       ("PymaximalBaudrate",c_uint), 
       ("PyfpgaCoreCapabilities",c_ubyte), 
       ("PyspecialDeviceStatus",c_ubyte), 
       ("PychannelBusActiveCapabilities",c_ushort), 
       ("PybreakOffset",c_ushort), 
       ("PydelimiterOffset",c_ushort), 
       ("Pyreserved",c_uint*3) 
       ] 

class PyXLdriverConfig(Structure): 
    _fields_ = [("PydllVersion",c_uint), 
       ("PychannelCount",c_uint), 
       ("Pyreserved",c_uint*10), 
       ("Pychannel",PyXLchannelConfig*64) 
       ] 

if __name__ == "__main__": 
    drivercfg = PyXLdriverConfig() 
    PyxlGetDriverConfig(byref(drivercfg)) 

你能不能幫我出這一點,非常感謝你!

回答

0

我看到至少有兩點不同:

unsigned int transceiverState; 
("PytransceiverState",c_ushort), 

XLbusParams busParams; 
("PybusParams",c_uint), 
+0

謝謝,修改後,腳本正常運行 – edward 2012-03-27 07:50:58

0

有代碼中的一些錯誤,但在大多數情況下它的存在。

我假設以下定義出現在原來的C.

#define XL_CONFIG_MAX_CHANNELS (64) 
#define XL_MAX_LENGTH (31) 

雖然我得到XL_MAX_LENGTH大概定義爲32

的感覺我想從你的ctypes結構問題的莖和你的C結構是不同的大小,所以你的庫正在讀/寫內存,它是不允許訪問的。

這些都是相關的問題:

  • 有一些錯誤的類型在ctypes的結構; PytransceiverState應該是c_uint,而不是c_ushort; PyconfigError應該是c_ulonglong,而不是c_longlong);等等。
  • 有相當多的錯誤領域;什麼是C的當量PyconfigError; PydominantTimeout之後的所有ctypes字段都來自哪裏?
  • 您沒有定義相當於XLBusParams的ctypes結構,而是使用c_uint。由於XLBusParams的大小至少爲45個字節,而您提供了4個字節,所以會發生不好的事情。
0

你看過這個包裝的vxlapi for python: http://code.google.com/p/pycanlibxl/

請注意,它沒有實現所有vxlapi.dll函數和頭文件常量和枚舉。 還有一些函數定義具有可能不適合你的默認值(如接口類型)。

+0

太棒了!非常感謝!這爲我節省了很多時間。 – edward 2012-03-27 07:49:16

相關問題