subscribe

[ Function ]

Version 1.8

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

Version 1.12

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

Registers a subscription on a list of commands.

subscribe 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.

subscribe 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.

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.

subscribe 3:

Available from 1.8

Sample - JavaScript

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

TcHmi.Server.subscribe(commands, 500, 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.bTest2'
    }
];
TcHmi.Server.subscribe<boolean>(commands, 500, 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
    }
});