Definition: Example unit classes

The following example shows three typical applications for unit classes:

Scenario

The scenario describes the following case:

A distance of 0 ... 1000 mm recorded by the PLC is to be displayed in the application. Two different sensor types with the following output ranges are available:

Type

Measuring range

Output value range

A

0 ... 2000 mm

0 ... 32000

B

0 ... 100 inch

0 ... 32000

In the application, it should be possible to switch the sensor to be used via a variable-dependent unit class.

In addition, the country-specific representation of the unit (cm/inch) must be understood. A language-dependent unit class is used for this purpose.

As an extension, switching between cm/mm/inch/mil is also shown. Switching takes place via a second variable-dependent unit class.

Definitions

Definitions are required for the example:

The block diagram shows the systematic structure of the example:


Design

The design of the example includes the individual views and the controls for switching the units.


The following elements are required for the design:

Work steps

To simplify matters, the order of the steps to be carried out is arranged in such a way that the definitions and objects can be easily linked together:

Definition level

  • Carry out the following steps in the project file:

  1. Create two internal variables of the data type "VT_I2" by selecting the project explorer node VariablesWChannelsWInternal. Open the table editor for the "Internal" channel by double-clicking on the node. Select the table editor with a mouse click and insert the two variable definitions by pressing the F8 key. Change the names of the two variables to "DistanceSensorAdaptionSwitch" and "DistanceSensorExtendedSwitch".

  2. Create a variable-dependent unit class. To do this, go to the VariablesWEunit classesWvariable-dependent node in the project explorer. Press the F8 key directly in the project explorer to create a new definition. Enter "DistanceSensorAdaption" as the name and "DistanceSensorAdaptionSwitch" as the switching variable on the properties pages.

  3. Create a second variable-dependent unit class. Enter "LengthConversionExtended" as the name and "DistanceSensorExtendedSwitch" as the switching variable.

  4. Select the node of the "DistanceSensorAdaption" unit class and insert a new unit definition with the following parameters in the table editor:

    Parameters

    Value

    X1

    0

    X2

    1

    Y1

    0

    Y2

    1

  5. Insert a second unit definition with the following parameters into the "DistanceSensorAdaption" unit class:

    Parameters

    Value

    X1

    0

    X2

    12599

    Y1

    0

    Y2

    16000

  6. In the project explorer, switch to the "LengthConversionExtended" unit class and insert a new unit definition with the following parameters in the table editor:

    Parameters

    Value

    X1

    0

    X2

    16000

    Y1

    0

    Y2

    100

  7. Add a second unit definition with the following parameters to the "LengthConversionExtended" unit class:

    Parameters

    Value

    Switching identifier

    1

    X1

    0

    X2

    16000

    Y1

    0

    Y2

    39,37

    Unit text

    in

  8. Add a third unit definition with the following parameters to the "LengthConversionExtended" unit class:

    Parameters

    Value

    Switching identifier

    2

    X1

    0

    X2

    16000

    Y1

    0

    Y2

    16000

    Unit text

    mm

  9. Add the fourth required unit definition with the following parameters to the "LengthConversionExtended" unit class:

    Parameters

    Value

    Switching identifier

    3

    X1

    0

    X2

    16000

    Y1

    0

    Y2

    39370

    Unit text

    mil

  10. Create a language-dependent unit class. To do this, go to the VariableWEinitialisation classesLanguage-dependent node in the project explorer. Press the F8 key directly in the project explorer to create a new definition. Change the name of the unit class to "LengthConversion".

  11. Select the node of the "LengthConversion" unit class and insert a new unit definition with the following parameters in the table editor:

    Parameters

    Value

    Switching identifier

    German (1031)

    X1

    0

    X2

    16000

    Y1

    0

    Y2

    1000

    Unit text

    cm

  12. Add the second required unit definition with the following parameters to the "LengthConversion" unit class:

    Parameters

    Value

    Switching identifier

    English (1033)

    X1

    0

    X2

    16000

    Y1

    0

    Y2

    39,37

    Unit text

    in

  13. Browse the variable to be converted into your project. To do this, add a new communication channel of the type "VisiWin(.NET) driver" via the VariablesWChannels project explorer node. Specify the "Sample driver" in the Driver name parameter. Open the browser dialogue via the context menu entry Browse variables... of the newly added driver channel node. Select the DataTypesWReal variable here and confirm your selection by clicking OK.

  14. Rename the variable to "RawValue" using the Alias parameter.

  15. Duplicate the variable. To do this, select the Duplicate entry in the context menu of the definition in the table editor.

  16. Change the alias of the variable copied in this way to "SensorIndependent" and select the definition "DistanceSensorAdaption" via the property pages in the parameter Unit class (process).

  17. Duplicate the variable definition "SensorIndependent" and enter the name "SensorIndependent_Lng" as the alias. Specify "LengthConversion" as the "Unit class (display)".

  18. Duplicate the variable definition "SensorIndependent" once again and enter the name "SensorIndependent_Ext" as the alias. Specify "LengthConversionExt" as the "Unit class (display)".

The ItemID is identical for the four variable definitions. This means that the visualisation accesses the same value in the communication component across all variable definitions.

Why this complex approach?

If a variable value is converted via a unit class (process) between the communication component and the variable server, the raw value from the communication component is no longer available anywhere. The only way to bypass the conversion of the unit class (process) in the view is to define a second variable that does not use a unit class (process) but accesses the same variable in the communication component. To show the conversion in the application, the following steps (see Design) also implement the non-converted value as a display value in the application.

Test run

  • Carry out the following steps to test the project:

  1. Start the project via F5 and navigate to the corresponding view.

  2. Enter the value "12599" as the maximum for the type B sensor in vinRawValue.

  3. Press swtAdaptSensor to adapt the type B sensor and display the converted value in "vinSensorIndependent".

  4. Click the buttons for language switching to observe the display in inches or cm in the "vinLangDepDist" control element.

  5. Click the swtNormal_Milli button in combination with the language-controlling buttons to additionally view the changeover in [cm], [mm], [in.] and [mil.] in "vinLangDepDistExt".

  6. To test write access to variables with unit switching, enter values in the "vinSensorIndependent", "vinLangDepDist", "vinLangDepDistExt" control elements for different button constellations. Please also note that when switching a unit class, the raw value is always retained and all other determined values are reloaded into the display.