HMI/PLC Programming: How to implement a programming loop?

HMI script programming can do FOR NEXT loop and WHILE loop. Use GOTO instruction to break out of the loop.

Here’s a sample of loop on HMI to search the first ON state from M500 to M508, then when found, copy the value at the corresponding position of D500-D508 over.

dim i as integer
dim tmp as integer
dim newaddr as string
dim newaddr_d as string

dim interval as integer

for i = 1 to 8

‘ Check state of M500 to M508, if 1 is met, then read the corresponding value from D500 to D508, then break out

newaddr = NewNoAddr(“@B_M500”,i)
newaddr_d = NewNoAddr(“@W_D500”,i)

tmp = ReadAddr(newaddr)

if tmp = 1 then
interval = ReadAddr(newaddr_d)
goto got_interval
endif

next

got_interval:

When using the GOTO label, a big attention needs to be made to make sure the labels are globally unique. Even in different scripts, they can’t be using the same label.

The same can be done in PLC with the following snippet: