Integration in Microsoft Visual Studio .NET (VB)


Create new project

The TwinCAT.Ads component can only be integrated in an existing project. Start Visual Studio and select the following command from the menu: File->New->Project...



 

In the New Project dialog please select as project type: Visual Basic->Windows Forms Application.

Integration in Microsoft Visual Studio .NET (VB) 1:

 

 

Adding a reference

To select the class library TwinCAT.Ads select the command Add Reference... from the Project menu. The Add Reference dialog opens.
Alternatively you double-click on My Project (1) to open the Project Properties window and click the Add... button (3) under Reference (2).

Integration in Microsoft Visual Studio .NET (VB) 2:

 

In this dialog you should select the Browse tab and navigate to file TwinCAT.Ads.dll. Confirm the selected dll with OK. By default the TwinCAT.Ads component is located in the ..TwinCAT\Ads Api\.NET\ folder.

Integration in Microsoft Visual Studio .NET (VB) 3:

 

Add namespace

All class library types belong to the TwinCAT.Ads namespace. Therefore, the following lines should be added at the beginning of the source text:

Imports TwinCAT.Ads
Imports System.IO


This enables access to the data types defined in TwinCAT.Ads without having to specify the namespace repeatedly. The TcAdsClient class is the core of the TwinCAT.Ads class library and enables communication with an Ads device. The first step is to create an instance of the TcAdsClient class. Then make use of the Connect method to establish a connection with the ADS device.

 

Establishing a connection to an Ads device:

Private tcAds As TcAdsClient
...

tcAds = New TcAdsClient
tcAds.Connect("1.2.3.4.1.1", 801)

...


The data are transmitted with the aid of the AdsStream class, which is derived from System.IO.MemoryStream. The class System.IO.BinaryWriter can be used to write data to the stream, the class System.IO.BinaryReader can be used to read data from the stream.

Reading a PLC variable:

' creates a stream with a length of 4 byte
Dim ds = New AdsStream(4)
Dim br = New BinaryReader(ds)

' reads a DINT from PLC
tcAds.Read(&H4020, 0, ds)
ds.Position = 0
TextBox1.Text = br.ReadInt32().ToString()

 

Writing a PLC variable:

' creates a stream with a length of 4 byte
Dim ds = New AdsStream(4)
Dim bw = New BinaryWriter(ds)

ds.Position = 0
bw.Write(New Integer = 100)

' writes a DINT to PLC
tcAds.Write(&H4020, 0, ds)

 

Separating a connection to an Ads device:

tcAds.Dispose()

 

PLC program:

PROGRAM MAIN
VAR
    test AT%MD0 : DINT;
END_VAR
test := test + 1