Search code examples
plccodesysiec61131-3

IEC61131-3 directly represented variables: data width and datatype


Directly represented variables (DRV) in IEC61131-3 languages include in their "addresses" a data-width specifier: X for 1 bit, B for byte, W for word, D for dword, etc.

Furthermore, when a DRV is declared, a IEC data type is specified, as any variable (BYTE, WORD, INT, REAL...).

I'm not sure about how these things are related. Are they orthogonal or not? Can one define a REAL variable with a W (byte) address? What would be the expected result?

A book says:

Assigning a data type to a flag or I/O address enables the programming system to check whether the variable is being accessed correctly. For example, a variable declared by AT %QD3 : DINT; cannot be inadvertently accessed with UINT or REAL.

which does not make things clearer for me. Take for example this fragment (recall that W means Word, i.e., 16 bits - and both DINT and REAL correspond to 32 bits)

 X AT %MW3 : DINT;
 Y AT %MD4.1 : DINT;
 Z AT %MD4.1 : REAL; 

The first line maps a 32-bits IEC var to a 16-bits location. Is this legal? would the write/read be equivalent to a "cast" or what?

The other lines declare two 32-bits IEC variables of different type that points to the same address (I guess this should be legal). What is the expected result when reading or writing?


Solution

  • Like everything in PLC world, its all vendor and model specific, unfortunately.

    Siemens compiler would not let you declare Real address with bit component like MD4.1, it allowed only MD4 and data length had to be double word, MB4 was not allowed.

    Reading would not be equivalent to cast. For example you declare MW2 as integer and copy some value there. PLC stores integer as, lets say in twos complement format. Later in program you read MD2 as real. The PLC does not try to convert integer to real, it just blindly reads the bytes and treats it as real regardless what was saved there or what was declared there. There was no automatic casting.

    This is how things worked in Siemens S7 plc-s. But you have to be very careful since each vendor does things its own way.