pycomm3 icon indicating copy to clipboard operation
pycomm3 copied to clipboard

If statement with PLC TAG is it possible?

Open demjandav opened this issue 3 years ago • 2 comments

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

demjandav avatar Sep 19 '22 12:09 demjandav

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}')


ottowayi avatar Sep 19 '22 17:09 ottowayi

Thanks so much it helped a lot.

demjandav avatar Sep 20 '22 06:09 demjandav