Siemens data format


This chapter should be inserted into "Siemens communications", but due to many emails received about “strange behaviors” about data exchanging, I decided to highlight it.

The PLC internal data format is BIG-Endian, i.e. the complex variables (which size is greater than 1 byte) are stored in memory starting from the most significant byte up to least significant one.

The PC internal data format, except for some architectures such as SUN Sparc, Mips and 68000 system based, is LITTLE-Endian.

This is how the DWORD 0x2F11214C is stored into the PLC.






And this is how the same DWORD is stored into the PC.






This is how the WORD 0x2C10 is stored into the PLC.




And this is how the same WORD is stored into the PC.




This means that every variable must be rotated before inserting it into the PLC and, using the same criteria, must be rotated after it is read from the PLC.

Helper classes


Starting from version 1.3.0 you do not need to worry about the endian convention.

You will find a class and/or a set of functions that allow you to insert/extract a complex variable from a bytebuffer adjusting its format as well.

These classes are not built into the binary library but you will find them into the wrapper of your programming language.

The class name is S7 for .NET/Pascal/Moka7, for C++ language you will find a set of functions instead.

S7 is a static class, i.e. you don’t need to create it.

The idea behind this class is that you can read a byte buffer from a PLC using a single Read then extract the vars from it knowing their offset.

Please refer to the source code of the class to see its methods. With them you can insert/extract:

·         BITS

·         WORD (unsigned 16 bit integer)

·         INT (signed 16 bit integer)

·         DWORD (unsigned 32 bit integer)

·         DINT (signed 32 bit integer)

·         REAL (32 bit floating point number)

·         S7 Strings

·         S7 Array of char

The class is able also to read/write a S7 DATE_AND_TIME variable mapping it into the native language format and vice-versa:

·         S7 DT <-> .NET DateTime

·         S7 DT <-> Pascal TDateTime

·         S7 DT <-> C++ tm struct


With this C# code snippet, a 256 bytes buffer is read from the CPU and the REAL DB25.DD4 is extracted from the buffer.


byte[] mybuffer = new byte[256];
Client.DBRead(25, 0, 256, mybuffer);
Single MyFloat = S7.GetRealAt(mybuffer, 4);