Skip to content

Latest commit

 

History

History
75 lines (65 loc) · 2.83 KB

InteractingWithServerExtensions.md

File metadata and controls

75 lines (65 loc) · 2.83 KB

Interacting with a server extension

Every server extension provides a set of symbols that clients, the HMI server, as well as other server extensions can use to interact with the server extension. There are several ways to invoke these symbols in the client.

Via a Data Binding

  • Drop an HMI control from the toolbox and create a "Data Binding" to a control attribute

  • Browse to the symbol that you want to invoke in the "Mapped Symbols" tab

    Build

  • In the example shown in the screenshot, the current timestamp is periodically fetched from an NTP server by the "NetworkTime" extension and displayed in the "TcHmiTextblock"

Using the Framework API

Example JavaScript: Register onPressed event of button to request server extension symbol

var destroyEvent = TcHmi.EventProvider.register(
    'TcHmiButton.onPressed',
    function (evt, data) {
        if (TcHmi.Server.isWebsocketReady()) {
            var request = {
                requestType: 'ReadWrite',
                commands: [
                    {
                        symbol: 'NetworkTime.Now',
                        commandOptions: ['SendErrorMessage']
                    }
                ]
            };

            // Send request to TwinCAT HMI Server.
            TcHmi.Server.requestEx(request, { timeout: 2000 }, function (data) {
                // Callback handling.
                if (data.error !== TcHmi.Errors.NONE) {
                    // Handle TcHmi.Server class level error here.
                    return;
                }
                var response = data.response;
                if (!response || response.error !== undefined) {
                    // Handle TwinCAT HMI Server response level error here.
                    return;
                }
                var commands = response.commands;
                if (commands === undefined) {
                    return;
                }
                for (var i = 0, ii = commands.length; i < ii; i++) {
                    var command = commands[i];
                    if (command === undefined) {
                        return;
                    }
                    if (command.error !== undefined) {
                        // Handle TwinCAT HMI Server command level error here.
                        return;
                    }
                    // Print network time (readValue to browser console)
                    console.log(command.symbol + '=' + command.readValue);
                }
            });
        }
    }
);