TO_LWORD block

Short summary

Name

TO_LWORD

→POU type

→function

Category

IEC-block, Convert, block with internal error diagnostic

Conform to →IEC-standard

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/error.svg currently restricted ; see " Functionality "

and

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/add.svg more possible than defined in IEC-standard; see "Explicit conversion according to IEC-standard"

Graphical interface

images/download/thumbnails/414781128/TO_LWORD-version-1-modificationdate-1534929858176-api-v2.png

Available since

  • version 1.0.0 (for logi.CAD 3) – initial variant

  • version 1.90.0 (for logi.CAD 3) – enhancement: TIME_OF_DAY for IN

  • version 2.0.15 (for library Standard) – enhancement: STRING for IN without character _, internal error diagnostic

  • version 2.0.16 (for library Standard) – enhancement: STRING for IN with character _

  • version 2.0.17 (for library Standard ) – enhancement: CHAR for IN

  • version 3.1.0 (for library Standard) – For inputs of data type REAL, the optimized float variant of the C-runtime is used. Compared to previous versions , it is possible that (marginal) other results are returned within the threshold range of the block.

Functionality

The blocks serves for explicitly converting the value connected to input IN into a value of data type LWORD.

If the connected value is within the range of values overlapping for the data type of the input and for the result value LWORD, the blocks returns this value.

Deviation from IEC-standard

The IEC-standard defines that the conversion for the data types as defined by the IEC-standard is done as a binary transfer . However, this block deviates from the IEC-standard. The values of the data type LREAL, DINT, INT, SINT CHAR are converted deviating from the IEC-standard (see the following description for these data types ).

Observe as well:

  • A value of the data type REAL or LREAL in the range from 0 to the maximum value of ULINT (i.e., 18_446_744_073_709_551_615) is rounded to a ULINT value. Only then the binary transfer of this value to the target data type takes place. The following behavior applies to this (according to the IEC standard):

    • If the source data type is smaller than the target data type, the source value is stored in the rightmost bytes of the target variable and the leftmost bytes are set to zero (= fill up with 0).

    • If the source data type is bigger than the target data type, only the rightmost bytes of the source variable are stored in the target data type.

  • If a negative value of data type REAL or LREAL is connected to the input, the blocks returns the value 0. If a negative value of a different data type is connected, the block returns a value according to the data type of the return value.

  • If a REAL value with more than 6 digits before the decimal point is converted, the result might be inexact. Example: For REAL#4_294_967_290.0, the block returns the value 0. The reason is the number of significant digits of REAL (details: see "Supported data types (in ST) " the note 3 and the note 4 after the table).

  • If a LREAL value with more than 15 digits is converted, the result might be undefined. Example: For LREAL#9223372036854775807.0, the block returns the value -9223372036854775808. The reason is the number of significant digits of LREAL (details: see "Supported data types (in ST)" the note 3 and the note 4 after the table).

  • The following applies to the conversion of DINT, INT and SINT: These data types are not filled up with 0 during the conversion, but they are filled up with bits (0 or 1) depending on the sign. Example : TO_LWORD(SINT#-1) has the result 16#FFFFFFFFFFFFFFFF in logi.CAD 3 instead of the result 16#0000000000000FF (as expected according to the IEC-standard).

  • If converting data types to the TIME data type and vice versa, the conversion is always based on seconds.

  • If a value of data type STRING is connected to the input, the value is converted to a decimal value (base: 10, allowed digits: 0 to 9) and then this decimal value is converted to a value according to the target data type . The following special cases might occur (see under "Example for usage within ST-editor" ) :

    • If the value starts with the character + , with blanks or tabs (= so-called whitespace) and the value is within the range of values for the target data type, the value is converted to a positive decimal value (without these characters). In this case, the output ENO is set to value TRUE .
      Details on this conversion: The character + as prefix for the value is ignored, blanks and tabs (= so-called whitespace) are ignored as well.

    • If the value starts with the character and the value is within the range of values for the target data type, the value is converted analogously to a connected, negative INT value. In this case, the output ENO is set to value TRUE as well.
      Details on this conversion: The upper limit of the target data type is converted to the positive decimal value, this decimal value is increased by the value 1 and then the STRING value (in fact the corresponding decimal value) is subtracted. The result is converted to a value appropriate to the target data type.

    • If the value contains an invalid character, the block returns the value 0. In this case, the output ENO is set to value FALSE .

      Valid characters

      invalid characters

      digits (base: 10, numbers: 0 to 9)
      Example for value with valid character: '7'

      other characters such as letters
      Example for value with invalid character: '12a3', 'a123'

      one character + or as a prefix of the value
      Example for value with valid character: +2 or –3

      repetitions of + or at the beginning of the value, the character + or within the value
      Example for value with invalid character: '++1', '1–3'

      single underscore characters _ within the value
      Example for value with valid character: 1_2 or 1_2_3

      underscore character _ at the beginning or the end of the value, several characters _ behind one another
      Example for value with invalid character: _12, 12_ or 1__2

    • If the STRING value to be converted exceeds the upper limit of the target data type, the block returns the upper limit of the target data type. In this case, the output ENO is set to value FALSE as well.

  • If a value of data type CHAR is connected to the input, the conversion is analogous to a STRING value. But different to the STRING conversion, the CHAR conversion supports single digits digits 0 to 9 only. For all other characters (letters, the characters +, - and _), the block returns the value 0 and the output ENO is set to value FALSE .

Explicit conversion according to IEC-standard

The IEC-standard defines an explicit conversion of the following data types to LWORD:

LREAL, LINT, DINT, INT, SINT, ULINT, UDINT, UINT, USINT, DWORD, WORD, BYTE, BOOL, CHAR

The IEC-standard (Edition 3.0) defines a binary transfer for all data types of the input . But contrary to this, the appropriate value is converted according to the IEC-standard (Edition 2.0) for the following data types of the input.

LREAL , DINT, INT, SINT, CHAR

Details on the logi.CAD 3 behavior of the block can be found in the section "Functionality".

Inputs, return value


Identifier

→Data type

Description

Inputs:

IN

REAL, LREAL, USINT, UINT, UDINT, ULINT, SINT, INT, DINT, LINT, TIME, BOOL, BYTE, WORD, DWORD, LWORD, STRING, CHAR or TIME_OF_DAY

value to be converted

Return value:

LWORD


Input EN and output ENO are available when →calling the block. See "Execution control: EN, ENO" for information on input EN and output ENO.

See:

Internal error diagnostic for block

The block checks the value connected to the input. The output ENO of the block is set to value FALSE (or an equivalent) in the following cases:

  • A STRING value contains an invalid character. (Moreover for this case, the block returns the value 0.)

  • A STRING value exceeds the upper limit of the target data type. (Moreover for this case, the block returns the upper limit of the target data type.)

  • The CHAR value is an invalid character. (Moreover for this case, the block returns the value 0.)

Example for usage within ST-editor

The calculated values are evaluated by using the Assert block .

Example for DINT value
FUNCTION_BLOCK ExampleToLword
VAR
result : LWORD;
END_VAR
 
result := TO_LWORD(IN := DINT#12);
Assert(result = 16#0000_0000_0000_000C);
END_FUNCTION_BLOCK
Example for STRING values
FUNCTION_BLOCK ExampleToLwordString
VAR
resultStr1, resultStr2, resultStr3, resultStr4, resultStr5, resultStr6, resultStr7, resultStr8, resultStr9, resultStr10, resultStr11, resultStr12, resultStr13, resultStr14 : LWORD;
CheckENO1, CheckENO2, CheckENO3, CheckENO4, CheckENO5, CheckENO6, CheckENO7, CheckENO8, CheckENO9, CheckENO10, CheckENO11, CheckENO12, CheckENO13, CheckENO14 : BOOL;
END_VAR
resultStr1 := TO_LWORD(IN:='12', ENO=>CheckENO1);
Assert(resultStr1 = 16#0000_0000_0000_000C);
Assert(CheckENO1 = TRUE);
resultStr2 := TO_LWORD(IN:=' 3', ENO=>CheckENO2);
Assert(resultStr2 = 16#0000_0000_0000_0003);
Assert(CheckENO2 = TRUE);
resultStr3 := TO_LWORD(IN:='+2', ENO=>CheckENO3);
Assert(resultStr3 = 16#0000_0000_0000_0002);
Assert(CheckENO3 = TRUE);
resultStr4 := TO_LWORD(IN:='-3', ENO=>CheckENO4);
Assert(resultStr4 = 16#FFFF_FFFF_FFFF_FFFD);
Assert(CheckENO4 = TRUE);
resultStr5 := TO_LWORD(IN:='1_2', ENO=>CheckENO5);
Assert(resultStr5 = 16#0000_0000_0000_000C);
Assert(CheckENO5 = TRUE);
resultStr6 := TO_LWORD(IN:='1_2_3', ENO=>CheckENO6);
Assert(resultStr6 = 16#0000_0000_0000_007B);
Assert(CheckENO6 = TRUE);
resultStr7 := TO_LWORD(IN:='123a45', ENO=>CheckENO7);
Assert(resultStr7 = 16#0000_0000_0000_0000);
Assert(CheckENO7 = FALSE);
resultStr8 := TO_LWORD(IN:='a1234', ENO=>CheckENO8);
Assert(resultStr8 = 16#0000_0000_0000_0000);
Assert(CheckENO9 = FALSE);
resultStr9 := TO_LWORD(IN:='++1', ENO=>CheckENO9);
Assert(resultStr9 = 16#0000_0000_0000_0000);
Assert(CheckENO9 = FALSE);
resultStr10 := TO_LWORD(IN:='1-3', ENO=>CheckENO10);
Assert(resultStr10 = 16#0000_0000_0000_0000);
Assert(CheckENO10 = FALSE);
resultStr11 := TO_LWORD(IN:='_12', ENO=>CheckENO11);
Assert(resultStr11 = 16#0000_0000_0000_0000);
Assert(CheckENO11 = FALSE);
resultStr12 := TO_LWORD(IN:='12_', ENO=>CheckENO12);
Assert(resultStr12 = 16#0000_0000_0000_0000);
Assert(CheckENO12 = FALSE);
resultStr13 := TO_LWORD(IN:='1__2', ENO=>CheckENO13);
Assert(resultStr13 = 16#0000_0000_0000_0000);
Assert(CheckENO13 = FALSE);
resultStr14 := TO_LWORD(IN:='18500000000000000000', ENO=>CheckENO14); (* The upper limit for LWORD is 16#FFFF_FFFF_FFFF_FFFF. This evaluates to the decimal value 18.446.744.073.709.551.615. *)
Assert(resultStr14 = 16#FFFF_FFFF_FFFF_FFFF);
Assert(CheckENO14 = FALSE);
END_FUNCTION_BLOCK

When creating your application within the ST-editor, enter a call of a block by typing the text as requested by the syntax or use Content Assist.