request
[ Function ]
Version 1.8
public static request(
request: TcHmi.Server.IMessage,
callback?: null | (
(data: Server.IResultObject) => void
)
): number | null;
Version 1.12
public static request<W = any, R = any>(
request: TcHmi.Server.IMessage,
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.
![]() | 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 | TwinCAT HMI server web socket request object. | |
callback [ optional ] | null, (data: TcHmi.Server.IResultObject) => void | Asynchronous callback function that is triggered when the operation was terminated. |
Return value
Type | Description |
---|---|
The request ID as number or null in the event of an error. |
![]() | Available from 1.8 |
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.request(request, 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 annotation
/** 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.request<boolean>(request, 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
}
});