Conversions
You can use the Conversions options to convert a variable of one data type into a variable of a different data type. For example, you can convert a boolean value into a byte value by using the BOOL_TO_BYTE conversion.
To access the conversions on a Function Block Diagram or Ladder Diagram:
- Display the diagram in Design mode.
- Right-click on the background of the diagram to display a context sensitive menu.
- Select the Conversions option to display a further menu.
- Select the option for the type of value that you want to convert. For example, if you want to convert a string value into a byte value, select the String option.
A further menu is displayed. - Select the option that corresponds to the type of value into which the value will be converted. For example, if you want to convert a string into a byte, select the STRING_TO_BYTE option.
A conversion box is added to the diagram. - Connect the value that is to be converted to the connection box on the left of the conversion box. The value will be converted and will be the output of the connection box. You can connect a write tag to the right connection box so that the converted value is written to the database, or you can connect the right connection box to a function block.
NOTE: If a Logic program is invalid due to an incorrect data type, a message is displayed in the compiler display at the bottom of the Logic diagram. The message indicates the type of data that is expected (required) and the input or output at which an incorrect data type was found. You can use this information to determine where conversions are needed and where different types of conversion are required.
To use conversions in ST programs, simply reference the conversion by its name within an expression. The names the conversions are same as the option names on the context sensitive menus, for example, BOOL_TO_BYTE, STRING_TO_DINT, INT_TO_SINT etc.
The format for using a conversion in ST is:
<Name of Target Variable> := <Conversion type> (<Name of Source Variable>);
where:
- <Name of Target Variable> is the name of the variable that is to be affected by the result of the conversion (the converted value).
- <Conversion type> is the name of the conversion, such as BYTE_TO_BOOL.
- <Name of Source Variable> is the name of the variable that has the source value that is the input for the conversion.
In each case, do not enter the angle brackets < >.
NOTE: When using conversions in ST programs, you need to enter the name of the conversion in its full form (as shown in the Conversion options on the context sensitive menu for Function Block Diagrams and Ladder Diagrams). For example, for date and time conversions, you have to enter DATE_AND_TIME and not use the abbreviated form (DT). The only exceptions to this rule are DT_TO_DATE and DT_TO_TOD.