How to Code Your Way to an Error-Free IO Assignment
Maxim Krylov
Co-founder of Kiptr.com
This is the second part of an article about L5X file format.
Let's perform a single task - generate the IO assignment.

We usually prepare the list of IO points in the form of a table for every project. A very simplified variant will look something like this:
It is not such a big deal to assign the IOs by hand, right?

We will do it like that:
Oops! I made a mistake!
Rung 4 moves the second channel to the same tag that is already used in rung 3.

Well, things happen. We have all done it.
But now, imagine if you have hundreds of IO points. It is insane to do it by hand, and I can only imagine the number of such "oops"… The solution is to automate the creation of logic.
Machines don't make mistakes; only humans do.
So, what can we do with our assignment list in Excel? We can generate the rung logic from what we already have. Let's get the DI assignment and check what we want to achieve. Double click on the rung number, and we see our logic:
Our goal is to get the line of text: "XIC(Local:1:I.Data.0)OTE(DI_tag_1);".
In Excel we can do it with a formula:
Cell E3 will have the formula: ="XIC(Local:1:I.Data."&B3&")OTE("&C3&");". Now we need to add everything needed to inject this into L5X file.
You remember the structure from the example above? Here is the rung once again:

<Rung Number="0" Type="N">
    <Text>
        <![CDATA[XIC(bit1)OTE(bit2);]]>
    </Text>
</Rung>

The good thing about XML is – we don't have to keep the formatting, so we can simply convert the whole structure to a line:

<Rung Number="0" Type="N"><Text><![CDATA[XIC(bit1)OTE(bit2);]]></Text></Rung>
If we break it down to constants (yellow) and variables (blue):
Or as a formula:
Some notes:
  • In Excel, "&" is used with text, and "+" is used with numbers.
  • We need to use double quotes inside the formula, so we will use CHAR(34) instead of explicitly entering it in the text. There are other ways around this limitation, but I consider this one the most convenient.
  • Our First Tag Variable will be "Local:1:I.Data." &B3, which is a combination of variable and text, but for other IOs, it will change. So, we will call it a variable
Let's try it in Excel:
Now we can simply drag down the cell, and Excel will apply this formula to other lines.
Let's do similar operations with the other IO types in our list. Here is the list of formulas I came up with:

DI: ="<Rung Number=" & CHAR(34) & A3 & CHAR(34) & " Type=" & CHAR(34) &"N"& CHAR(34) &"><Text><![CDATA[XIC(Local:1:I.Data." & B3 & ")OTE(" & C3 & ");]]></Text></Rung>"
DO: ="<Rung Number=" & CHAR(34) & A9 & CHAR(34) & " Type=" & CHAR(34) &"N"& CHAR(34) &"><Text><![CDATA[XIC(" & C9 & ")OTE(Local:2:O.Data." & B9 & ");]]></Text></Rung>"
AI: ="<Rung Number=" & CHAR(34) & A15 & CHAR(34) & " Type=" & CHAR(34) &"N"& CHAR(34) &"><Text><![CDATA[MOV(Local:3:I.Ch0" & B15 & "Data," & C15 & ");]]></Text></Rung>"
AO: ="<Rung Number=" & CHAR(34) & A21 & CHAR(34) & " Type=" & CHAR(34) &"N"& CHAR(34) &"><Text><![CDATA[MOV(" & C21 & ",Local:4:O.Ch" & B21 & "Data);]]></Text></Rung>"

Now you can copy your text from the cells with formulas in them and paste into your L5X:
Notes:
  • This operation generates the ladder logic but does not create the tags for you. If you import the L5X into Studio 5000, you will likely see the code, but all the tags will be shown as not existing. You can create the tags in L5X, though. First, check the structure of the tag in the example above.
  • The shown example references the AI channels with the prefix (Local:3:I.Ch0x) and the AO without the prefix (Local:4:O.Chx). This is due to Allen Bradley's way of handling different IO cards differently. So, in this example, your prefix will have to go away once you get to channel 10 or higher.
So, this way, you can create some IO assignments, handle PLC-PLC communications from a comm table, create alarm tags, etc. But still, it is quite a significant amount of manual work.


Can we go fancier? Can we make it look like magic?
Yes, we can! In Part 3 we will talk about scripting!