This time, I will introduce the functions of Serialize and Deserialize. Siemens is pretty good at programming with structs and arrays. If you use these two instructions, you can transfer the variables of the structure to the array at once (Serialize) and transfer the array to the structure at once (Deserialize). There are various ways to use it, for example, when communicating with other FieldBus.
Setup
Let’s create FB first, then create structured data statData1 for transfer. (PLC user data type can also be used,
そしてもうひとつを複製します。
最後は転送用の配列を作ります。ここは長さをきにせずに多めに定義します。
Serialize
Serialize is a Function for transferring data to an array.
- SRC_VARIABLE
- Any Data types of Variables
- Source
- POS:
- DINT
- The first index of your array destination
- RET_VAL
- INT
- The result of Function
- DEST_ARRAY
- The Destination of array.
Implementation
Here we transfer the data of statData1 to the array of barray. Since pos is 0, data will be stored from 0 in barray.
Result
Since w1 is 1234, 0 and 1 in the barray are 12 and 34.
For b16, if 0 and 2 are on, 1+4=5, so Byte6 is 5.
Deserialize
Deserialize is a Function for transferring array data to variables of other Type.
- SRC_VARIABLE
- Array
- The source data
- POS:
- DINT
- This is the index of the element in which the destination array stores data first.
- RET_VAL
- INT
- The result of Function
- DEST_ARRAY
- The Destination
Implementation
Here we transfer the data from the barray array to statData2. Since pos is 0, data will be transferred from 0 of the barray.
Result
As you can see, each variable of statData2 is transferred cleanly from the array.