×
Siemens Industry Online Support
Siemens AG
Entry type: FAQ Entry ID: 21214698, Entry date: 03/29/2006
(1)
Rate

How can you compare variables of the type WORD or DWORD with each other?

  • Entry
  • Associated product(s)

Two variables of the type WORD are to be compared with each other in an S7-SCL program, for example, the variables:

  • Max_Var: WORD;
    Min_Var: WORD;
    Compare_Var: BOOL;

However, the comparison operations with ">", "<" and "<>" are not permitted in S7-SCL with variables of the type WORD or DWORD and in the case of a statement, for example with

  • Compare_Var:= Max_Var > Min_Var; 

the error message "Invalid operand types" is displayed during compilation. Such comparison operations are permitted only with variables of the type DINT. You must therefore first program data type conversion from WORD to DWORD and from DWORD to DINT using the conversion functions.
 

No. Procedure for programming the comparison operation
1 First of all, declare two variables each for the two variables "Max_Var" and "Min_Var" to be converted. For example for the variable "Max_Var":
  • Max_X: DWORD;
    Max_Y: DINT;
2 Then program the first data type conversion from WORD to DWORD using the conversion function of Class A:
  • Max_X:= WORD_TO_DWORD(Max_Var);
3 Using the second conversion function of Class B, convert from DWORD to DINT:
  • Max_Y:= DWORD_TO_DINT(Max_X);
4 The conversions described in points 2 and 3 must also be programmed for the variables for the second comparison variable "Min_Var". You can now write an instruction such as "Compare_Var:= Max_Y > Min_Y;", which is compiled by the compiler, for the purposes of a comparison operation.


Fig. 01

Keywords:
Greater than or equal to, Less than or equal to, Does not equal


Security information
In order to protect technical infrastructures, systems, machines and networks against cyber threats, it is necessary to implement – and continuously maintain – a holistic, state-of-the-art IT security concept. Siemens’ products and solutions constitute one element of such a concept. For more information about cyber security, please visit
https://www.siemens.com/cybersecurity#Ouraspiration.