Sample03: ADS client UI in C#

This article describes the ADS client, which sends ADS messages to the previously described ADS server.

The implementation of the ADS server depends neither on the language (C++ / C# / PLC / ...) nor on the TwinCAT version (TwinCAT 2 or TwinCAT 3).

Download

Here you can access the source code for this sample.

This code requires .NET Framework 3.5 or higher!
1. Unpack the downloaded ZIP file.
2. Open the sln file contained in it with Visual Studio.
3. Create the sample on your local machine (right-click on the project and click on Build).
4. Start the program with a right-click on Project, Debug->Start new instance.

Description

This client performs two tasks:

Using the client

Selecting a communication partner

Enter both ADS parameters in order to specify your ADS communication partner:

Create link with communication partner

Click on Connect to call the method TcAdsClient.Connect for the purpose of creating a link with the configured port.

Sample03: ADS client UI in C# 1:

ADS messages are sent to the ADS server with the help of the Start / Read / Stop / Overwrite / Reset buttons.
The specific indexGroup / indexOffset commands were already designed in the ADS interface of the ADS server.

The result of clicking on the command buttons can also be seen in the module instance in the Parameters (online) tab.

Sample03: ADS client UI in C# 2:

C# program

Here is the "Core" code of the ADS client – download for the GUI or ZIP file above.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TwinCAT.Ads;

namespace adsClientVisu
{
public partial class form : Form
{
    public form()
    {
     InitializeComponent();    
    }

    private void Form1_Load(object sender, EventArgs e)
    {
     // create a new TcClient instance    
     _tcClient = new TcAdsClient();
     adsReadStream = new AdsStream(4);
     adsWriteStream = new AdsStream(4);
    }

    /*
     * Connect the client to the local AMS router
     */

    private void btConnect_Click(object sender, EventArgs e)
    {
     AmsAddress serverAddress = null;
     try
     {
         serverAddress = new AmsAddress(tbNetId.Text,
                         Int32.Parse(tbPort.Text));
     }
     catch
     {
         MessageBox.Show("Invalid AMS NetId or Ams port");
         return;
     }

     try
     {
         _tcClient.Connect(serverAddress.NetId, serverAddress.Port);
         lbOutput.Items.Add("Client port " + _tcClient.ClientPort + " opened");

     }
     catch
     {
         MessageBox.Show("Could not connect client");
     }
    }

    private void btStart_Click(object sender, EventArgs e)
    {
     try
     {
         _tcClient.ReadWrite(0x1, 0x1, adsReadStream, adsWriteStream);
         byte[] dataBuffer = adsReadStream.ToArray();
         lbOutput.Items.Add("Counter started value = " + BitConverter.ToInt32(dataBuffer, 0));
     }
    
     catch (Exception err)
     {
         MessageBox.Show(err.Message);
     }
    
    }

    private void btRead_Click(object sender, EventArgs e)
    {
     try
     {
         _tcClient.ReadWrite(0x1, 0x2, adsReadStream, adsWriteStream);
         byte[] dataBuffer = adsReadStream.ToArray();
         lbOutput.Items.Add("Counter = " + BitConverter.ToInt32(dataBuffer, 0));
     }

     catch (Exception err)
     {
         MessageBox.Show(err.Message);
     }
    }

    private void btStop_Click(object sender, EventArgs e)
    {
     try
     {
         _tcClient.ReadWrite(0x2, 0x1, adsReadStream, adsWriteStream);
         byte[] dataBuffer = adsReadStream.ToArray();
         lbOutput.Items.Add("Counter stopped value = " + BitConverter.ToInt32(dataBuffer, 0));
     }

     catch (Exception err)
     {
         MessageBox.Show(err.Message);
     }
    }

    private void btReset_Click(object sender, EventArgs e)
    {
     try
     {
         _tcClient.ReadWrite(0x2, 0x2, adsReadStream, adsWriteStream);
         byte[] dataBuffer = adsReadStream.ToArray();
         lbOutput.Items.Add("Counter reset Value = " + BitConverter.ToInt32(dataBuffer, 0));
     }

     catch (Exception err)
     {
         MessageBox.Show(err.Message);
     }
    }
}
}