Memory fault when setting potentialgroup of a Siemens IO-card through Openness with Python and Ctypes

70 views Asked by At

I am making a Hardware/Software-generator for TIA portal 16 in Python 3.7. It is working pretty well but now I am running into a problem:

I want to change the potentialgroups of certain IO-cards through this program but through inspection with Openness Explorer I found out that it is set in a Uint64 variable. Some digging tought me that Python doesn't support this datatype but you can use Ctypes to use it anyway. However when I do this I get an error:

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)

Another small sidenote is that the code that is run is compiled and then executed from a text file.

I have tried multiple things:

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

Shows the error above

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

Also shows the error above

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

Gives an error about wrong datatype

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

Gives 1 with datatype int

I am running out of ideas on what to try, so please let me know if you guys have any ideas.

Have a good one! -Rick

0

There are 0 answers