TwinCAT Samples LabVIEW Overview

NI LabVIEW und TwinCAT ADS :: Overview

Overview

Examples of use TwinCAT ADS DLL

Description | Source
PLC project for all samples | PLC_Demo.pro
Sample 1: Read DLL version | Sample 001.zip
Sample 2: Write flag in PLC synchronously | Sample 002.zip
Sample 3: Read flag in PLC synchronously | Sample 003.zip
Sample 4: Read ADS state | Sample 004.zip
Sample 5: Read ADS Information | Sample 005.zip
Sample 6: Start/Stop PLC | Sample 006.zip
Sample 7: Access by name on an array in the PLC | Sample 007.zip

Examples of use TwinCAT ADS OCX

Description | Source
PLC project for testing the samples | PLC_Demo.pro
Sample 1: Read AdsState | Sample 001.zip
Sample 2: synchron methods: Read via address | Sample 002.zip
Sample 3: synchron methods: Read via name | Sample 003.zip
Sample 4: synchron methods: Write via address | Sample 004.zip
Sample 5: synchron methods: Write via name | Sample 005.zip
Sample 6: Event driven reading:registering Callback-vi | Sample 006.zip
Sample 7: Event driven reading: variables | Sample 007.zip
Sample 8: Event driven reading: structure variables | Sample 008.zip
Sample 9: Event driven reading: structure variables with reference passing to Callback-vi | Sample 009.zip
Sample 10: Reading IndexGroup and IndexOffset of variable name | Sample 010.zip

Examples of use TwinCAT ADS NET

Description | Source
Sample 1: Accessing an array in the PLC | Sample 01.zip
Sample 2: Transmitting structures to the PLC | Sample 02.zip
Sample 3: Event driven reading | Sample 03.zip
Sample 4: Reading and writing of string variables | Sample 04.zip
Sample 5: Reading and writing of DATE/TIME variables | Sample 05.zip
Sample 6: Read PLC variable declaration | Sample 06.zip
Sample 7: Reading and writing of PLC variables of any type (ReadAny, WrtieAny) | Sample 07.zip


TwinCAT TcAdsDll: Integration in LabView

TwinCAT TcAdsDLL: Reference

Integration in LabView

Necessary files:

  • TcAdsDll.dll - dynamic function library

The TcAdsDll.dll is located in the ‚System32' directory of windows.

LabView Function: Call Library Function

Configuration of DLL call

Library Name or Path

Use the button "Browse..." or enter the path of TcAdsDll here.

Function Name

Selcet the name of the DLL function to be called.

Calling Conventions

The calling convention stdCall is used.

Parameter

The parameter list has to be adapt to the called function. The parameter list of the call is completed via the buttons 'Add a Parameter Before', 'Add a Parameter After' and 'Delete this Parameter' .The data types are adapted to the call parameter.

For the pointer passing of structures the type 'Adapt to Type' and the data format 'Handles by Value' is selected.

Example: Parameter pAddr of function AdsSyncReadReq

Function Prototype

The function prototype of the called function has to take issue with the function prototype of the TcAdsDll. The function prototypes are listed in the documentation TcAdsDll functions.

Example for the function AdsSyncReadReq

C declaration:

LONG AdsSyncReadReq(
  PAmsAddr  pAddr,
  ULONG     nIndexGroup,
  ULONG     nIndexOffset,
  ULONG     nLength,
  PVOID     pData
);

LabView:

Structures

For all structures used by the function calls corresponding clusters a created in the LabView.

AmsNetId

C Declaration:

typedef struct {
  UCHAR        b[6];
} AmsNetId, *PAmsNetId;

Is built in LabView as cluster of 6 x U8 values:

AmsAdr

C: Declaration

typedef struct {
  AmsNetId        netId;
  USHORT          port;
} AmsAddr, *PAmsAddr;

Is built in LabView as cluster of AmsNetId and a U16 value:

From this clusters a LabView control ( e,g, for adress input) can be created.:

Parameter passing

For parameter passing it is important to pass all parameter variables to the Dll call. Especially to the parameter marked with [out] in the documentation a place holder variable has to be passed, so that accordant memory is reserved.

Note

A false parameter passing causes memory protection breach. This can create a LabView system crash!


Read DLL version

This program determines the version of DLL file.

Extract sample program 'read DLL version'.


Write flag in PLC synchronously

In this sample the value, which is given by the user, is written into the flag word 502.

Sample: 'Write flag in PLC synchronously'.


Read flag in PLC synchronously

In this sample program the value from flag word 2 of the PLC is read and displayed on the screen.

Extract sample program 'Read flag in PLC synchronously'.


Read ADS state

This program reads the status of the PLC. The variable of type ADSSTATE contains information if the PLC is in RUN mode or STOP mode.

Extract sample program 'Read ADS state'.


Read ADS information

Each ADS device contains a version number and an identifier. The sample program reads these information from the PLC and displays them on the screen.

Extract sample program 'Read ADS information' .


Start/stop PLC

The following program starts/stops the run-time system 1 of the PLC.

Extract sample program 'Start/stop PLC'.


Array access in PLC via name

TwinCAT TcAdsDLL: Beispiele

Access by name on an array in the PLC

An array included in the PLC is to be read with the read command. The variable is called by the variable name. This procedure differs only marginally from the reading of discrete variables.
The length of the total array is the length of the function AdsSyncReadReq() .

Extract sample program 'Access by name on an array in the PLC'.


Integration in LabView

  1. Create ActiveX container

  2. Insert ActiveX Object

  3. AdsOcx Element in LabView

    Panel:

    Diagram:


Samples using AdsOcx properties

  1. Set EnableErrorHandling true.

  2. Set AdsAmsServerNetId and AdsAmsServerPort to fix values.

  3. Access to local PLC by reading and setting the AmsNetId

  4. Monitoring the status of the ADS device (Sample_OCX_001_AdsStateRead.zip)


synchron methods: Read via address

AdsSyncReadBoolReq,

AdsSyncReadIntegerReq,

AdsSyncReadLongReq,

AdsSyncReadSingleReq,

AdsSyncReadDoubleReq,

AdsSyncReadStringReq

Example AdsSyncReadBoolReq

PLC declaration:

    TCtoLV_boolVal AT%MX0.0: BOOL;

LabView (Sample_OCX_002_VarPerAdrRead.zip):


synchron methods: Read via name

AdsSyncReadBoolVarReq

AdsSyncReadIntegerVarReq

AdsSyncReadLongVarReq

AdsSyncReadSingleVarReq

AdsSyncReadDoubleVarReq

AdsSyncReadStringVarReq

Samplel: AdsSyncReadBoolVarReq

PLC declaration:

    TCtoLV_boolVal AT%MX0.0: BOOL;

LabView: (Sample_OCX_003_VarPerNameRead.zip)


synchron methods: Write via address

AdsSyncWriteBoolReq

AdsSyncWriteIntegerReq

AdsSyncWriteLongReq

AdsSyncWriteSingleReq

AdsSyncWriteDoubleReq

AdsSyncWriteStringReq

Sample: AdsSyncWriteBoolReq

PLC declaration:

    LVtoTC_boolVal AT%MX500.0: BOOL;

LabView: (Sample_OCX_004_VarPerAdrWrite.zip)


synchron methods: Write via name

AdsSyncWriteBoolVarReq

AdsSyncWriteIntegerVarReq

AdsSyncWriteLongVarReq

AdsSyncWriteSingleVarReq

AdsSyncWriteDoubleVarReq

AdsSyncWriteStringVarReq

Sample: AdsSyncWriteBoolVarReq

PLC declaration:

    LVtoTC_boolVal AT%MX500.0: BOOL;

LabView: (s. Sample_DLL_005_AdsInfoRead.zip)


Ereignisgesteuertes Lesen, Registrieren eines Callback-vi

TwinCAT ADS-OCX Integration in LabView

Event driven reading, registering Callback-vi

Sample files: Sample_OCX_006_Callback.zip

  1. To use a asynchronous method a callback-vi is registered that is called by the AdsOcx.

  2. The event element is connected with the AdsOcx reference, and the event to be called is selected.

  3. The Callback-Vi must have a certain parameter structure. The Callback-Vi can be created by LabView.

  4. The Event-Callback-vi is to be stored with a definite name.


Ereignisgesteuertes Lesen

TwinCAT ADS-OCX Integration in LabView

Event driven reading, simple data types

Method: AdsReadVarConnectEx

Sample: Sample_OCX_007_VarEventRead.zip

PLC declaration:

    TCtoLV_boolVal AT%MX0.0: BOOL;

A Callback-Vi is registered for the event AdsReadConnectUpdateEx.

The method AdsReadVarConnectEx is a fix connection between LabView and a PLC variable. The returned handle identifies the connection. If the connection is no longer required, it is disconnecetd by AdsDisconnectEx.

By calling the Callback-Vi the data are passed as variant. With the handle the variables can be changed into the right type and assigned to the right global LabView variable.


Event driven reading, structure variables

Method: AdsReadVarConvertConnect

Sample : Sample_OCX_008_StrucVarEventRead.zip

TwinCAT declaration:

    TYPE ST_DataExchange :

    STRUCT arrBool:

        ARRAY[0..63] OF BOOL;

        arrInt: ARRAY[0..63] OF INT;

        arrReal: ARRAY[0..63] OF REAL;

    END_STRUCT END_TYPE

    stTCtoLV AT%MB1000: ST_DataExchange;

  1. Registrate Callback-Vi fotr the event method AdsReadConvertConnectUpdate.

  2. Global variables:
    - create cluster variable as illustration of the TwinCAT structure.
    - create global handle variable for the determination of the events.

  3. Initialise the data structure as illustration of the TwinCAT structure

  4. Create data connection and store the connection handle

  5. Cyclic access to global data and disconnect the connection

  6. Event handling in the Callback-Vi

    With the passed handle hConnect the Callback-Vi decides for which variable the event has been called. It assigns the in data passed value to the right variable.


Event driven reading with data reference passing to Callback-vi

If only one variable is to be read by connect, the reference to the variable can be passed to the Callbak-Vi. Thus the using of global variables is not necessary.

The Callback-Vi writes by reference directly to the variable of the calling Vi.

Sample: Sample_OCX_009_StrucVarEventReadCallback.zip

  1. A LabView display element of the correct type is created and initialized.

  2. Create reference to element.

  3. Passing the reference to CallbackVi

  4. Access to the reference variable in the Callback-Vi
    The variant variable (without type) has to be changed into the correct data type and passed to the reference variable.


General Methods

  1. The methods AdsCreateVarHandle and AdsDeleteVarHandle are used to access to the PLC variable by name.
    PLC declaration:
    TCtoLV_boolVal AT%MX0.0: BOOL;
    LabView:

  2. The methods AdsAmsConnect and AdsAmsDisconnect are called in the start and end phase to log on/log off the AdsOcx at the router.

    If the AdsOcx is logged off at the router per AdsAmsDisconnect, AdsAmsConnect has to be called before a next call of a AdsOcx method, or LabView has to be restarted.

  3. The method AdsReadSymbolDesc
    With the method AdsReadSymbolDesc information about a named PLC variable can be read during the runtime. For example the adress data nIndexGroup and nIndexOffset can be read, to be able to acces to the variable by address (maybe with the TcAdsDll) (Sample_OCX_010_ReadIndexGroup.zip)


Integration in LabView

To be able to use a TwinCAT.Ads object in a LabView program you have to insert a construction node from the palette :NET functions in the block diagram. The construction node gives a reference to the .NET object.

The palette .NET functions contains futhermore feature nodes for defining or reading the object features, and method nodes for calling a method at the project that is given by the reference.

After Inserting or later after a double click on the construction node the followuing dialog opens. Here you can select the .Net Assembly TwinCAT.Ads.dll and the object TcAdsClient .

Create a connection to the Ads decvice:

Reading a PLC variable:

The data are trensmitted by the aid of the AdsStream class. The class AdsBinaryReader is responsible for reading the data from the stream.

Writing a PLC variable:

To write data in the stream, you can use the class AdsBinaryWriter.

Disconnect the connection to the Ads device:


Accessing an array in the PLC

System requirements

  • LabView 8.5 or higher

Extract sample program .


Transmitting structures to the PLC

System requirements

  • LabView 8.5 or higher

Extract sample program.


Event driven reading

System requirements

  • LabView 8.5 or higher

Extract sample program.


Reading and writing of string variables

System requirements

  • LabView 8.5 or higher

Extract sample program.


Reading and writing of TIME/DATE variables

System requirements

  • LabView 8.5 or higher

Extract sample program.


Read PLC variable declaration

System requirements

  • LabView 8.5 or higher

Extract sample program.


Reading and writing of PLC variables of any type (ReadAny, WriteAny)

System requirements

  • LabView 8.5 or higher

Extract sample program.