This sample shows how a control uses the TcHmi.ObjectResolver class to resolve symbol expressions inside the object value of an attribute and how it writes changes of the object back to the symbols behind those expressions.
- Open ObjectResolver.sln in Visual Studio or the TwinCAT XAE Shell with TwinCAT HMI Engineering installed.
- Restore the NuGet packages, build the solution and start the Live-View of the
HMIproject. No PLC is needed. - Select
UsageorImplementationin the navigation. Both pages contain an instance ofMyControl. - Change a value in
MyControlor in the spinboxes at the bottom. The other inputs show the new value.
The solution contains two projects:
| Project | Content |
|---|---|
| MyPackage | A framework project with the control MyControl. It shows the three values of its attribute in spinboxes. |
| HMI | An HMI project which uses MyControl and binds its attribute to the internal symbols Number1 to Number3. |
Desktop.view uses the layout of the TwinCAT HMI BaseTemplate from the Beckhoff.TwinCAT.HMI.BaseTemplate package: a header at the top, a navigation on the right, a region for the pages in the center and a container with three spinboxes at the bottom. The spinboxes are bound to the same internal symbols as MyControl, so you can watch the values flow in both directions.
MyControl has the attribute MyAttribute of an object type with the properties number1, number2 and number3. A plain binding can only bind the whole attribute to one symbol. Here each property of the object contains its own symbol expression:
<div id="UsageMyControl" data-tchmi-type="TcHmi.Controls.MyPackage.MyControl">
<script data-tchmi-target-attribute="data-tchmi-my-attribute" type="application/json">
{
"number1": "%i%Number1|BindingMode=TwoWay%/i%",
"number2": "%i%Number2|BindingMode=TwoWay%/i%",
"number3": "%i%Number3|BindingMode=TwoWay%/i%"
}
</script>
</div>The attribute allows this with "allowSymbolExpressionsInObject": true in Description.json. The Framework does not resolve these expressions by itself: the control receives the object as it is and resolves it with a TcHmi.ObjectResolver.
Number1 to Number3 are internal symbols defined in tchmiconfig.json, so no PLC is needed. The expressions can just as well point to server symbols like %s%PLC1.MAIN.nValue%/s%.
The Implementation page shows the same four steps as code excerpts. The complete code is in MyControl.ts.
The setter of MyAttribute creates a resolver for the new value and registers its watch:
const resolver = new TcHmi.ObjectResolver(convertedValue, {
parentControl: this,
type: this.getAttributeDescription('MyAttribute')?.type,
});
this.__objectResolvers.set('MyAttribute', {
resolver: resolver,
watchCallback: this.__onMyAttributeWatchCallback,
watchDestroyer: resolver.watch(this.__onMyAttributeWatchCallback),
});TcHmi.ObjectResolverreplacesTcHmi.Symbol.ObjectResolver, which is deprecated.parentControldelays the resolution of control symbols like%ctrl%...%/ctrl%until the partial which contains the control is initialized.typepasses the type of the attribute from Description.json, so the resolver knows the expected structure of the object.- Storing the resolver together with its watch callback in
__objectResolverslets the base classTcHmiControltake care of it: it watches the resolver again whenever the control is attached and destroys it together with the control.
The resolver replaces each symbol expression in the object with the current value of its symbol and calls the watch callback __onMyAttributeWatchCallback with the resolved object. It calls the callback again whenever one of the symbols changes. The watch callback is the only place where the control takes over a new value:
- While the control is not attached, it suspends the resolver after the first value.
- It logs an error with
TcHmi.Log.Controls.error. - It ignores a value which did not change.
- It stores the new value, raises
onPropertyChangedand shows the values in the spinboxes.
Each spinbox inside MyControl edits one property of MyAttribute. __attach registers the onValueChanged event of each spinbox and passes the property and the new value to __writeMyAttributeProperty:
TcHmi.EventProvider.register(`${this.getId()}.SpinboxInput_number1.onValueChanged`, () => {
this.__writeMyAttributeProperty('number1', this.__spinBoxNumber1?.getValue());
});__writeMyAttributeProperty passes a changed copy of the resolved value to write and names the changed path:
// __processMyAttribute sets the values of the Spinboxes, which raises onValueChanged again. Skip it.
if (this.__myAttributeValue[property] === value) {
return;
}
const valueNew: MyControl.MyObjectType = { ...this.__myAttributeValue, [property]: value };
resolverInfo.resolver.write(valueNew, [property]);- The control does not modify its current value. The resolver takes over the new value and reports it to the watch callback once the writes are done, so a change from the user takes the same path as a change of a symbol and
onPropertyChangedis raised for it. writeexpects the resolved values, not symbol expressions.- The dirty paths name the changed properties. Without them the resolver compares the object with its last value to find the changes.
- A changed path is written to its symbol if its symbol expression uses
BindingMode=TwoWay. Without it the resolver only keeps the new value locally until the symbol changes the next time. - Skipping unchanged values avoids writing a symbol again with the value it just delivered.
- HMI/Desktop.view: the BaseTemplate layout with the navigation, the region which hosts the pages and the spinboxes which are bound to the internal symbols.
- HMI/Pages/Usage.content:
MyControlwith the object value ofMyAttributeand its explanation. - HMI/Pages/Implementation.content:
MyControlwith the explanation of theObjectResolvercode. - HMI/Properties/tchmiconfig.json: the internal symbols
Number1toNumber3. - MyPackage/MyControl/Description.json: the attribute
MyAttributewith its type andallowSymbolExpressionsInObject. - MyPackage/MyControl/MyControl.ts: the control which resolves and writes its attribute value.
- MyPackage/MyControl/Template.html: the caption, the labels and the spinboxes of the control. The theme dependent colors are in the
Themesfolder of the control.
