subscribeEx

[ Function ]

Version 1.10

public static subscribeEx(
    commands: TcHmi.Server.ICommand[],
    interval: number,
    requestOptions: Server.IRequestOptions,
    callback?: null | (
         (data: TcHmi.Server.IResultObject) => void
    )
): number | null;

Version 1.12

public static subscribeEx<R = any>(
    commands: TcHmi.Server.ICommand[],
    interval: number,
    requestOptions: Server.IRequestOptions,
    callback?: null | (
         (data: TcHmi.Server.IResultObject<unknown, R>) => void
    )
): number | null;

Registers a subscription on a list of commands.

subscribeEx 1:

Version 1.12

If the API is used in TypeScript code, the TypeScript compiler can be notified of the server symbol type. A subsequent access to readValue in the callback function then automatically has the correct type.

When accessing several symbols with different types, the type can be manually set to any.

subscribeEx 2:

This is a merely an auxiliary programming feature. The browser does not recognize this "type annotation". No check or conversion takes place at runtime.

Parameter

Name

Type

Description

commands

TcHmi.Server.ICommand[]

An array of ICommand objects.

interval

number

The earliest time after which the TwinCAT HMI server transmits changes.

requestOptions

Server.IRequestOptions

Options

callback [ optional ]

null, (data: TcHmi.Server.IResultObject) => void

Asynchronous callback function that is triggered when the operation was terminated.

Return value

Type

Description

number, null

The request ID as number or null in the event of an error.

subscribeEx 3:

Available from version 1.10

Sample - JavaScript

var commands = [
    {
        'symbol': 'PLC1.MAIN.bTest'
    },
    {
        'symbol': 'PLC1.MAIN.nTest'
    }
];

TcHmi.Server.subscribeEx(commands, 500, { timeout: 2000 }, function (data) {
    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;
        }

        // Handle result...
        TcHmi.Log.debugEx(command.symbol, ':', command.readValue);
    }
});

Sample - TypeScript

// subscription with type annotation
var commands = [
    {
        'symbol': 'PLC1.MAIN.bTest'
    },
    {
        'symbol': 'PLC1.MAIN.bTest1'
    }
];

TcHmi.Server.subscribeEx<boolean>(commands, 500, { timeout: 2000 }, function (data) {
    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;
        }

        // Handle result...
        TcHmi.Log.debugEx(command.symbol, ':', command.readValue); // TS knows this is a boolean
    }
});