ctypes:OSError:exception:读取0x00000001时访问冲突

2024-05-23 16:59:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试用python通过c dll与HV电源通信。我能做的最简单的功能。但如果我调用更复杂的函数CAENHVInitSystem,就会得到一个错误:OSError:exception:access violation reading 0x00000001。我对Python中的ctypes很陌生。据我所知,这个错误可能是因为我的一些参数类型错误。但是我怎样才能调试它来知道哪个参数是错误的呢?有人看到我的错误了吗?

提前谢谢

import os
from ctypes import *

bib = CDLL("CAENHVWrapper")

ret = bib.CAENHVLibSwRel()  # This call works
print(c_char_p(ret)) 

sysType = c_int(1) #SY2527
link = c_int(0) #TCP/IP
#arg = c_char_p(b'149.217.10.241')  #i change it for test to c_void_p but later the arg should be the ip adress
arg = c_void_p()                   
user = c_char_p(b'admin')
passwd = c_char_p(b'admin')
sysHndl = c_int()

# c function definition in the header file
#CAENHVLIB_API CAENHVRESULT CAENHV_InitSystem(
#   CAENHV_SYSTEM_TYPE_t system,
#   int LinkType,
#   void *Arg,
#   const char *UserName,
#   const char *Passwd,
#   int *handle);

# definition of the enum of the first argument
#typedef enum {
#   SY1527      = 0,
#   SY2527      = 1
#} CAENHV_SYSTEM_TYPE_t;

bib.CAENHVInitSystem.argtypes = [c_int, c_int, c_void_p, c_char_p, c_char_p,     POINTER(c_int)]
ret = bib.CAENHVInitSystem(sysType, link, arg, user, passwd, byref(sysHndl))

print(ret)
print(bib.CAENHV_GetError(sysHndl))

Tags: theimport参数错误argctypesintprint