Client

Client

Unofficial Node.js library for connecting to Beckhoff TwinCAT automation systems using ADS protocol

This library is not related to Beckhoff in any way.

Constructor

new Client(settings)

Source:

Constructor for Client

Parameters:
Name Type Description
settings Settings

ADS Client settings as an object, see datatype Settings for setting descriptions

Throws:

If required settings are missing, an error is thrown

Type
ClientException

Members

(readonly) _internals

Source:

Internal variables - Not intended for external use

(readonly) connection :Connection

Source:

Connection info

Type:

(readonly) metaData :Metadata

Source:

Metadata related to the target system that is used during connection

Type:

Methods

(static) defaultSettings() → {Settings}

Source:

Default settings

Returns:

Default settings

Type
Settings

amsNetIdStrToByteArray(byteArray) → {array}

Source:

Helper: Converts AmsNetId string to byte array

Parameters:
Name Type Description
byteArray string

String that represents an AmsNetId

Returns:

AmsNetId as array

Type
array

byteArrayToAmsNetIdStr(byteArray) → {string}

Source:

Helper: Converts byte array (Buffer) to AmsNetId string

Parameters:
Name Type Description
byteArray Buffer | array

Buffer/array that contains AmsNetId bytes

Returns:

AmsNetId as string

Type
string

connect() → {Promise}

Source:

Connects to the target system using pre-defined Client::settings (at constructor or manually given)

Returns:

Returns a promise (async function)

  • If resolved, client is connected successfully and connection info is returned (object)
  • If rejected, something went wrong and error info is returned (object)
Type
Promise

convertFromRaw(rawData, dataTypeName) → {Promise.<object>}

Source:

Converts given raw data (byte Buffer) to Javascript object by given dataTypeName

Parameters:
Name Type Description
rawData Buffer

A Buffer containing valid data for given data type

dataTypeName string

Data type name in the PLC - Example: 'ST_SomeStruct', 'REAL',..

Returns:

Returns a promise (async function)

  • If resolved, Javascript object / variable is returned
  • If rejected, parsing failed and error info is returned (object)
Type
Promise.<object>

convertToRaw(value, dataTypeName, autoFill) → {Promise.<object>}

Source:

Converts given Javascript object/variable to raw Buffer data by given dataTypeName

Parameters:
Name Type Default Description
value object

Javacript object or variable that represents dataTypeName

dataTypeName string

Data type name in the PLC - Example: 'ST_SomeStruct', 'REAL',..

autoFill boolean false

If true and variable type is STRUCT, given value can be just a part of it and the rest is SET TO ZEROS. Otherwise they must match 1:1. Note: can also be 'internal' in internal use (no additional error message data)

Returns:

Returns a promise (async function)

  • If resolved, Javascript object / variable is returned
  • If rejected, parsing failed and error info is returned (object)
Type
Promise.<object>

createVariableHandle(variableName) → {Promise.<CreateVariableHandleResult>}

Source:

Creates an ADS variable handle to given PLC variable.

Using the handle, read and write operations are possible to that variable with readRawByHandle() and writeRawByHandle()

Parameters:
Name Type Description
variableName string

Variable name in the PLC (full path) - Example: 'MAIN.SomeStruct.SomeValue'

Returns:

Returns a promise (async function)

  • If resolved, creating handle was successful and handle, size and data type are returned (object)
  • If rejected, creating failed and error info is returned (object)
Type
Promise.<CreateVariableHandleResult>

createVariableHandleMulti(targetArray) → {Promise.<Array.<CreateVariableHandleMultiResult>>}

Source:

Creates ADS variable handles to given PLC variables.

Using the handle, read and write operations are possible to that variable with readRawByHandle() and writeRawByHandle()

NOTE: Returns only handle, not data type and size like createVariableHandle()

Parameters:
Name Type Description
targetArray Array.<string>

Variable names as array in the PLC (full path) - Example: ['MAIN.value1', 'MAIN.value2']

Returns:

Returns a promise (async function)

  • If resolved, command was successful and results for each handle request are returned(object)
  • If rejected, command failed and error info is returned (object)
Type
Promise.<Array.<CreateVariableHandleMultiResult>>

deleteVariableHandle(handle) → {Promise.<object>}

Source:

Deletes the given previously created ADS variable handle

Parameters:
Name Type Description
handle number

Variable handle to delete (created previously using createVariableHandle())

Returns:

Returns a promise (async function)

  • If resolved, deleting handle was successful
  • If rejected, deleting failed and error info is returned (object)
Type
Promise.<object>

deleteVariableHandleMulti(handleArray) → {Promise.<Array.<DeleteVariableHandleMultiResult>>}

Source:

Deletes the given previously created ADS variable handles

Parameters:
Name Type Description
handleArray Array.<string>

Variable handles to be deleted as array (can also be array of objects that contain 'handle' key)

Returns:

Returns a promise (async function)

  • If resolved, command was successful and results for each request are returned(object)
  • If rejected, command failed and error info is returned (object)
Type
Promise.<Array.<DeleteVariableHandleMultiResult>>

disconnect(forceDisconnectopt) → {Promise}

Source:

Unsubscribes all notifications, unregisters ADS port from router (if it was registered) and disconnects target system and ADS router

Parameters:
Name Type Attributes Default Description
forceDisconnect boolean <optional>
false

If true, the connection is dropped immediately (default = false)

Returns:

Returns a promise (async function)

  • If resolved, disconnect was successful
  • If rejected, connection is still closed but something went wrong during disconnecting and error info is returned
Type
Promise

getDataType(dataTypeName) → {Promise.<object>}

Source:

Returns full datatype as object.

First searchs the local cache. If not found, reads it from PLC and caches it

Parameters:
Name Type Description
dataTypeName string

Data type name in the PLC - Example: 'ST_SomeStruct', 'REAL',..

Returns:

Returns a promise (async function)

  • If resolved, full data type info is returned (object)
  • If rejected, reading or parsing failed and error info is returned (object)
Type
Promise.<object>

getEmptyPlcType(dataTypeName) → {Promise.<object>}

Source:

Returns empty Javascript object that represents given dataTypeName

Parameters:
Name Type Description
dataTypeName string

Data type name in the PLC - Example: 'ST_SomeStruct', 'REAL',..

Returns:

Returns a promise (async function)

  • If resolved, Javascript object / variable is returned
  • If rejected, parsing failed and error info is returned (object)
Type
Promise.<object>

getSymbolInfo(variableName) → {Promise.<object>}

Source:

Returns symbol information for given symbol.

First searchs the local cache. If not found, reads it from PLC and caches it

Parameters:
Name Type Description
variableName string

Variable name in the PLC (full path) - Example: 'MAIN.SomeStruct.SomeValue'

Returns:

Returns a promise (async function)

  • If resolved, symbol info is returned (object)
  • If rejected, reading failed and error info is returned (object)
Type
Promise.<object>

invokeRpcMethod(variableName, methodName, parameters) → {Promise.<RpcMethodResult>}

Source:

Invokes (calls) a function block method from PLC with given parameters. Returns the function result as Javascript object. NOTE: The method must have {attribute 'TcRpcEnable'} above its METHOD definiton

Parameters:
Name Type Description
variableName string

Function block path in the PLC (full path) - Example: 'MAIN.FunctionBlock'

methodName string

Method name to invoke

parameters object

Method parameters as object - Example: {param1: value, param2: value}

Returns:

Returns a promise (async function)

  • If resolved, invoking was successful and method return data (return value and outputs) is returned
  • If rejected, command failed error info is returned (object)
Type
Promise.<RpcMethodResult>

readAndCacheDataTypes() → {Promise.<object>}

Source:

Reads and caches all data types from target PLC runtime

WARNING Returned object is usually very large

Returns:

Returns a promise (async function)

  • If resolved, data types are cached and returned (object)
  • If rejected, reading failed and error info is returned (object)
Type
Promise.<object>

readAndCacheSymbols() → {Promise.<object>}

Source:

Reads all symbols from target device and caches them to the metaData.symbols

WARNING Returned object is usually very large

Returns:

Returns a promise (async function)

  • If resolved, symbols are returned (object)
  • If rejected, reading failed and error info is returned (object)
Type
Promise.<object>

readDeviceInfo() → {Promise.<object>}

Source:

Reads target device information and also saves it to the Client::metaData.deviceInfo object

Returns:

Returns a promise (async function)

  • If resolved, device information is returned (object)
  • If rejected, reading failed and error info is returned (object)
Type
Promise.<object>

readPlcRuntimeState() → {Promise.<object>}

Source:

Reads target device status and also saves it to the metaData.plcRuntimeStatus

Returns:

Returns a promise (async function)

  • If resolved, device status is returned (object)
  • If rejected, reading failed and error info is returned (object)
Type
Promise.<object>

readRaw(indexGroup, indexOffset, size, targetAdsPortopt) → {Promise.<Buffer>}

Source:

Reads (raw byte) data from PLC by given index group, index offset and size

All required parameters can be read for example with getSymbolInfo() method, see also readRawBySymbol()

Parameters:
Name Type Attributes Default Description
indexGroup number

Variable index group in the PLC

indexOffset number

Variable index offset in the PLC

size number

Variable size in the PLC (bytes)

targetAdsPort number <optional>
null

Target ADS port (optional) - default is this.settings.targetAdsPort

Returns:

Returns a promise (async function)

  • If resolved, reading was successful and data is returned (Buffer)
  • If rejected, reading failed and error info is returned (object)
Type
Promise.<Buffer>

readRawByHandle(handle, sizeopt) → {Promise.<Buffer>}

Source:

Reads (raw byte) data from PLC by given previously created variable handle

Parameters:
Name Type Attributes Default Description
handle object | number

Variable handle or object including {handle, size} to read from

size number <optional>
4294967295

Variable size in the PLC (bytes). Keep as default if not known - Default 0xFFFFFFFF

Returns:

Returns a promise (async function)

  • If resolved, reading was successful and data is returned (Buffer)
  • If rejected, reading failed and error info is returned (object)
Type
Promise.<Buffer>

readRawByName(variableName) → {Promise.<Buffer>}

Source:

Reads (raw byte) data from PLC by given variable name (uses READ_SYMVAL_BYNAME ADS command inside)

Parameters:
Name Type Description
variableName string

Variable name in the PLC (full path) - Example: 'MAIN.SomeStruct.SomeValue'

Returns:

Returns a promise (async function)

  • If resolved, reading was successful and data is returned (Buffer)
  • If rejected, reading failed and error info is returned (object)
Type
Promise.<Buffer>

readRawBySymbol(symbol) → {Promise.<Buffer>}

Source:

Reads (raw byte) data from PLC by given symbol info. Note: Returns Buffer

Parameters:
Name Type Description
symbol object

Object (PLC symbol - read for example with getSymbolInfo() method) that contains at least {indexGroup, indexOffset, size}

Returns:

Returns a promise (async function)

  • If resolved, reading was successful and data is returned (Buffer)
  • If rejected, reading failed and error info is returned (object)
Type
Promise.<Buffer>

readRawMulti(targetArray, targetAdsPortopt) → {Promise.<Array.<ReadRawMultiResult>>}

Source:

Reads multiple (raw byte) data from PLC by given index group, index offset and size

All required parameters can be read for example with getSymbolInfo() method, see also readRawBySymbol()

Parameters:
Name Type Attributes Default Description
targetArray Array.<ReadRawMultiParam>

Targets to read from

targetAdsPort number <optional>
null

Target ADS port (optional) - default is this.settings.targetAdsPort

Returns:

Returns a promise (async function)

  • If resolved, reading was successful and data is returned (object)
  • If rejected, reading failed and error info is returned (object)
Type
Promise.<Array.<ReadRawMultiResult>>

readSymbol(variableName) → {Promise.<object>}

Source:

Reads given PLC symbol value and parses it as Javascript object

Parameters:
Name Type Description
variableName string

Variable name in the PLC (full path) - Example: 'MAIN.SomeStruct.SomeValue'

Returns:

Returns a promise (async function)

  • If resolved, symbol value and data type are returned {value, type} (object)
  • If rejected, reading or parsing failed and error info is returned (object)
Type
Promise.<object>

readSymbolVersion() → {Promise.<number>}

Source:

Reads target device PLC software symbol version and also saves it to the Client::metaData.symbolVersion

Returns:

Returns a promise (async function)

  • If resolved, symbol version number is returned (number)
  • If rejected, reading failed and error info is returned (object)
Type
Promise.<number>

readSystemManagerState() → {Promise.<object>}

Source:

Reads target device system status (run/config/etc) Uses ADS port 10000, which is system manager

Returns:

Returns a promise (async function)

  • If resolved, system status is returned (object)
  • If rejected, reading failed and error info is returned (object)
Type
Promise.<object>

readUploadInfo() → {Promise.<object>}

Source:

Reads target device PLC upload info and also saves it to the Client::metaData.uploadInfo

Returns:

Returns a promise (async function)

  • If resolved, upload info is returned (object)
  • If rejected, reading failed and error info is returned (object)
Type
Promise.<object>

readWriteRaw(indexGroup, indexOffset, readLength, dataBuffer, targetAdsPortopt) → {Promise.<Buffer>}

Source:

Writes given byte Buffer to the target and reads result. Uses ADS command ReadWrite.

Parameters:
Name Type Attributes Default Description
indexGroup number

Index group in the PLC

indexOffset number

Index offset in the PLC

readLength number

Read data length in the PLC (bytes)

dataBuffer Buffer

Data to write

targetAdsPort number <optional>
null

Target ADS port (optional) - default is this.settings.targetAdsPort

Returns:

Returns a promise (async function)

  • If resolved, writing and reading was successful and data is returned (Buffer)
  • If rejected, command failed and error info is returned (object)
Type
Promise.<Buffer>

reconnect(forceDisconnectopt) → {Promise}

Source:

Disconnects and reconnects again. Subscribes again to all active subscriptions. To prevent subscribing, call unsubscribeAll() before reconnecting.

Parameters:
Name Type Attributes Default Description
forceDisconnect boolean <optional>
false

If true, the connection is dropped immediately (default = false)

Returns:

Returns a promise (async function)

  • If resolved, reconnecting was successful
  • If rejected, reconnecting failed and error info is returned
Type
Promise

restartPlc(adsPort) → {Promise.<object>}

Source:

Restarts the PLC runtime from settings.targetAdsPort or given ads port

WARNING: Make sure the system is ready to stop and start

Parameters:
Name Type Description
adsPort number

Target ADS port, for example 851 for TwinCAT 3 PLC runtime 1 (default: this.settings.targetAdsPort)

Returns:

Returns a promise (async function)

  • If resolved, command was successful
  • If rejected, command failed error info is returned (object)
Type
Promise.<object>

restartSystemManager() → {Promise.<object>}

Source:

Restarts TwinCAT system (system manager) for software update etc. Internally same as setSystemManagerToRun()

WARNING: Make sure the system is ready to stop and start

Returns:

Returns a promise (async function)

  • If resolved, command was successful
  • If rejected, command failed error info is returned (object)
Type
Promise.<object>

sendAdsCommand(adsCommand, adsData, targetAdsPortopt, targetAmsNetIdopt) → {Promise.<object>}

Source:

Sends an ADS command including provided data As default the command is sent to target provided in Client settings. However, the target can also be given

Parameters:
Name Type Attributes Default Description
adsCommand number

ADS command to send (See ADS.ADS_COMMAND)

adsData Buffer

Data to send as byte Buffer

targetAdsPort number <optional>
null

Target ADS port (default: same as in Client settings, this.settings.targetAdsPort)

targetAmsNetId string <optional>
null

Target AmsNetID (default: same as in Client settings, this.settings.targetAmsNetId)

Returns:

Returns a promise (async function)

  • If resolved, command was successful and result is returned
  • If rejected, command failed error info is returned (object)
Type
Promise.<object>

setDebugging(level)

Source:

Sets debugging using debug package on/off. Another way for environment variable DEBUG:

  • 0 = no debugging
  • 1 = Extended exception stack trace
  • 2 = basic debugging (same as $env:DEBUG='ads-client')
  • 3 = detailed debugging (same as $env:DEBUG='ads-client,ads-client:details')
  • 4 = full debugging (same as $env:DEBUG='ads-client,ads-client:details,ads-client:raw-data')
Parameters:
Name Type Description
level number

0 = none, 1 = extended stack traces, 2 = basic, 3 = detailed, 4 = detailed + raw data

setSystemManagerToConfig() → {Promise.<object>}

Source:

Sets the TwinCAT system (system manager) to config mode (blue TwinCAT icon)

WARNING: Make sure the system is ready to stop

Returns:

Returns a promise (async function)

  • If resolved, command was successful
  • If rejected, command failed error info is returned (object)
Type
Promise.<object>

setSystemManagerToRun() → {Promise.<object>}

Source:

Sets the TwinCAT system (system manager) to start/run mode (green TwinCAT icon)

WARNING: Make sure the system is ready to stop and start

Returns:

Returns a promise (async function)

  • If resolved, command was successful
  • If rejected, command failed error info is returned (object)
Type
Promise.<object>

startPlc(adsPort) → {Promise.<object>}

Source:

Starts the PLC runtime from settings.targetAdsPort or given ads port

WARNING: Make sure the system is ready to start

Parameters:
Name Type Description
adsPort number

Target ADS port, for example 851 for TwinCAT 3 PLC runtime 1 (default: this.settings.targetAdsPort)

Returns:

Returns a promise (async function)

  • If resolved, command was successful
  • If rejected, command failed error info is returned (object)
Type
Promise.<object>

stopPlc(adsPort) → {Promise.<object>}

Source:

Stops the PLC runtime from settings.targetAdsPort or given ads port

WARNING: Make sure the system is ready to stop

Parameters:
Name Type Description
adsPort number

Target ADS port, for example 851 for TwinCAT 3 PLC runtime 1 (default: this.settings.targetAdsPort)

Returns:

Returns a promise (async function)

  • If resolved, command was successful
  • If rejected, command failed error info is returned (object)
Type
Promise.<object>

subscribe(variableName, callback, cycleTime, onChange, initialDelay) → {Promise.<object>}

Source:

Subscribes to variable value change notifications

Parameters:
Name Type Default Description
variableName string

Variable name in the PLC (full path) - Example: 'MAIN.SomeStruct.SomeValue'

callback subscriptionCallback

Callback function that is called when notification is received

cycleTime number 10

How often the PLC checks for value changes (milliseconds) - Default 10 ms

onChange boolean true

If true (default), PLC sends the notification only when value has changed. If false, the value is sent every cycleTime milliseconds

initialDelay number 0

How long the PLC waits for sending the value - default 0 ms (immediately)

Returns:

Returns a promise (async function)

  • If resolved, subscribing is successful and notification data is returned (object)
  • If rejected, subscribing failed and error info is returned (object)
Type
Promise.<object>

subscribeRaw(indexGroup, indexOffset, size, callback, cycleTime, onChange, initialDelay) → {Promise.<object>}

Source:

Subscribes to variable value change notifications by index group and index offset

Parameters:
Name Type Default Description
indexGroup number

Variable index group in the PLC

indexOffset number

Variable index offset in the PLC

size number

Variable size in the PLC (enter 0xFFFFFFFF if not known)

callback subscriptionCallback

Callback function that is called when notification is received

cycleTime number 10

How often the PLC checks for value changes (milliseconds) - Default 10 ms

onChange boolean true

If true (default), PLC sends the notification only when value has changed. If false, the value is sent every cycleTime milliseconds

initialDelay number 0

How long the PLC waits for sending the value - default 0 ms (immediately)

Returns:

Returns a promise (async function)

  • If resolved, subscribing is successful and notification data is returned (object)
  • If rejected, subscribing failed and error info is returned (object)
Type
Promise.<object>

unsubscribe(notificationHandle) → {Promise.<object>}

Source:

Unsubscribes from the PLC variable value change notifications

Parameters:
Name Type Description
notificationHandle number

Notification handle for the notification

Returns:

Returns a promise (async function)

  • If resolved, subscribing is successful and notification data is returned (object)
  • If rejected, subscribing failed and error info is returned (object)
Type
Promise.<object>

unsubscribeAll() → {Promise.<object>}

Source:

Unsubscribes from all active user-added subscriptions

Returns:

Returns a promise (async function)

  • If resolved, subscribing from all was successful and number of unsubscribed notifications is returned (object)
  • If rejected, subscribing failed for some of the notifications, number of successful, number of failed and error info are returned (object)
Type
Promise.<object>

writeControl(adsPort, adsState, deviceStateopt, dataopt) → {Promise.<object>}

Source:

Sends a WriteControl ADS command to the given ADS port. WriteControl can be used to start/stop PLC, set TwinCAT system to run/config and so on

NOTE: Commands will fail if already in requested state

Parameters:
Name Type Attributes Default Description
adsPort number

Target ADS port (TC3 runtime 1: 851, system manager: 10000 and so on)

adsState number

ADS state to be set (see ADS.ADS_STATE for different values)

deviceState number <optional>
0

device state to be set (usually 0) - Default: 0

data Buffer <optional>

Additional data to be send (Buffer object - usually empty) - Default: none

Returns:

Returns a promise (async function)

  • If resolved, command was successful
  • If rejected, command failed error info is returned (object)
Type
Promise.<object>

writeRaw(indexGroup, indexOffset, dataBuffer, targetAdsPortopt) → {Promise.<object>}

Source:

Writes (raw byte) data to PLC with given index group and index offset

Parameters:
Name Type Attributes Default Description
indexGroup number

Variable index group in the PLC

indexOffset number

Variable index offset in the PLC

dataBuffer Buffer

Buffer object that contains the data (and byteLength is acceptable)

targetAdsPort number <optional>
null

Target ADS port (optional) - default is this.settings.targetAdsPort

Returns:

Returns a promise (async function)

  • If resolved, writing was successful
  • If rejected, writing failed and error info is returned (object)
Type
Promise.<object>

writeRawByHandle(handle, dataBuffer) → {Promise.<object>}

Source:

Writes (raw byte) data to PLC by previously created variable handle

Parameters:
Name Type Description
handle number

Variable handle to write to (created previously using createVariableHandle())

dataBuffer Buffer

Buffer object that contains the data (and byteLength is acceptable)

Returns:

Returns a promise (async function)

  • If resolved, writing was successful
  • If rejected, writing failed and error info is returned (object)
Type
Promise.<object>

writeRawBySymbol(symbol, dataBuffer) → {Promise.<object>}

Source:

Writes (raw byte) data to PLC by given symbol info

Parameters:
Name Type Description
symbol object

Object (PLC symbol - read for example with getSymbolInfo() method) that contains at least {indexGroup, indexOffset}

dataBuffer Buffer

Buffer object that contains the data (and byteLength is acceptable)

Returns:

Returns a promise (async function)

  • If resolved, writing was successful
  • If rejected, writing failed and error info is returned (object)
Type
Promise.<object>

writeRawMulti(targetArray, targetAdsPortopt) → {Promise.<Array.<WriteRawMultiResult>>}

Source:

Writes multiple (raw byte) data to PLC by given index group and index offset

All required parameters can be read for example with getSymbolInfo() method, see also readRawBySymbol()

Parameters:
Name Type Attributes Default Description
targetArray Array.<WriteRawMultiParam>

Targets to write to

targetAdsPort number <optional>
null

Target ADS port (optional) - default is this.settings.targetAdsPort

Returns:

Returns a promise (async function)

  • If resolved, writing was successful and data is returned (Buffer)
  • If rejected, reading failed and error info is returned (object)
Type
Promise.<Array.<WriteRawMultiResult>>

writeSymbol(variableName, value, autoFill) → {Promise.<object>}

Source:

Writes given PLC symbol value

Parameters:
Name Type Default Description
variableName string

Variable name in the PLC - Example: 'MAIN.SomeStruct'

value object

Value to write

autoFill boolean false

If true and variable type is STRUCT, given value can be just a part of it and the rest is kept the same. Otherwise they must match 1:1

Returns:

Returns a promise (async function)

  • If resolved, write is successful and value and data type are returned {value, type} (object)
  • If rejected, writing or parsing given data failed and error info is returned (object)
Type
Promise.<object>