This is a C# wrapper for the Beckhoff ADS library on Windows.
It is a standalone ADS client that can be used on Windows PC without anye TwinCAT installation on it.
ADS library is based on the Beckhoff ADS repo
Note: If you use this standalone ADS client on PC with TwinCAT installed, it might clashes with the ADS router
The wrapper have the following functionalities:
- Add a route to a remote target.
- Get remote AMS Net ID.
- Get the ADS state.
- Set the ADS state.
- Get the device info.
- Read/Write symbol by name.
Clone this repository and initialize the ADS submodule:
git clone https://github.com/rassrollers/AdsWrapperCsharp.git
cd AdsWrapperCsharp
git submodule init
git submodule update- Create a new C# Console App project in the same solution.
- Add project references to both
AdsLibandAdsWrapper:- Right-click on your C# project → Add → Project Reference → Select both
AdsLibandAdsWrapper
- Right-click on your C# project → Add → Project Reference → Select both
- Setup platform settings to match the wrapper project:
- Project Properties → General → Target OS =
Windows - Project Properties → General → Target OS version =
7.0
- Project Properties → General → Target OS =
Here's a basic example of how to use the ADS wrapper in your C# project:
using AdsWrapper;
class Program
{
static void Main()
{
var localIp = "192.168.1.119";
var localNetId = "192.168.1.119.1.1";
var remoteIp = "192.168.1.10";
var routeName = "C6015";
var userName = "Administrator";
var password = "1";
using var ads = new AdsDeviceWrapper(localIp, localNetId);
var remoteNetId = ads.GetRemoteNetId(remoteIp);
ads.AddRemoteRoute(routeName, remoteIp, remoteNetId, AmsPort.TC3Runtime1, userName, password);
var info = ads.GetDeviceInfo();
Console.WriteLine($"Device info: {info}");
var state = ads.GetState();
Console.WriteLine($"ADS state: {state}");
Console.WriteLine($"Set PLC in run");
ads.SetTwinCatState(AdsState.Run, 0);
double position = ads.ReadSymbol<double>("MAIN.axisPosition");
Console.WriteLine($"Axis position: {position}");
Console.WriteLine("Axis powered on");
ads.WriteSymbol<bool>("MAIN.powerOnAxis", true);
}
}Getting the static and dynamic library to build in Visual Studio.
- Make a C++ Static Library project.
- Add the header and source files from the ADS GIT repo.
- Select all .cpp files and disable Precompiled Header.
- C/C++ -> Precompiled Headers -> Precompiled Header =
Not Using Precompiled Headers
- C/C++ -> Precompiled Headers -> Precompiled Header =
- Add additional include path to the ADS library file in the project properties.
- C/C++ -> General -> Additional Include Directories =
$(ProjectDir)..\ADS\AdsLib
- C/C++ -> General -> Additional Include Directories =
- Changes the output files directory to not get error on duplicated files (standalone/TwinCAT directories).
- C/C++ -> Output Files -> Object File Name =
$(IntDir)%(RelativeDir)
- C/C++ -> Output Files -> Object File Name =
- Add the following to the Preprocessor Defines:
CONFIG_DEFAULT_LOGLEVEL=1_CRT_SECURE_NO_WARNINGSNOMINMAX
- Exclude the header and source files from the AdsLib/TwinCAT directory for building an standalone library.
- Require you to have TwinCAT installed.
- Make a C++ Dynamic-Link Library (DLL) project.
- Add projet reference to the AdsLib project.
- Setup the C++/CLI properties
- Advanced -> Common Language Runtime Support =
.NET Runtime Support (/clr:netcore) - Advanced -> .NET Target Windows Version =
7.0
- Advanced -> Common Language Runtime Support =
- Add additional include path to the ADS library file in the project properties.
- C/C++ -> General -> Additional Include Directories =
$(ProjectDir)..\ADS\AdsLib
- C/C++ -> General -> Additional Include Directories =
Add the following code to the pch.h header to fix the winsock issues:
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")- Make a C# Console App.
- Add projet reference to the AdsLib project.
- Setup platform settings to match wrapper project.
- General -> Target OS =
Windows - General -> Target OS version =
7.0
- General -> Target OS =