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:
- Testing the ADS-server, which was described before.
- Providing sample code for implementing a ADS-client
Using the client
Selecting a communication partner
Enter both ADS parameters in order to specify your ADS communication partner:
- NetID:
127.0.0.1.1.1 (for ADS partner also linked with local ADS Message Router)
Enter another NetID, if you want to communicate with an ADS partner connected to another ADS router via the network.
First you have to create an ADS route between your device and the remote device.
- AdsPort
Enter the AdsServerPort of your communication partner.
Do not confuse the ADS server port (which has explicitly implemented your own message handler) with the regular ADS port for the purpose of access to symbols (this is provided automatically, without the need for user intervention).
Find the assigned AdsPort, in this sample the AdsPort was 0x8235 (dec 33333).
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.
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.
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);
}
}
}
}