gpt4 book ai didi

Python Ctypes 异常 : access violation reading

转载 作者:太空狗 更新时间:2023-10-30 01:29:39 25 4
gpt4 key购买 nike

我尝试通过 DLL(PLC 制造商分发的 C API 接口(interface))与 PLC 通信。我正在使用 Python 3.1,它作为脚本环境嵌入到其他软件(x64 - Windows 7)中。

我设法让一些 DLL 函数正常工作,但现在我得到了一个我无法解决的“访问冲突读取”。

DLL函数信息:

LONG AdsSyncReadReq(
PAmsAddr pAddr,
ULONG nIndexGroup,
ULONG nIndexOffset,
ULONG nLength,
PVOID pData
);

参数:

  • pAddr:[in] 包含 ADS 服务器的 NetId 和端口号的结构。
  • nIndexGroup:[in] 索引组。
  • nIndexOffset:[in] 索引偏移量。
  • nLength:[in] 数据的字节长度。
  • pData:[out] 指向将接收数据的数据缓冲区的指针。
  • 返回值:返回函数的错误状态。

结构 AmsAddr:

typedef struct {
AmsNetId netId;
USHORT port;
} AmsAddr, *PAmsAddr;

结构 AmsNetId

typedef struct {
UCHAR b[6];
} AmsNetId, *PAmsNetId;

Python 实现:

# -*- coding: utf-8 -*-
from ctypes import *

#I've tried OleDll and windll as wel..
ADS_DLL = CDLL("C:/Program Files/TwinCAT/Ads Api/TcAdsDll/x64/TcAdsDll.dll")

class AmsNetId(Structure):
_fields_ = [('NetId', c_ubyte*6)]

class AmsAddr(Structure):
_fields_=[('AmsNetId',AmsNetId),('port',c_ushort)]

# DLL function working fine
version = ADS_DLL.AdsGetDllVersion()
print(version)

#DLL function working fine
errCode = ADS_DLL.AdsPortOpen()
print(errCode)

#DLL function using the AmsAddr() class, working fine
amsAddress = AmsAddr()
pointer_amsAddress = pointer(amsAddress)
errCode = ADS_DLL.AdsGetLocalAddress(pointer_amsAddress)
print(errCode)
contents_amsAddres = pointer_amsAddress.contents

#Function that doens't work:
errCode = ADS_DLL.AdsSyncReadReq()
print(errCode) # --> errCode = timeout error, normal because I didn't pass any arguments

# Now with arguments:
plcNetId = AmsNetId((c_ubyte*6)(5,18,18,27,1,1)) #correct adress to the PLC
plcAddress = AmsAddr(plcNetId,801) #correct port to the PLC
nIndexGroup = c_ulong(0xF020)
nIndexOffset = c_ulong(0x0)
nLength = c_ulong(0x4)
data = c_void_p()
pointer_data = pointer(data)

#I tried with an without the following 2 lines, doesn't matters
ADS_DLL.AdsSyncReadReq.argtypes=[AmsAddr,c_ulong,c_ulong,c_ulong,POINTER(c_void_p)]
ADS_DLL.AdsSyncReadReq.restype=None

#This line crashes
errCode = ADS_DLL.AdsSyncReadReq(plcAddress,nIndexGroup,nIndexOffset,nLength,pointer_data)
print(errCode)


>>>> Error in line 57: exception: access violation reading 0xFFFFFFFFFFFFFFFF

我希望任何人都无法弄清楚哪里出了问题。我只是 Python 编程的高级新手,完全没有 C 语言的经验

提前致谢

最佳答案

您传递的是无效指针,请改为提供有效的内存缓冲区:

data = create_string_buffer(nLength)

如果 PVOID 表示 void *,则参数应该只是 c_void_p 而不是 POINTER(c_void_p)。不要将 restype 设置为 None(该函数返回 LONG)。

同时传递 pointer(plcAddress)(在 argtypes 中指定 POINTER(AmsAddr))。

使用正确的调用约定(在 cdll、windll、oledll 之间选择)。

关于Python Ctypes 异常 : access violation reading,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15472217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com