Unit conversion
Unit switching in TwinCAT HMI enables the switching of the displayed units and the conversion of the corresponding values depending on the language or a specific event.
Conversion of values
The conversion of the units is solved by functions. A function is linked to the Control property in the properties window and returns the converted value. As a parameter value, the function receives the output value that is present in a certain unit.
Within the function, the developer can convert the corresponding value (e.g. from a symbol in the PLC) into the desired unit. The respective unit can be added to the converted value directly within the function as a string or alternatively stored as a language key.
Switching units
The units can be switched depending on the language or any other event. To do this, another parameter is passed to the function that contains the unit into which the value is to be converted. As soon as this parameter changes, the function is automatically called again and the value is converted and returned accordingly.
For example, pass an internal symbol to the function that contains the unit into which the value is to be converted. The unit can be written anywhere in the internal symbol. Possible scenarios are e.g:
- "onLocaleChanged": This global event is triggered as soon as a language is loaded. At this event the unit can be written into the internal symbol according to the active language.
- Events: Depending on any event, a unit can be written into the internal symbol. The event can be triggered, for example, if a value in the PLC changes or the user has pressed a button on the user interface.