Conversion routines
The buffer will contain a copy of the PLC memory area. A further complication results from
the fact that Siemens PLCs store multibyte values beginning with the most significant byte
(big endian) while Intel based PCs store the least significant byte first (little endian).
It is not possible to convert the byte order inside daveReadBytes() or daveWriteBytes()
because the start position of each multibyte value is not known then.
As you are free to place such values at arbitrary byte addresses in your PLC program,
the same adresses must in turn be used to retrieve values from the copy of PLC memory.
If you have a data block DB2 with the following layout:
DBB 0 | BYTE |
DBD 1 | DWORD |
DBD 5 | REAL |
You can retrieve the single values in three ways:
1. From the intenal buffer. After a successful call, an internal pointer points to the 1st byte.
Now use daveGetU8(dc) to get the value of the first byte as an unsigned value. The internal pointer
is incremented by 1, now pointing to the copy of DBD1. Use daveGetS32(dc) to get the value of the
of the next 4 bytes as a signed value. The internal pointer is incremented by 4, now pointing to
the copy of DBD5. Use daveGetFloat(dc) to get the value of the next 4 bytes as a single precision
float.
2. From the internal buffer, specifying a position. Use daveGetU8at(dc,0) to get the value of the
first byte as an unsigned value. Next use daveGetS32at(dc,1) to get the value of the 4 bytes
starting at 1 as a signed value. Finally, use daveGetFloatat(dc,5) to get the value of the 4 bytes
starting at 5 as a single precision float. You may perform these operation in any order and also
repeat them.
3. From a user buffer. Use daveGetU8from(buffer) to get the value of the first byte as an
unsigned value. Use daveGetS32from(buffer+1) to get the value of the 4 bytes at buffer+1, i.e.
DBD 1, as a signed value. Use daveGetFloatat(nbuffer+5) to get the value of the 4 bytes starting
at buffer+5 as a single precision float, i.e. DBD5.
The conversion functions are named after the bit length an signedness they assume:
int buffer | int buffer+pos | buffer pointer | size | signed | C-return type | Pascal ret type |
daveGetU8 | daveGetU8at | daveGetU8from | 8 bit=1 byte | no | int | longint |
daveGetS8 | daveGetS8at | daveGetS8from | 8 bit=1 byte | yes | int | longint |
daveGetU16 | daveGetU16at | daveGetU16from | 16 bit=2 byte | no | int | longint |
daveGetS16 | daveGetS16at | daveGetS16from | 16 bit=2 byte | yes | int | longint |
daveGetU32 | daveGetU32at | daveGetU32from | 32 bit=4 byte | no | unsigned int | longint |
daveGetS32 | daveGetS32at | daveGetS32from | 32 bit=4 byte | yes | int | longint |
daveGetFloat | daveGetFloatat | daveGetFloatfrom | 32 bit=4 byte | yes | float | single |
There had been an older set of those functions named after data types, e.g.
daveGetDWORD(). Those functions should not be used any more, as there names might be
misunderstnadable between PLC and C or other programming languages. They are still supported
for compatibility with older versions. These functions had been inlined in earlier versions
but are now not inlined by default, because other languages than C cannot make use of inline
definitions in a C header file.