Four Methods to Convert REAL Data Type to Byte Array in Siemens TIA Portal PLC

“The Path of an Excellent Engineer in Three Stages”

1. Basic Stage: TIA Portal PLC Technology.

2. Intermediate Stage: TIA Portal and C# Advanced Language Technology.

3. Advanced Stage: Standardized Programming Methods for PLCs.

The Path of Standardization!

Four Methods to Convert REAL Data Type to Byte Array in Siemens TIA Portal PLC

Editor’s Note: A friend from the TIA Portal community asked: How to convert REAL to 4 bytes in TIA Portal? This question is universal, especially regarding the understanding of data types. I shared it in 15 TIA Portal technical groups for discussion, and a friend provided four methods worth learning, which I share with everyone.

Method 1: Using POKE and PEEK_BYTE in SCL Statements

Define a REAL data type variable in a standard-access DB data block and use the following conversion SCL program to implement the conversion: with a byte array of length 4. Taking the floating-point number 3.14 as an example, its corresponding hexadecimal number is: 16#4048_F5C3, defined as follows:

Four Methods to Convert REAL Data Type to Byte Array in Siemens TIA Portal PLC

Use the following conversion SCL program to implement the conversion:

Four Methods to Convert REAL Data Type to Byte Array in Siemens TIA Portal PLC

Note: Cycle is of USint data type. For specific usage of POKE and PEEK_BYTE, please refer to the TIA Portal help.

After executing the program, the conversion result is as follows:

Four Methods to Convert REAL Data Type to Byte Array in Siemens TIA Portal PLC

Method 2: Using Serialization Instruction

Define a floating-point number in the DB block, taking 3.14 as an example, its corresponding hexadecimal number is: 16#4048_F5C3. The program using this instruction for conversion is as follows:

Four Methods to Convert REAL Data Type to Byte Array in Siemens TIA Portal PLC

Note: For specific usage of this instruction, please refer to the TIA Portal help.

After executing the conversion instruction, the byte array will save the four bytes corresponding to the floating-point number as follows:

Four Methods to Convert REAL Data Type to Byte Array in Siemens TIA Portal PLC

Method 3: Using Slice Access Method for Conversion

Define a REAL data type variable and a byte array of length 4 in a DB data block, taking 3.14 as an example, its corresponding hexadecimal number is: 16#4048_F5C3. Use an FC block to encapsulate the conversion, with the FC interface defined as follows:

Four Methods to Convert REAL Data Type to Byte Array in Siemens TIA Portal PLC

The conversion program is as follows:

Four Methods to Convert REAL Data Type to Byte Array in Siemens TIA Portal PLC

Note: When using slice access, be careful with the conversion of high and low words.

After executing the program, the conversion result is as follows:

Four Methods to Convert REAL Data Type to Byte Array in Siemens TIA Portal PLC

Method 4: Using AT Access Method for Conversion

Define a REAL data type variable and a byte array of length 4 in a DB data block, taking 3.14 as an example, its corresponding hexadecimal number is: 16#4048_F5C3. Use an FB block to encapsulate the conversion, with the FB interface defined as follows:

Four Methods to Convert REAL Data Type to Byte Array in Siemens TIA Portal PLC

In this FB, use the MOVE instruction to assign values

Four Methods to Convert REAL Data Type to Byte Array in Siemens TIA Portal PLC

After executing the program, the conversion result is as follows:

Four Methods to Convert REAL Data Type to Byte Array in Siemens TIA Portal PLC

Conclusion: All roads lead to Rome; I hope to find the optimal path!

Walk with the “Excellent Ones”!

Make technical learning simpler!

Four Methods to Convert REAL Data Type to Byte Array in Siemens TIA Portal PLC

Learning methods for PLC standardization: a process from chaos to understanding and insight.

Technical methods for PLC standardization: What technical points does standardization include? What is the relationship between these technical points?

Video explanation of the technical route for PLC standardization: S88 standard, technical architecture, and continuous iteration.

Discussion in TIA Portal groups: Two groups discussed C# and SCL languages, finally clarifying the application of SCL language.

The value of group communication: Real feedback from friends in the Siemens elite group over 6 years.

Book friends’ appointment: Release of three major supporting resources for TIA Portal PLC books.

Join TIA Portal friends and meet excellent engineers.

Four Methods to Convert REAL Data Type to Byte Array in Siemens TIA Portal PLC

Teacher Rui’s books (available for purchase on JD and Dangdang)

Four Methods to Convert REAL Data Type to Byte Array in Siemens TIA Portal PLC

Leave a Comment