writeSymbol

[ Function ]

Version 1.8

public static writeSymbol(
    symbolName: string | string[],
    value: any | any[],
    callback?: null | (
        (data: TcHmi.Server.IResultObject) => void
    ) = null
): number | null;

Version 1.12

public static writeSymbol<W = any, R = W>(
    symbolName: string | string[],
    value: W | W[],
    callback?: null | (
        (data: TcHmi.Server.IResultObject<W, R>) => void
    ) = null
): number | null;

Writes values to one or more TwinCAT HMI server symbols. If several symbols are to be written, the desired symbol names must be passed as an array and the value parameter as an array must be assigned exactly the same size.

writeSymbol 1:

Version 1.12

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

However, this may be overwritten. First the type of the value is specified 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.

writeSymbol 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

SymbolName

Support for string[] from version 1.8.714.0

 

string, string[]

The name of a TwinCAT HMI server symbol. If a string array is passed, several symbols are written simultaneously.

value

Support for array from version 1.8.714.0

 

string, number, boolean, Object, Array

The value to be written or an array of values to be written.

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.

writeSymbol 3:

Available from 1.8

Sample - JavaScript

// Write single symbol
TcHmi.Server.writeSymbol('PLC1.MAIN.bTest', true, 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;
    }
    var command = commands[0];
    if(command === undefined){
        return;
    }
    if(command.error !== undefined){
        // Handle TwinCAT HMI Server command level error here.
        return;
    }
    // Handle result...
    TcHmi.Log.debugEx('PLC1.MAIN.bTest:', command.readValue); // Editor knows this will be a boolean
});
// Write multiple symbols (since version 1.8.714.0)
TcHmi.Server.writeSymbol(['PLC1.MAIN.bTest', 'PLC1.MAIN.iTest'], [true, 1], 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;
    }
    var command = commands[0];
    if(command === undefined){
        return;
    }
    if(command.error !== undefined){
        // Handle TwinCAT HMI Server command level error here.
        return;
    }
    // Handle result...
    TcHmi.Log.debugEx('PLC1.MAIN.bTest:', command.readValue); // Editor thinks this will be a boolean or number

    var command1 = commands[1];
    if(command1 === undefined){
        return;
    }
    if(command1.error !== undefined){
        // Handle TwinCAT HMI Server command level error here.
        return;
    }
    // Handle result...
    TcHmi.Log.debugEx('PLC1.MAIN.iTest:', command1.readValue); // Editor thinks this will be a boolean or number
});

Sample 1 - TypeScript

// Write symbol with different write and read value types
TcHmi.Server.writeSymbol<boolean, string>('PLC1.MAIN.bTest', true, 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;
    }
    var command = commands[0];
    if(command === undefined){
        return;
    }
    if(command.error !== undefined){
        // Handle TwinCAT HMI Server command level error here.
        return;
    }
    // Handle result...
    TcHmi.Log.debugEx('PLC1.MAIN.bTest:', command.readValue); // Editor knows this is a string
});