Looping Through Tags

Dear Sir,

I am new to Jython Scripting / Ignition Scripting

I have about 16 Tags as memory tags
and about 16 Tags in the PLC (throught kepware OPC)
all are boolean tags
Tag in memory: MOTOR_SETTINGS/STAND1/EN_STAND1 (stand 1 to stand 16)
Tag in plc: MOTOR_SETTINGS/EN_STAND1 (stand 1 to stand 16)

I want to dynamically loop through these tags??
does this works???

x = 1

while x < 17:
z = system.tag.getTagValue(“MOTOR_SETTINGS/STAND%d” % x"/EN_STAND%d" % x).
system.tag.writeToTag(“MOTOR_SETTINGS/EN_STAND%d” % x, z)
print x
x = x+1

could you please adivse me???


You’re on the right track, but avoid the “while” instruction when other instructions will do. I have done a few fairly complex projects, and the only place I use “while” is in a socket module. If you forget to program an escape in a “while” loop, you’ll kick yourself. :slight_smile:

So, use the “for” instruction instead. Here is a basic “for” loop with some formatting, and you can apply it to your tag code:

for i in range(1,17):
    print "MOTOR_SETTINGS/STAND%d/EN_STAND%d" %(i,i)

To make it even more dynamic, you could pass in the number of motors if you have that value somewhere:

for i in range(1,NumberOfMotors+1):

“Range” basically loops from n to n-1, which is why I added 1 to the number of motors.