-
Notifications
You must be signed in to change notification settings - Fork 7
/
SiemensClient.cs
63 lines (53 loc) · 1.77 KB
/
SiemensClient.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
namespace Opc.Ua.Edge.Translator
{
using Opc.Ua.Edge.Translator.Interfaces;
using S7.Net;
using Serilog;
using System;
using System.Threading.Tasks;
public class SiemensClient : IAsset
{
private Plc _S7 = null;
private string _endpoint = string.Empty;
public void Connect(string ipAddress, int port)
{
try
{
_endpoint = ipAddress;
_S7 = new Plc(CpuType.S71500, _endpoint, 0, (short)port);
_S7.Open();
byte result = _S7.ReadStatus();
if (result != 0x8)
{
throw new Exception("S7 status error: " + result.ToString());
}
Log.Logger.Information("Connected to Siemens S7: " + result.ToString());
}
catch (Exception ex)
{
Log.Logger.Error(ex.Message, ex);
}
}
public void Disconnect()
{
if (_S7 != null)
{
_S7.Close();
_S7 = null;
}
}
public string GetRemoteEndpoint()
{
return _endpoint;
}
public Task<byte[]> Read(string addressWithinAsset, byte unitID, string function, ushort count)
{
return Task.FromResult(_S7.ReadBytes(DataType.DataBlock, unitID, int.Parse(addressWithinAsset), count));
}
public Task Write(string addressWithinAsset, byte unitID, string function, byte[] values, bool singleBitOnly)
{
_S7.WriteBytes(DataType.DataBlock, unitID, int.Parse(addressWithinAsset), values);
return Task.CompletedTask;
}
}
}