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:
Read the inputs
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.
Process the IEC code
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.
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 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.
Open the POU in the editor by choosing the command
.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.
Perform Step 2 for other variables.
Click
.The variable values are overwritten with the prepared values. The values are marked with the
symbol.
Tip
You can also force the variable values in the Watch view.
Forcing in the implementation part
Requirement: The application is in online mode.
Open the POU in the editor by choosing the command
.In the implementation part of the editor, double-click an inline monitoring field (1).
The Prepare Value dialog opens.
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.
Click
.The value of the variables is overwritten with the prepared values. The values are marked with the
symbol.
Viewing and editing all forced variables one list
Requirement: The application is in online mode. Multiple variables are forced.
Click
.The Watch All Forces view opens. It contains all currently forced variables of the application in the form of a watch list.
Select all lines in the list and in the list box in the upper left part of the view click
.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 or Unforce Values commands.
Open the editor of the CFC POU by double-clicking the object in the tree.
Log in to the application on the target device.
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
.In CFC, select the input of the POU and click Force Function Block Input in the context menu.
The Force Value dialog opens.
Set a new value for the input. Example in the case of a
TON
POU:FALSE
for the Boolean inputIN
, ort#4s
for thePT
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.
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