loops found during compilation in CBM Program.
This error comes up when I download the program to CPU how to solve it?
Tanks.
Answers
Inspect the Loop try to read the notepad file and fix the loop in the logic which it is developed.
Sometimes you have to solve this through the attribute "nosort", else you need to rewrite the code.
There is no magically simple solution. You need to understand code sorting so that you can understand where the problem comes from. Then you can fix your code correctly and reliably.
A "Code Loop" occurs when the Program Compiler cannot determine the correct execution order of your code. Yes - you read that correctly ... The COMPILER - Not You - determines what order to execute your code in. It is as mad as it sounds.
The basic rule for code sorting is that All variables must be WRITTEN before they are READ. This means that the following code ( as far as the ABB Compiler is concerned ) is not in the "correct" execution order .....
Result := MyVariable ;
MyVariable := YourVariable;
"MyVariable" is being Read before it was set. Code sorting sees this as an error. ( The fact that this is common practice in PLC programming to preserve the result from the last scan seems to have been lost on the folks who came up with this idea ).
But of course, code sorting cannot change the execution order of every line of code. Code sorting works on determining the execution order of the "Code Blocks". So if you Have the following code ....
Block_1
Result := MyVariable;
Block_2
MyVariable := YourVariable;
The Code sorting will always execute Code Block_2 *BEFORE* Code Block_1. No Matter where you place the code blocks in your program. You cannot control this - it will always happen this way. The compiler does this optimisation silently. If you intended to execute the program statements in the order (Block_1)(Block_2) then you cant. You need to write your code differently.
Code Sorting problems come when you write code like this ....
Block_1
YourVariable := MyVariable;
Block_2
MyVariable := YourVariable;
Now the Compiler cannot decide which Code Block should be executed first. So it throws a code loop error.
The "Easiest" way to solve this error is to declare "MyVariable" as a "STATE" variable and specifically use the "old" - ie last scan State ...
Block_1
YourVariable := MyVariable:OLD;
Block_2
MyVariable := YourVariable;
Now the compiler knows that you want the last scan value of MyVariable in Block_1 , so Block_1 should be executed first. ( Of course, in this case the code is rubbish and cant actually do anything useful in the real world )
The way that many people will tell you to solve the problem is to declare MyVariable and YourVariable as "NoSort". This is dangerous ! If you declare a variable as Nosort, then the compiler will execute the code blocks in an UNKNOWN order. And this order can change when you re-compile the code. Sometimes this does not matter - like in the example above. But sometimes it does because declaring just one variable as Nosort tells the compiler to ignore sorting for ALL of the program in the code block - which includes variables that otherwise can and should be sorted correctly.
So if you have a code loop error in your program ....
- 90% of "Code Loop" errors are actually logic errors. When you first write the code you get lops because you're writing to the wrong variable, made a dumb mistake etc. Fix and debug your code and most of these errors go away.
- You can solve almost all of the remaining code loop errors by understanding WHAT is causing the loop. Usually you have to move one or two lines of code from one code block into another code block. This allows the compiler to code sort correctly.
- You can use a STATE variable to control properly when you want to read the Last Scan value instead of the current scan.
- Finally - there are the 1 or 2 % of code loop errors that happen "just because". The compiler is not perfect and sometimes it cannot correctly optimize the execution order. In some cases the code loop should be fixed inside a locked library that you have no access to change. In this case, you typically have no choice other than use "NoSort". But be warned - this is a last resort and you must understand the risk that you may break the execution order of the program code if used incorrectly.
To find the code loop errors, open the "Inspect" window from the compiler output. There is information in the Control Builder Programming manuals on how to find and identify the code loops. But basically, You are looking for places where the Variables are being read, before they are written.
Add new comment