pyads
pyads copied to clipboard
Added fallback to type detection to allow automatic ENUM usage
+ Added exception raising for unknown types
Fixes #263 . The issue is almost three years old, but still relevant.
This allows using enums both with plc.read_by_name()
and through AdsSymbol.read()
.
Tested the code with:
from pyads import Connection
import pyads
def main():
plc = Connection(
ams_net_id="127.0.0.1.1.1",
ams_net_port=851,
)
variables = [
"my_double",
"my_enum",
"my_double_array",
"my_enum_array",
"my_struct",
]
with plc:
for var in variables:
try:
value = plc.read_by_name(f"GVL_Main.{var}", cache_symbol_info=False)
except pyads.ADSError as err:
print("ERROR:", err)
else:
print("Value:", value)
for var in variables:
try:
symbol = plc.get_symbol(f"GVL_Main.{var}")
value = symbol.read()
except pyads.ADSError as err:
print("ERROR:", err)
else:
print("Value:", value)
return
if __name__ == "__main__":
main()
Output:
Value: 0.0 Value: 1 Value: [0.0, 0.0, 0.0, 0.0, 0.0] Value: [2, 2, 2, 2, 2] ERROR: ADSError: Failed to detect datatype for
GVL_Main.my_struct
Value: 0.0 Value: 1 Value: [0.0, 0.0, 0.0, 0.0, 0.0] Value: [2, 2, 2, 2, 2] ERROR: ADSError: Cannot read data with unknown datatype for symbol GVL_Main.my_struct (MyStructure)