The IEC 61131-3 specification is nuts

Join Date
Apr 2002
Location
No income tax, no capital gains tax. Freedom!
Posts
8,389
Before I start slamming the IEC specification I want to show the program that 'wound up my integrator'. I wanted to write a quick and dirty random number generator. I found this algorithm. This should be easy.

http://www.codeproject.com/KB/recipes/SimpleRNG.aspx

Code:
private static uint GetUint()
{
    m_z = 36969 * (m_z & 65535) + (m_z >> 16);
    m_w = 18000 * (m_w & 65535) + (m_w >> 16);
    return (m_z << 16) + m_w;
}

and

public static double GetUniform()
{
    // 0 <= u <= 2^32
    uint u = GetUint();
    // The magic number below is 1/(2^32 + 2).
    // The result is strictly between 0 and 1.
    return (u + 1) * 2.328306435454494e-10;
}
This looks like java code to me.
I wanted something 'lean and mean' because it is to be used on a motion controller for simulating varying readings etc.

The code translated into two steps

Code:
(* Step 0 Initialize once using timers and clocks *)
(* %MD20.2 is a register that the last scan time *)
m_z:=DINT_TO_DWORD(REAL_TO_DINT(%MD20.2*1000000000.0));
m_w:=DINT_TO_DWORD(_SysTicks);

(* Step 1 Run this when a new random number is required  *)
m_z:=DINT_TO_DWORD(36969*DWORD_TO_DINT(m_z AND 16#FFFF)+DWORD_TO_DINT(SHR(m_z,16)));
m_w:=DINT_TO_DWORD(18000*DWORD_TO_DINT(m_w AND 16#FFFF)+DWORD_TO_DINT(SHR(m_w,16)));
u :=  DWORD_TO_DINT( SHL(m_z,16))+DWORD_TO_DINT(m_w);
rnd:=DINT_TO_REAL(u+1)*2.328306435454494E-10+0.5;
The code works well and runs in 7.2 microseconds but look at all the type conversions!!! I spent as much time converting data types and swearing at the need for this bs as doing useful work. This is nuts. Does the ST code look easier than the Java code? I think Rockwell has the right idea and that conversions should be automatic.

I can't believe that people really want to do all the type conversions when they aren't necessary. No code is generated for the DWORD_TO_DINT or DINT_TO_DWORD or DWORD_TO_DINT so what is the point. However, one opcode is generated for the REAL conversions. Also, they could have used a shorter way of saying DINT_TO_DWORD. How about copying from others instead of making up your own crappy specifications just to be different? Why not do it like in C or java where DINT_TO_DWORD(number) is simply (DWORD)number.

Comments. I like comments. I like C or java comments.
I want to say

I=I+1 // increment I

instead of

I:=I+1 (* increment I *)

If is much easier to hit the same key twice than to (* *).
This is nuts. I don't see how (* *) improves on the tried and true //.

You can see I much prefer using = as an assignment and == as an equality check. I use assignments much more often than I use equality checks.

I have already specified that // will be allowed on our product. I must have // for line comments.

Hex numbers. What is wrong with 0xFFFF?. Again IEC had to make up some thing different. We already allow 0xFFFF as well as 16#FFFF on our product.

Standards limit creativity and searches for better ways of doing things especially when the standards are so flawed and yet so many think IEC is the only way to go.

Is there anybody here that seriously thinks the IEC specification is only way to go? I would prefer a C# or Java or even C++ with the pointers and the need for dynamic memory allocation.

I just wanted to stir thing up and by slamming the the IEC standard and the lemmings. We feel pressure to conform with the IEC specification but I don't think it is the right thing to do because it is flawed. I can provide examples if required. This lemming sees the cliff and feels like he is being pushed along by all the other lemmings. I also wanted point out there is a good way to generate pseudo random numbers is very quick and random enough.
 
Is there anybody here that seriously thinks the IEC specification is only way to go?

Certainly not me. You have mentioned some of the things that had me peeved about IEC standard - type casting, hex literals format( what's wrong with HFFFF or FFFFH?), comments... BTW, CoDeSys ver. 3 has already introduced // as alternative comment format, as an extension to the standard.

It will not change. They don't really care. The standard is not for us, users - it is the other way around.
 
Certainly not me. You have mentioned some of the things that had me peeved about IEC standard - type casting, hex literals format( what's wrong with HFFFF or FFFFH?), comments... BTW, CoDeSys ver. 3 has already introduced // as alternative comment format, as an extension to the standard.
So much for standards.

It will not change. They don't really care. The standard is not for us, users - it is the other way around.
Who are 'they'. I think of myself more as a manufacturer than a user. I program PLCs when I need to make an example program that shows how to communicate with our controllers. Sometimes in the field I help out but that is it. That example above was written on a motion controller.

Look at the PLC manufacturers, who has a true IEC programming package? Not Rockwell, not Siemens, I have heard that Schneider does. I don't think the traditional PLC manufacturers are forcing the IEC standard on us. So who are 'they'
 
So who are 'they'
Whoever creates the standard, I guess? Or whoever enforces it or at least is trying to enforce.

Some extentions CoDeSys has thrown into v.3 make a lot of sense and I wish they would eventually become standard. There is still alot of things left to be desired though. A UNION construct would be very useful - many times I have come across a situation where accessing the same memory location as a word or as a pair of bytes or as an array of bits would help a lot. I am still surprised that a PLC standard does not have a way to index through individual bits or to create data structures containing bits (a BOOL variable, as everyone knows, takes a whole byte in memory).

And I am still stunned by the fact that "local" variables are not really local but can be accessed from outside of a POU where they are declared - that is totally opposite to everything I've seen so far and to any notion of encapsulation.
 
About the mass of type conversions:
Typecasting is a total nuisance, until you realise it makes you fix the problems before you create them. I only rarely have to use type conversions. In your example with the random number generator, you have a lot of type conversions indeed. But for mingling bits about that way, it is more noticable that it is at all possible in a PLC language, rather than it is tedious with all these type conversions.

About hex format. I think the IEC way is a lot clearer than the java way.
Hex is 0xFFFF in Java, 16#FFFF in IEC.
Octal is O7777 in Java, 8#7777 in IEC. It is way too easy to make a typo by using the letter "O" to specify an Octal value.
Quote from a Java glossay:
"Be careful! It is very common to specify an octal literal by mistake in Java and scratch your head for hours trying to figure out the problem. "
Binary is not possible in Java, is 2#10010001 in IEC.
Quote from a Java glossary:
"Unfortunately, there is no way in Java to include binary literals in programs other than by encoding them in hex or the more old fashioned octal. "
All in all I think IEC handles number formats better than Java.

About // comments: Yes, they should be allowed. In Siemens SCL I use it all the time.

Besides, I believe IEC traces its roots to Siemens S5, so I think it preceedes Java. So, it is not fair to blame IEC for not following the 'standard' of Java.

Ladder Logic said:
A UNION construct would be very useful - many times I have come across a situation where accessing the same memory location as a word or as a pair of bytes or as an array of bits would help a lot.
Can't you use AT for that ? I use it myself for exact the same reason you state. Granted, it isnt possible for all types of data access.
 
Just so that I dont start a flame war:
Java started in 1991. 1st version of IEC-1131 was released in 1992.
So Java preceedes IEC61131-3.

But I am certain that IEC was started before Java was released, and long before anyone knew Java would become widespread, so it is still unfair to blame IEC for not following Java.
 
Can't you use AT for that ? I use it myself for exact the same reason you state. Granted, it isnt possible for all types of data access.

I certainly can and that is the way I ended up doing it.

But I don't consider code "clean" if it has to refer to a specific memory location without a real need for it. It is, in essence, a hack, a way around the system's limitation.

A lot of things have changed since 1991 - both in the world of PLC programming and in the world of "real" :) programming. The influence of the archaic DOS-based Siemens approach is all too visible in 61131 standard.
 
Who 'they' are, politics and marketing in the control world.

Just so that I dont start a flame war:
Java started in 1991. 1st version of IEC-1131 was released in 1992.
So Java preceedes IEC61131-3.

But I am certain that IEC was started before Java was released, and long before anyone knew Java would become widespread, so it is still unfair to blame IEC for not following Java.

It doesn't make any difference. C, C++ and Pascal existed long before either. A subset of C without pointers would be good enough.

My main topic is why some people insist using a flawed specification and who is pushing the flawed specification and who is promoting it.

The International Fluid Power Society document for Electronic Certification has said that a controller is a microprocessor that can run IEC code. That means DSP hydraulic motion controllers that don't execute IEC code are not controllers in the view of the IFPS.
http://www.ifps.org/Certification/ElectronicCertification.htm
Lock at the topic outline for the section on controllers. There is the definition section where they define what a controller is. I don't mind that they use the IEC specification for PLC programming examples but to say that a controller must run IEC code is crazy. Apparently the ability to actually control a hydraulic or pneumatic actuator doesn't count for anything. This is nuts.

I will be blunt. I think there there are representatives of companies on these committees that use them for marketing purposes. That is who 'they' are. If you want to score points marketing points against Rockwell then just define the Control Logix as being a non controller because it isn't programmable using the IEC specification. Think of the hydraulic guys that will be 'contaminated' with this point of view.

BTW, I reviewed the specifications for the IFPS Electronic Specification. When I saw the definition for what a controller is I objected and refused to contribute to the specification until that sentence was removed. So what reason would the IFPS Electronic Specialist committee have for specifying that a controller is a microprocessor that runs IEC code? I think the IEC is just hype and a marketing tool. The fact you can use it to program is incidental. Look at the who sells IEC software. If these companies can pack committees to get the IEC specification made part of the standard then that is a way of scoring marketing points relative to the established PLC and motion controller companies.

I just think that you guys need to know there is a lot of politics involved in these standards and committee wars.

I still think PLCs and motion controllers of today are passing fads. The are just tools. The IFPS electronic specialist should be taught things that are true know and will be in the future. That is what I call forever knowledge. There are plenty of things I saw in the IFPS Electronics Certification that are wrong, useless or need improvement. 10 years from now we will be certainly selling a different and better motion controller. I am sure the PLC companies will be selling better PLCs if they aren't afraid of ignoring the IEC specifications. The IEC specification 'freezes' innovation right here.
 
JesperMP said:
Just so that I dont start a flame war:
Java started in 1991. 1st version of IEC-1131 was released in 1992.
So Java preceedes IEC61131-3.

But I am certain that IEC was started before Java was released, and long before anyone knew Java would become widespread, so it is still unfair to blame IEC for not following Java.

It doesn't make any difference. C, C++ and Pascal existed long before either. A subset of C without pointers would be good enough.
IEC ST is more in family with Pascal than C, and I think that it was OK to modify Pascal somewhat.
For example hex is $FFFF in Pascal, octal is &7777, binary is %11111111. I think that 16#FFFF, 8#7777 and 2#1111_1111 is more readily understandable.

edit: In short, in IEC ST everything is 'spelled out' more than in C and Java.
Hence the long DINT_TO_REAL, 16#FFFF etc. rather than the more 'compact' notation in C and Java.
A nuisance, yes. But maybe for the better.
 
Last edited:
Peter Nachtwey said:
My main topic is why some people insist using a flawed specification and who is pushing the flawed specification and who is promoting it.
I have no idea "who" it is. But I can well imagine that it consists of some well-meaning people plus some not-so-well-meaning corporations that just want to obfuscate everything.
For what it has become, I find that it is 'not so bad'. The alternative would have been that all the PLC vendors pursue completely different paths.
What we have now is a set of languages that are quite similar if not exactly similar, and a minimal level of functionality. This minimal level of functionality is the most important to me. Without IEC ST, some of the things I do today would be most painful.
 
For once, I would much prefer the C way of defining variables rather than Pascal's. Something like this:

Code:
bool bStartLatch, bStartPB @ %IX0.0, bStopLatch;
int nCycleCount := 0, nBatchCount, anValues[20];

and so on. No VAR... END_VAR thing.
 
You guys are concentrating too much on the flaws and not who 'they' are...

that push the standard for marketing reasons.

After my last post I did some research.
http://www.google.com/url?sa=t&sour...ojpUpfSEZ0jtFaZTQ&sig2=6DEuuY5SmeZajr4IpllvwA
It the IFPS Electronic Certification committee meets some where around Minneappolis or Eden Praire where Eaton is.

Perhaps thing would be different if the committee met here in Vancouver, WA instead in Minneapolis. That would make it costly for Eaton to have someone attend the meetings and cheaper for us.

Look at the article. Now we know who some of the 'they's are.
I have been told that the link may need to be copied to the URL bar.
 

Similar Topics

Hello, I have a small programming task that I need help solving. I have to: * Create an analog input (4-20v)and a digital output * The analog...
Replies
45
Views
25,092
I was poking through the Stackoverflow PLC Tag when I saw this answer to a question about converting a Real to a DWord and back again. What...
Replies
8
Views
2,906
I am looking for some expert advice. I have always made function blocks both with inputs and outputs and without inputs or outputs. I make the...
Replies
21
Views
6,772
Hello and happy new year! I would like to further understand who needs to design by this standard and is it a requirement for everyone or only...
Replies
3
Views
1,788
So Ladder is easy to read graphically, most everything can be coded in ladder, and it represents wiring diagrams the closest. SCL and ST are good...
Replies
5
Views
1,735
Back
Top Bottom