• No results found

Prefix and Postfix

In document C For WinCC (Page 160-166)

The increment and decrement operators are available in a prefix and postfix version. Both versions perform the same action - they increase or decrease the value of the variable to which they are applied to by one. The difference lies in the return value. The prefix version increments or decrements the value of the variable and returns this new value. The postfix version returns the original value of the variable and only then increments or decrements the variable.

C-Action at Button2

In the first section, two variables of the int data type are defined and initialized.

• The increment operator in the prefix or postfix version are applied to these two

variables. The return values of these operators are output through the printf() function in the diagnostics window. Afterwards, the variable contents are also output by the printf() function in the diagnostics window. This output is displayed in the next section.

WinCC Configuration Manual 4-35 C79000-G8276-C157-01

Output in the Diagnostics Window

The sample described in this section generates the following output in the diagnostics window: It can be seen that the prefix version of the increment operator returns the incremented variable value, whereas the postfix version returns the original variable value.

However, in both cases the variable will be incremented.

4-36 WinCC Configuration Manual C79000-G8276-C157-01

4.3.3 Sample 3 - Bit Operations

In this sample, the basic bit-by-bit operators are used. The sample has been configured at the Button3 object displayed below at Event Mouse Mouse Action.

Description

In this sample, the bit-by-bit operators are applied to the content of two WinCC tags (unsigned 16-Bit values). The result of the operation is stored in another WinCC tag of the same type. The operator applied is controlled and simultaneously displayed by the Button6 object. The bit-by-bit connections AND, OR, NAND, NOR and EXOR are available. A number value is assigned to each selection option, which is stored in another WinCC tag (unsigned 8-Bit value).

WinCC Configuration Manual 4-37 C79000-G8276-C157-01

C-Action at Button3

In the first section, one variable of the BYTE data type and three variables of the DWORD data type are defined. These variables are used to temporarily store the WinCC tags.

In the next section, the two WinCC tags to be connected are read into the dwValue1 and dwValue2 variables. Additionally, the WinCC tag determining the type of the bit-by-bit connection operator will be read into the byOperation variable.

In the next section, the dwValue1 and dwValue2 variables are connected bit-by-bit depending on the content of the byOperation variable. The connection result is stored in the dwResult variable. The connection operation to be performed is selected by a switch-case construction. This construction is described in greater detail in the Loops chapter.

In the next section, the connection result contained in the dwResult variable is written to the corresponding WinCC tag.

4-38 WinCC Configuration Manual C79000-G8276-C157-01

4.3.4 Sample 4 - Rotating Byte-by-Byte

In this sample, the bit-by-bit move operators are used to rotate the value contained in a WinCC tag (unsigned 16-Bit value) byte-by-byte. This means the exchange of the high byte and low byte. The sample has been configured at the Button4 object displayed below at Event Mouse Mouse Action.

C-Action at Button4

In the first section, a variable of the DWORD data type is defined. This variable is used to temporarily store the WinCC tag. In addition, two help variables of the DWORD type are defined.

In the next section, the WinCC tag to be processed is written to the dwValue variable.

In the next section, the individual bits of the dwValue variable are moved to the left by eight positions (one byte) and stored in the dwtempValue1 variable. Afterwards, the individual bits of the dwValue variable are moved to the right by eight positions and stored in the dwtempValue2 variable. Both values determined in this section are connected bit-by-bit (OR) and the result stored in the dwValue variable.

In the next section, the rotated variable value contained in the dwValue variable is written to the corresponding WinCC tag.

WinCC Configuration Manual 4-39 C79000-G8276-C157-01

4.3.5 Sample 5 - Mathematical Functions

In this sample, various mathematical functions are used which are available in C by default.

The sample has been configured at the Button5 object displayed below at Event Mouse Mouse Action.

C-Action at Button5

• In the first section, the variables are defined.

First, the pow() function is called. Two parameters are assigned to this function. In this sample, the return value of the function corresponds to the third power of the dValue variable content.

Next, the sqrt() function is called. The return value of this function is the square root of the value transferred.

Next, the abs() function is called. The return value of this function is the absolute value of the value transferred.

Next, the rand() function is called. No parameters are assigned to this function. The function will return a random value as the return value.

The results of these calculations are output through the printf() function in the diagnostics window. This output is displayed in the next section.

4-40 WinCC Configuration Manual C79000-G8276-C157-01

Output in the Diagnostics Window

The sample described in this section generates the following output in the diagnostics window:

In document C For WinCC (Page 160-166)