通过 Python 和 Ctypes 的 Openness 设置西门子 IO 卡的电位组时出现内存故障

问题描述 投票:0回答:1

我正在使用 Python 3.7 为 TIA Portal 16 制作硬件/软件生成器。它运行得很好,但现在我遇到了问题:

我想通过这个程序更改某些IO卡的电位组,但通过Openness Explorer检查我发现它是在Uint64变量中设置的。经过一番挖掘,我发现 Python 不支持这种数据类型,但无论如何你都可以使用 Ctypes 来使用它。但是,当我这样做时,我收到错误:

Onverwerkte uitzondering: System.Reflection.TargetInvocationException: The goal of the call created an exception. ---> System.AccessViolationException: Attempt at reading or writing of protected memory. This often points to damaged memory
   at Python.Runtime.Runtime.PyObject_GetIter(IntPtr op)
   at Python.Runtime.PyIter..ctor(PyObject iterable)
   at Python.Runtime.PyObject.GetEnumerator()
   at Siemens.Engineering.Private.Session.ViewToContract(IEnumerable items)
   at Siemens.Engineering.Private.Session.ViewToContract(Object value)
   at Siemens.Engineering.Private.Session.Siemens.Engineering.Private.IObjectSession.SetAttribute[TC](LifetimeContractHandle`1 lifetimeContractHandle, String attributeName, Object attributeValue, String fullName)
   at Siemens.Engineering.Private.InternalObject_Access`2.SetAttribute(String name, Object value)
   --- End of intern exceptionstackpad ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Python.Runtime.MethodBinder.Invoke(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, MethodInfo[] methodinfo)
   at Python.Runtime.MethodObject.Invoke(IntPtr target, IntPtr args, IntPtr kw, MethodBase info)
   at Python.Runtime.MethodBinding.tp_call(IntPtr ob, IntPtr args, IntPtr kw)

另一个小旁注是运行的代码是编译然后从文本文件执行的。

我尝试了多种方法:

import ctypes
device.DeviceItems[index + 3].SetAttribute("PotentialGroup", ctypes.c_uint64(tia.HW.PotentialGroup.LightBaseUnit))

显示上面的错误

import ctypes
PotGroup = ctypes.c_uint64(1)
device.DeviceItems[index + 3].SetAttribute("PotentialGroup", PotGroup)

还显示上面的错误

import ctypes
device.DeviceItems[index + 3].SetAttribute("PotentialGroup", 1)

给出有关错误数据类型的错误

import ctypes
device.DeviceItems[index + 3].GetAttribute("PotentialGroup")

数据类型为 int 时给出 1

我已经没有什么想法可以尝试了,所以如果你们有任何想法,请告诉我。

祝你过得愉快! -瑞克

python python-3.x ctypes tia-portal
1个回答
0
投票

我正在为 Simocode 做类似的事情,但原理相同 “devItem.SetAttribute('电机保护类', 20)”

我发现我可以让它适用于文本、bool 和 Float,但是当它是 Int 时,我已经厌倦了所有相同的事情,甚至通过位移位硬编码为 UInt 32

您找到解决方案了吗?我们也在 C# 中对此进行了测试

© www.soinside.com 2019 - 2024. All rights reserved.