Options
All
  • Public
  • Public/Protected
  • All
Menu

Class TcCom

Class responsible for establishing connection and managing all communication and data transformation to and from the Target PLC over TwinCAT's ADS layer.

Is used as a wrapper for the ads-client library.

Hierarchy

Index

Constructors

constructor

  • Constructor, which stores the TcComSettings used for establishing communication, as well as the callback, which is triggered upon Code Change detection

    Parameters

    • context: TcContext

      Parent TcContext, of whom TcCom is part of, and whom to propagate events to

    • settings: TcComSettings

      Settings used for communicating over ADS. Definition of connection settings can be found at ads-client library

    • Optional onChange: undefined | (() => void)

      Callback, which is called when Code Changes are detected. This callback is called after the sourceChanged event is emitted

    • Default value debug: boolean = false

      If enabled, will produce debug information

    Returns TcCom

Events

on

Accessors

isValid

  • get isValid(): boolean
  • Returns true if the current TcCom Object is in a valid state, and can be used for communication

    Returns boolean

settings

Methods

Private __callback

  • __callback(response: { value: number }): void
  • Internal function, which is invoked whenever Code Changes are detected by the TcCom object. Will emit the sourceChanged event, as well as invoke a callback which was provided

    Parameters

    • response: { value: number }

      The current PLC Last code change stamp which is used to see if changes have happened

      • value: number

    Returns void

Private __splitData

  • Internal function, which splits SymbolPointers into groups of 500, due to a TwinCAT ADS limitation, used by the TcCom.read() function

    Parameters

    Returns TcSymbolPointer[][]

    • Groups of 500 Symbol Pointers
  • Internal function, which splits Data Packages into groups of 500, due to a TwinCAT ADS limitation, used by the TcCom.write() function

    Parameters

    Returns TcDataPackage[][]

    • Groups of 500 Data Packages

addListener

  • addListener(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

callMethod

  • callMethod(variable: string, method: string, parameters: any): Promise<{ outputs?: any; result: any }>
  • Performs a call to a method of a specific variable over ADS

    throws

    TcComIsInvalidException - Attempted to use an Invalid TcCom Object for subscription

    throws

    TcComMethodCallException - Failed to call the Rpc Method on the PLC Side

    Parameters

    • variable: string

      The variable name, whose method is called

    • method: string

      The name of the method that is to be called

    • parameters: any

      The parameters, which are passed to the method

    Returns Promise<{ outputs?: any; result: any }>

    • The result of the method call

disconnect

  • disconnect(): Promise<void>
  • Disconnects the previously established connection to the TwinCAT PLC, and cleans up all subscription handles. The TcCom Object is no longer usable after this point, unless TcCom.initialize() is once again called, to reestablish the connection.

    throws

    TcComUnsubscribeException - Failed to unsubscribe the Handles

    throws

    TcComDisconnectException - Failed to disconnect from the TwinCAT PLC

    Returns Promise<void>

emit

  • emit(eventName: string | symbol, e: TcEvent): boolean
  • Emits an event of eventName with data e and if parent is present will propagated that event to it, unless stopped

    Parameters

    • eventName: string | symbol

      The event name to emit

    • e: TcEvent

      The Data associated with this event

    Returns boolean

    • True if the event had listeners, false otherwise.

eventNames

  • eventNames(): Array<string | symbol>
  • Returns Array<string | symbol>

fromRaw

  • fromRaw(type: string, buffer: Buffer): Promise<boolean | number | bigint | string>
  • Converts a given Buffer of data to Javascript Data, based on the TwinCAT Type. This conversion works for primitive types, and not structured

    throws

    TcComIsInvalidException - Attempted to use an Invalid TcCom Object for conversion

    throws

    TcComFromRawException - Failed to convert the Raw Data

    Parameters

    • type: string

      The TwinCAT Type, whose data is to be converted

    • buffer: Buffer

      The Buffer of Raw Data, that is to be converted

    Returns Promise<boolean | number | bigint | string>

    • The Javascript equivalent of buffer data converted from the TwinCAT type

getMaxListeners

  • getMaxListeners(): number

initialize

  • initialize(): Promise<TcCom>
  • Initializes the TcCom Object, by establishing a connection to the TwinCAT PLC, with the previously provided TcComSettings, as well as setting up Code Change monitoring, if the Source Code on the PLC Changes, during run-time

    throws

    TcComBusyException - Connection has already been created previously

    throws

    TcComConnectException - Failed to establish a connection to the TwinCAT PLC over ADS

    throws

    TcComChangeDetectionException - Failed to set up Code Change monitoring

    Returns Promise<TcCom>

    • The initialized TcCom Object

listenerCount

  • listenerCount(type: string | symbol): number
  • Parameters

    • type: string | symbol

    Returns number

listeners

  • listeners(event: string | symbol): Function[]
  • Parameters

    • event: string | symbol

    Returns Function[]

off

  • off(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

once

  • once(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

prependListener

  • prependListener(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

prependOnceListener

  • prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

rawListeners

  • rawListeners(event: string | symbol): Function[]
  • Parameters

    • event: string | symbol

    Returns Function[]

read

  • Performs a read operation over ADS of the TwinCAT Symbol Pointers. When requesting more than 500+ packages at once, the pointers will be split in groups of 500 due to a limitation of ADS

    throws

    TcComIsInvalidException - Attempted to use an Invalid TcCom Object for subscription

    throws

    TcComDataReadException - Failed to read data pointers

    Parameters

    Returns Promise<TcDataPackage[]>

    • The data packages which were queried by the Symbol Pointers

removeAllListeners

  • removeAllListeners(event?: string | symbol): this
  • Parameters

    • Optional event: string | symbol

    Returns this

removeListener

  • removeListener(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

setMaxListeners

  • setMaxListeners(n: number): this
  • Parameters

    • n: number

    Returns this

subscribe

  • Subscribes to a TwinCAT Symbol, with a callback, which is invoked, whenever the Symbol value changes. The detection of change speed can be set through the sampling argument, in case the value changes too fast and such detection is not needed

    throws

    TcComIsInvalidException - Attempted to use an Invalid TcCom Object for subscription

    throws

    TcComSubscribeException - Failed to subscribe to the provided pointer

    Parameters

    • sampling: number

      The speed in ms of detecting change. Any change in this interval will not trigger change events

    • pointer: TcSymbolPointer

      The Symbol Pointer, to which to subscribe

    • callback: () => void

      The callback that is invoked, whenever Symbol change is detected

        • (): void
        • Returns void

    Returns Promise<TcSubscription>

    • The Subscription Handle, that can be used to unsubscribe in the future

symbols

toRaw

  • toRaw(type: string, value: boolean | number | bigint | string): Promise<Buffer>
  • Converts a primitive non-structured Javascript Value to a Buffer of Data, which can be passed to a TwinCAT Type, as specified by the type argument. This conversion works for primitive types, and not structured

    throws

    TcComIsInvalidException - Attempted to use an Invalid TcCom Object for conversion

    throws

    TcComToRawException - Failed to convert to Raw Data

    Parameters

    • type: string

      The TwinCAT Type, to whom the value is converted

    • value: boolean | number | bigint | string

      The Javascript value, which is converted to Raw Data

    Returns Promise<Buffer>

    • The Data Buffer, which can be passed to a TwinCAT Symbol of Type type, representing the passed value

types

unsubscribe

write

  • Performs a write operation over ADS to the TwinCAT PLC of the provided TcDataPackages. When sending more than 500+ packages at once, the packages will be split in groups of 500 due to a limitation of ADS

    throws

    TcComIsInvalidException - Attempted to use an Invalid TcCom Object for subscription

    throws

    TcComDataWriteException - Failed to write data packages

    Parameters

    • dataPackages: TcDataPackage[]

      The packages with symbol location and data to be send to the Target

    Returns Promise<void>

Object literals

Static Readonly defaultSettings

defaultSettings: object

The Default settings, used for connecting to a TwinCAT PLC, located at localhost. These settings are merged in, with whatever custom settings are provided during construction

disableStructPackModeWarning

disableStructPackModeWarning: boolean = true

readAndCacheDataTypes

readAndCacheDataTypes: boolean = true

readAndCacheSymbols

readAndCacheSymbols: boolean = true

targetAdsPort

targetAdsPort: number = 851

targetAmsNetId

targetAmsNetId: string = "127.0.0.1.1.1"

Generated using TypeDoc