requestEx

[ Function ]

Version 1.10

public static requestEx(
    request: TcHmi.Server.IMessage,
    requestOptions: Server.IRequestOptions,
    callback?: null | (
     (data: Server.IResultObject) => void
    )
): number | null;

Version 1.12

public static requestEx<W = any, R = any>(
    request: TcHmi.Server.IMessage,
    requestOptions: Server.IRequestOptions,
    callback?: null | (
     (data: Server.IResultObject<W, R>) => void
    )
): number | null;

Sends a user-defined request to the TwinCAT HMI server.

Version 1.12

If the API is used in TypeScript code, the TypeScript compiler can be notified of the server symbol type. A later access to readValue in the callback function then automatically has the correct type. Here, the type of the value is specified first when writing (Write) and then optionally the type of the value to be read (Read).

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

requestEx 1:

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

request

TcHmi.Server.IMessage

TwinCAT HMI server web socket request object.

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.

requestEx 2:

Available from version 1.10

Sample - JavaScript

// Request object.
/** @type {TcHmi.Server.IMessage} */
var request = {
    'requestType': 'ReadWrite',
    'commands': [
        {
            'symbol': 'PLC1.MAIN.bTest'
        },
        {
            'symbol': 'PLC1.MAIN.nTest'
        }
    ]
};

// 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;
        }
        // Handle result...
        TcHmi.Log.debugEx(command.symbol, ':', command.readValue);
    }
});

Sample - TypeScript

// Request with type annotations

// Request object.
var request: TcHmi.Server.IMessage = {
    'requestType': 'ReadWrite',
    'commands': [
        {
            'symbol': 'PLC1.MAIN.bTest'
        },
        {
            'symbol': 'PLC1.MAIN.bTest2'
        }
    ]
};

// Send request to TwinCAT HMI Server.
TcHmi.Server.requestEx<boolean>(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;
        }
        // Handle result...
        TcHmi.Log.debugEx(command.symbol, ':', command.readValue); // TS knows this is a boolean
    }
});