Skip to main content

Forcing and Writing of Variables

Caution

Unusual changes to variable values in an application which is currently running on the controller can cause undesired behavior of the controlled plant.

Evaluate possible dangers before forcing variable values, and take the respective safety precautions. Depending on the controlled plant, this could result in damage to the plant or the workpieces, or injury or death of personnel.

In CODESYS, variable values in the PLC can be changed in online mode. Here we make a distinction between forcing and writing a previously prepared value.

Writing is done with the Write Values command (Ctrl+F7) and sets the variable to the prepared value one time. In this way, the value can be overwritten again by the program at any time.

Forcing is done with the Force Values command (F7) and sets the prepared value permanently. For more information, see below.

The preparation of a value for forcing or writing is possible at different places:

  • Declaration part: Prepared value field

  • Implementation part of the FBD/LD/IL editor: inline monitoring field

  • Watch view: Prepared value field

For instructions about this, see below. In the case that you want to prepare a value again for an already forced value, the Prepare Value dialog opens with options for handling the current force value.

Functionality of forcing

The prepared value is set to the respective variable at the beginning and end of a task cycle (or of a processing loop in the case of other task types).

The processing order in each cycle of a task is as follows:

  1. Read the inputs

  2. Force: Before the first program call, all prepared values are written to the variables by the runtime system, regardless of whether or not they are used by the task.

  3. Process the IEC code

  4. Force: After the last program call, all prepared values are written to the variables by the runtime system, regardless of whether or not they are used by the task.

  5. Write the outputs

Note: It is possible that a forced variable temporarily gets a different value in the cycle while the code is being processed because the IEC code performs an assignment. Then the variable receives the forced value again only at the end of the cycle. The variable value can also be overwritten by the write access of a client to symbols of the application in mid-cycle. For this case, see the Access variables in sync with IEC tasks option in the Properties of the device object, or the Configure synchronization with IEC tasks setting in the symbol configuration. In this way, a PLC handler-supported synchronization of the write accesses by clients can be enabled with the task cycle.

Important

Forced values are marked with the _cds_icon_forced_variable.png symbol. CODESYS does the forcing until the user lifts it explicitly by one of the following actions:

  • Execution of the Unforce All Values command

  • Releasing the force operation in the Prepare Value dialog

  • Logging out of the application

    If forced variables still exist when logging out, then a dialog opens, prompting whether or not forcing should be lifted for all variables. If you respond by clicking No, then the forced values are applied again at the next login.

Forcing in the declaration part

Requirement: Your application includes a POU with declarations. The application is in online mode.

  1. Open the POU in the editor by choosing the command Project → Edit Object.

  2. In the declaration part of the editor, double-click in column (1) Prepared Value of a variable.

    The field can be edited and a value can be entered. When it is a Boolean value, you change the value by clicking in the field.

    _cds_img_force_values_1.png
  3. Perform Step 2 for other variables.

  4. Click Debug → Force Values.

    The variable values are overwritten with the prepared values. The values are marked with the _cds_icon_forced_variable.png symbol.

    _cds_img_force_values_2.png

Tip

You can also force the variable values in the Watch view.

Forcing in the implementation part

Requirement: The application is in online mode.

  1. Open the POU in the editor by choosing the command Project → Edit Object.

  2. In the implementation part of the editor, double-click an inline monitoring field (1).

    The Prepare Value dialog opens.

  3. Enter the new value in the field Prepare a new value for the next write or force operation.

    The prepared value appears in the inline monitoring field.

    _cds_img_force_values_3.png
  4. Click Debug → Force Values.

    The value of the variables is overwritten with the prepared values. The values are marked with the _cds_icon_forced_variable.png symbol.

    _cds_img_force_values_4.png

Viewing and editing all forced variables one list

Requirement: The application is in online mode. Multiple variables are forced.

  1. Click View → Watch → Watch All Forces.

    The Watch All Forces view opens. It contains all currently forced variables of the application in the form of a watch list.

  2. Select all lines in the list and in the list box in the upper left part of the view click Unforce → Unforce and Keep All Selected Values.

    The variables are unforced and they get the values that they had before forcing.

Forcing a function block input in CFC

Requirement: An application has a CFC POU that contains a function block, and the application is in offline mode.

Important

This kind of forcing uses a data breakpoint internally and is therefore different from forcing with the Force Values command or F7.

Values which were forced by the Force Function Block Input command do not respond to the Watch all Forces or Unforce Values commands.

  1. Open the editor of the CFC POU by double-clicking the object in the tree.

  2. Log in to the application on the target device.

  3. When using compiler version 3.5.11.x or 3.5.12.x, enable the "forceability" for the desired function block. Select the POU element in CFC and click CFC → Prepare Box for Forcing.

  4. In CFC, select the input of the POU and click Force Function Block Input in the context menu.

    The Force Value dialog opens.

  5. Set a new value for the input. Example in the case of a TON POU: FALSE for the Boolean input IN, or t#4s for the PT input (TIME). Click OK to confirm.

    The set value is forced immediately. A green circle is displayed at the upper left of the POU element and the name of the input in the element is highlighted in green. In the case of a Boolean value, a small monitoring view with the value also opens at the input. In the monitoring views, the forced value is displayed, for example in the Value column, as in the declaration part.

  6. To remove the forced value, click Force Function Block Input again. In the Force Value dialog, select the Remove value option.

    Forcing is canceled. The input gets the current value from the controller.

For more information, see: Force Values and Write Values