Example 1, Example 2 – Rockwell Automation Logix5000 Controllers Structured Text Programming Manual User Manual

Page 30

Advertising
background image

30

Publication 1756-PM007D-EN-P - November 2012

Chapter 1 Program Structured Text

Example 1:

Example 2:

If you want this

Enter this structured text

Clear bits 0 - 31 in an array of BOOLs:

1. Initialize the subscript tag to 0.

2. Clear array[ subscript ] . For example, when subscript

= 5, clear array[5].

3. Add 1 to subscript.

4. If subscript is £ to 31, repeat 2 and 3.

Otherwise, stop.

For subscript:=0 to 31 by 1 do

array[subscript] := 0;

End_for;

If you want this

Enter this structured text

A user-defined data type (structure) stores this information
about an item in your inventory:

Barcode ID of the item (string data type)

Quantity in stock of the item (DINT data type)

An array of the above structure contains an element for each
different item in your inventory. You want to search the array
for a specific product (use its bar code) and determine the
quantity that is in stock.

1. Get the size (number of items) of the Inventory array and

store the result in Inventory_Items (DINT tag).

2. Initialize the position tag to 0.

3. If Barcode matches the ID of an item in the array, then:

a. Set the Quantity tag = Inventory[position].Qty. This

produces the quantity in stock of the item.

b. Stop.

Barcode is a string tag that stores the bar code of the item
for which you are searching. For example, when position =
5, compare Barcode to Inventory[5].ID.

4. Add 1 to position.

5. If position is £ to (Inventory_Items -1), repeat 3 and 4.

Since element numbers start at 0, the last element is 1
less than the number of elements in the array.

Otherwise, stop.

SIZE(Inventory,0,Inventory_Items);

For position:=0 to Inventory_Items - 1 do

If Barcode = Inventory[position].ID then

Quantity := Inventory[position].Qty;

Exit;

End_if;

End_for;

Advertising