Logic to allow logix_driver.write and .read to accept a list/tuple of (tag, value) #243
gis667en11
started this conversation in
Ideas
Replies: 1 comment 1 reply
-
Try:
Using * on the argument of a list will unpack it. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello! I made a quick modification to allow the write() method to accept an array of tuples.
if len(tags_values) == 1 and isinstance(tags_values[0], Union[tuple,list]):
. . . .
tags_values = tags_values[0]
This going just before the "if len(tags_values) == 2" around line 1050. I'm not certain how to do pull requests, but this change allowed me to do things like this:
strings = [ 'tag.msgData.timeSinceLastReceipt_ms',
. . . .
'tag.msgData.timeSinceLastUpdate_ms',
. . . .
'tag.msgData.receiptCount',
. . . .
'tag.state_Start',
. . . .
'tag.state_Standby',
. . . .
'tag.coordinate_X',
. . . .
'tag.coordinate_Y']
svalues = [45, 555, 789, True, False, 3.14, 8008.135]
writeTags = zip(strings,svalues)
with pycomm3.LogixDriver('192.168.1.10') as plc:
. . . .
plc.write(writeTags)
Or something fancy like this:
writeTags = []
if ButtonPressed:
. . . .
buttonCount += 1
. . . .
writeTags.extend( ('tag.ButtonCount', buttonCount) )
if updateTempAlarm:
. . . .
if TemperatureHigh:
. . . . . . . .
writeTags.extend( ('tag.tempAlarm', True)
. . . .
else:
. . . . . . . .
writeTags.extend( ('tag.tempAlarm', False)
if writeTags:
. . . .
with pycomm3.LogixDriver('192.168.1.10') as plc:
. . . . . . . .
plc.write(writeTags)
So, the write method would either write to the ButtonCount integer AND update the tempAlarm bool, OR just the tempAlarm bool, OR would write nothing at all.
With this method, the multi-service request feature of the write method can be leveraged while allowing a dynamic set of tags as an input. which is otherwise impossible with the current code.
The same is also possible by adding this to the .read() method:
if len(tags) == 1 and isinstance(tags[0], Union[tuple,list]):
. . . .
tags = tags[0]
Or am I missing some trick for passing a variable number of arguments to a method?
Beta Was this translation helpful? Give feedback.
All reactions