Forcing and Writing Variables Values

In TwinCAT you can change the values of variables on the controller in online mode. This overwrites the originally entered value of the variable. There are two different approaches to this, forcing and writing a previously prepared value.

CAUTION

Damage to property and persons due to unexpected behavior of the machine or system

The abnormal changing of variable values in a PLC program that is running on the controller can result in unexpected behavior of the controlled machine. Depending on the machine being controlled, the machine or workpieces may be damaged or the health and life of people may be endangered.

  • Evaluate possible risks before forcing variable values and take appropriate safety precautions.

Writing is done with the command Write values Forcing and Writing Variables Values 1: and sets the variable once to the prepared value. The value can thus be overwritten by the program at any time.

Forcing is done with the command Force values Forcing and Writing Variables Values 2: and sets the prepared value permanently.

Preparing a value for forcing or writing is possible at various points:

Operating principle of Forcing

In the case of forcing, TwinCAT writes the value in each cycle so that the variable is permanently kept at the forced value. Forcing must be canceled by the user. The forced value of the variable is changeable within a PLC cycle, as with any other variable.

Setting the prepared value to the respective variable is done at the beginning and end of each processing cycle. Sequence of processing in each cycle:

  1. Read inputs
  2. Force values
  3. Process code
  4. Force values
  5. Write outputs.

It is possible that a forced variable temporarily gets a different value during code processing in the cycle because the code performs an assignment. The variable then receives the forced value again only at the end of the cycle. Also by the write access of a client to symbols of the application the variable value can be overwritten during the cycle.

There are two different approaches to the forcing of values.

For one approach the project can be opened from the Solution Explorer. The values are then forced directly from here. Logging in to the runtime is not necessary for this. This keeps the value of the variable forced even if the user leaves the runtime system. In this case, neither a warning message nor a query dialog will appear.

Forcing and Writing Variables Values 3:

In contrast, it is only possible to force a variable in the PLC if the user logs into the runtime system Forcing and Writing Variables Values 4:.

Forcing and Writing Variables Values 5:

When logging out Forcing and Writing Variables Values 6: of the runtime system, a dialog appears asking whether the variable should remain forced.

Forcing and Writing Variables Values 7:

If you select Yes in this dialog, the forced values will be canceled. If you select No here, the forced values are stored on the runtime system and accordingly remain permanent. This means that you can log out of the runtime system in the meantime and the forced values still exist if you log back into the runtime system at another time.

Notice

Material damages due to permanently forced variables

Variables are held permanently at the forced value, as a result of which the forced value can persist longer than expected and material damage can occur. This applies in particular if the machine is running without supervision.

  • To ensure that the machine does not perform any unexpected movements, reset forced values at the end of the machining process.
Forcing and Writing Variables Values 8:

Please note that forced variables Forcing and Writing Variables Values 9: have to be canceled explicitly by the user. However, the forced value of the variable can persist even after unforcing.

  • Canceling the forcing of the variable.
  • To securely cancel the forced value of the variable, change the values back to the original values.
  • Log out of TwinCAT and confirm the query whether forcing should be canceled for all variables with Yes.
  • The variable now has its original value again.

Temporary variables cannot be forced. Temporary variables can only be written if the PLC is at a breakpoint that is located in the code of the same POU in which the temporary variable is defined. The writing of temporary variables also has no effect in flow control mode.

See also:

Forcing in the declaration part

Your PLC project has a POU with declarations. The application is in online mode.
1. Open the POU in the editor by double-clicking on the object or on the command Open in the menu View or the context menu.
2. In the declaration part of the editor, double-click in the column Prepared value of a variable.
The field becomes editable and you can enter a value.
Forcing and Writing Variables Values 10:
3. Execute step 2 for further variables.
4. Select the Force values command Forcing and Writing Variables Values 11: in the PLC menu or in the TwinCAT PLC toolbar options toolbar.
The values of the variables are overwritten by the prepared values. The values are marked with the symbol Forcing and Writing Variables Values 12:.
Forcing and Writing Variables Values 13:
Forcing and Writing Variables Values 14:

You can also force values of variables in the view PLC > Windows > Watch <n>.

See also:

Forcing in the implementation part

The application is in online mode.
1. Open the POU in the editor by double-clicking on the object or on the command Open in the menu View or the context menu.
2. Double-click on an inline monitoring field in the implementation part of the editor.
The dialog Prepare Value opens.
3. In the field Prepare a new value for the next write or force operation, enter the new value.
The prepared value appears in the inline monitoring field.
Forcing and Writing Variables Values 15:
4. Select the command Force values in the menu PLC or in the toolbar TwinCAT PLC Toolbar Options.
The value of the variable is overwritten by the prepared values. The values are marked with the symbol Forcing and Writing Variables Values 16:.
Forcing and Writing Variables Values 17:

See also:

Viewing and canceling all forced variables in a list

The application is in online mode. Several variables are in the forced state.
1. Select the command Watch all forces in the menu PLC > Windows.
The view Watch all forces appears. It contains all currently forced variables of the PLC project in the form of a watch list.
2. Select all rows of the list and, in the selection list at the top left select in the view Unforce > Unforce and keep all selected values.
Forcing is canceled for the variables and they are given the values that they had before forcing.

See also: