Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors in reading structures #443

Open
davidemannone opened this issue Jan 13, 2025 · 0 comments
Open

Errors in reading structures #443

davidemannone opened this issue Jan 13, 2025 · 0 comments

Comments

@davidemannone
Copy link

davidemannone commented Jan 13, 2025

I've tried to read this simple structure:

MyStruct = {
    ("MyVar1", pyads.PLCTYPE_BYTE, 1),
    ("MyVar2", pyads.PLCTYPE_REAL, 1)
}

Using Symbols:

myStruct = plc.get_symbol("MYSYMBOL", structure_def=MyStruct)

Reading and writing works fine. But when i try to use Notification:

myHandle = myStruct.add_device_notification(callback)

I've got a first error, and when found where and why, and solved, a second error.
The first error reports line 138 of symbol.py with error:

TypeError: unsupported operand type(s) for *: 'set' and 'int'

self._structure_size = size_of_structure(self.structure_def * self.array_size) 

To let it work properly I had to change it in:

self._structure_size = size_of_structure(self.structure_def) * self.array_size

And this way it worked fine. But after that I got this second error in the same file but line 254:

TypeError: this type has no size

attr = NotificationAttrib(sizeof(self.plc_type))

To let them work properly with at least the primitive types (I've tested just with them) I've changed the code in:

if attr is None: # when optional attr.Length is provided it works fine, telling PLC to monitor changes of primitive or structure up to size of the variable
  if self.plc_type is not None: # this is provided with primitive types
      attr = NotificationAttrib(sizeof(self.plc_type))
  elif self.structure_def is not None:  # because it is left None instead of self.symbol_type that is got from ADS, but seems to be just a string and no typedef is provided even if structure_def param has been provided 
      attr = NotificationAttrib(self._structure_size)  # size has been set correctly
  else: # I don't know if or when this can occur
      attr = NotificationAttrib(1)  # this should at least work, notification is set without error on a single byte subscription length (minimum length) and a warning message should be provided. Or other choices should be provided and  least (alternatively to notification(1) or as last chance should be raised an exception
      import warnings
      warnings.warn("No plc_type found for symbol notification with length 1 is used.")
      # raise ValueError('No plc_type found for symbol notification. Len value  cannot be retrieved')  # use instead of notification(1)+warning.warn()

This way, it seems me that primitives and structures works fine.

What did I wrong or is there something to improve?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant