Type
| Function |
Description
| The overloaded function TO_WORD converts an input value of various data types into an output value of the data type WORD (see description of the input for the supported data types). |
Parameters
| Input
IN
Data type: | The function is available for the following input data types:
- BOOL, BYTE, DWORD, LWORD
- Integers: SINT, INT, DINT, LINT, USINT, UINT, UDINT, ULINT
- STRING
|
Output
OUT
|
Conversion rules
|
- The conversion is done by copying the input value bitwise to the output value, i.e., a binary transfer from the input data type to the output data type is performed.
-
Input size > output size: If the input data type has a bigger size than the WORD output, only the bits 0 to 15 of the input value are considered.
-
Input size < output size: If the input data type is smaller than the WORD output, the "unused" bits of the WORD are set to FALSE (e.g. bits 8 to 15 for BYTE_TO_WORD or SINT_TO_WORD).
- If the range and precision of the input data type can be mapped to the range and precision of the output type, an implicit type conversion can be performed instead of using this function.
For the TO_WORD function, this applies to BOOL and BYTE inputs.
STRING conversion
- The string value has to be a valid bit literal of the output data type, otherwise the function returns the value 0x00 and the runtime error 'Format string failure' occurs.
- Additional characters except than whitespaces after or before the string value are not permitted. The function returns the value 0 and generates a 'Format String Failure' if the string value contains unallowed characters.
- All leading and trailing whitespaces in the input string are ignored in the return value.ASCII codes regarded as whitespaces:
Character | Value |
Horizontal tab | 9 (09h) |
Line feed | 10 (0Ah) |
Vertical tab | 11 (0Bh) |
Form feed | 12 (0Ch) |
Carriage return | 13 (0Dh) |
Blank | 32 (20h) |
- If the input value is out of the range for the output data type, the overflow value is returned and the runtime error 'Format string failure' occurs (see the note below).
Note
Controller stop on string error: The controller can be automatically stopped when a string error occurs. This allows you to locate the error in your project. To enable the automatic stop of the controller in case of a string error, activate the button in the Cockpit toolbar of the controller. |
|
Error behavior
| Error behavior
The following table shows the errors which may occur while executing the function and the value returned by the function in the corresponding error case. As described, the value returned by the function in the error case depends on the value of the input (for example, if the input value is no valid bit literal, the function returns the value 0x00).
Error case | Output value |
Overflow: input byte size too large | Overflow value |
String no valid bit literal or int number literal (with whitespaces) | 0x00 |
String: bit literal overflow | Overflow value |
|
Examples
| Examples in ST
out := TO_WORD(INT#32767); (* results in 16#FFFF *)
out := TO_WORD(DINT#65536); (* results in 16#0000 *)
out := TO_WORD(INT#-1); (* results in 16#FFFF *)
out := TO_WORD(SINT#-16); (* results in 16#00F0 *)
out := TO_WORD(DWORD#16#0000ABCD00); (* results in 16#CD00 *)
out := TO_WORD('WORD#16#ABCC'); (* results in 16#ABCC *)
|
Additional information
| EN/ENO behavior
The POU is only executed if TRUE is applied to the EN input. If EN = FALSE, the POU is not executed and ENO = FALSE indicates the inactivity.
If any of the errors listed under "Error behavior" occurs while executing the function, ENO is set to FALSE.
|