-
Notifications
You must be signed in to change notification settings - Fork 7
/
BeckhoffClient.cs
95 lines (81 loc) · 3.73 KB
/
BeckhoffClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
namespace Opc.Ua.Edge.Translator
{
using Opc.Ua.Edge.Translator.Interfaces;
using Serilog;
using System;
using System.Threading.Tasks;
using Viscon.Communication.Ads;
using Viscon.Communication.Ads.Common;
/*
Authorise this Beckhoff ADS client for accessing the Beckhoff PLC by adding an AMS route.
-----------------------------------------------------------------------------------------
TwinCAT Engineering: Go to the tree item SYSTEM/Routes and add a static route.
TwinCAT Systray: Open the context menu by right clicking the TwinCAT systray icon. (not available on Windows CE devices)
TC2: Go to Properties/AMS Router/Remote Computers and restart TwinCAT
TC3: Go to Router/Edit routes.
TcAmsRemoteMgr: Windows CE devices can be configured locally (TC2 requires a TwinCAT restart). Tool location: /Hard Disk/System/TcAmsRemoteMgr.exe
IPC Diagnose: Beckhoff IPC’s provide a web interface for diagnose and configuration.
Further information: http://infosys.beckhoff.de/content/1033/devicemanager/index.html?id=286
Sample AMS route:
Name: UA-EdgeTranslator
AMS Net Id: 192.168.0.1.1.1 # NetId of UA-EdgeTranslator, derived from its IP address
Address: 192.168.0.1 # IP address of UA-EdgeTranslator
Transport Type: TCP/IP
Remote Route: None / Server
Unidirectional: false
Secure ADS: false
*/
public class BeckhoffClient : IAsset
{
private AdsClient _adsClient = null;
private string _endpoint = string.Empty;
public void Connect(string ipAddress, int port)
{
try
{
_endpoint = ipAddress + ":" + port.ToString();
string[] addresses = ipAddress.Split(':');
if (addresses.Length == 2)
{
string localEndpoint = addresses[0] + ".1.1";
string remoteEndpoint = addresses[1] + ".1.1";
_adsClient = new AdsClient(localEndpoint, addresses[1], remoteEndpoint, (ushort)port);
_adsClient.RequestTimeout = AdsClient.DefaultRequestTimeout * 2;
_adsClient.Ams.ConnectAsync().GetAwaiter().GetResult();
AdsDeviceInfo result = _adsClient.ReadDeviceInfoAsync().GetAwaiter().GetResult();
Log.Logger.Information("Connected to Beckhoff TwinCAT ADS PLC: " + result.ToString());
}
else
{
throw new ArgumentException("Expected ipAddress to contain both the local and remote AMS ip addresses, seperated by a ':'");
}
}
catch (Exception ex)
{
Log.Logger.Error(ex.Message, ex);
}
}
public void Disconnect()
{
if (_adsClient != null)
{
_adsClient = null;
}
}
public string GetRemoteEndpoint()
{
return _endpoint;
}
public Task<byte[]> Read(string addressWithinAsset, byte unitID, string function, ushort count)
{
uint varHandle = _adsClient.GetSymhandleByNameAsync(addressWithinAsset).GetAwaiter().GetResult();
byte[] result = _adsClient.ReadBytesAsync(varHandle, count).GetAwaiter().GetResult();
return Task.FromResult(result);
}
public Task Write(string addressWithinAsset, byte unitID, string function, byte[] values, bool singleBitOnly)
{
_adsClient.WriteBytesAsync(uint.Parse(addressWithinAsset), values);
return Task.CompletedTask;
}
}
}