ottowayi / pycomm3

A Python Ethernet/IP library for communicating with Allen-Bradley PLCs.
MIT License
406 stars 88 forks source link

If statement with PLC TAG is it possible? #239

Closed demjandav closed 2 years ago

demjandav commented 2 years ago

Hi I would like to ask you if it is possible to use a PLC tag for If statement?

image

image

In this case it is always true: image image

Thank you for help. BR David

ottowayi commented 2 years ago

That is because Tag objects are returned and not just the value of the tag. And the truthiness represents the success of the request, so if you successfully read the tag then it would evaluate to True and if there was an error or something then it would be False. To get the actual value of the tag, you need to get it from the .value attribute.

Try running this code:

valami = plc.read('valami')
if valami:
    print(f'valami={valami.value}')
else:
    print(f'failed to read valami: {valami.error}')
demjandav commented 2 years ago

Thanks so much it helped a lot.