-
Notifications
You must be signed in to change notification settings - Fork 7
/
MitsubishiClient.cs
74 lines (59 loc) · 2.02 KB
/
MitsubishiClient.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
namespace Opc.Ua.Edge.Translator
{
using MCProtocol;
using Opc.Ua.Edge.Translator.Interfaces;
using Serilog;
using System;
using System.Threading.Tasks;
public class MitsubishiClient : IAsset
{
private string _endpoint = string.Empty;
public void Connect(string ipAddress, int port)
{
try
{
_endpoint = ipAddress + ":" + port.ToString();
PLCData.PLC = new Mitsubishi.McProtocolTcp(ipAddress, port, Mitsubishi.McFrame.MC3E);
int result = PLCData.PLC.Open().GetAwaiter().GetResult();
Log.Logger.Information("Connected to Mitsubishi PLC: " + result.ToString());
}
catch (Exception ex)
{
Log.Logger.Error(ex.Message, ex);
}
}
public void Disconnect()
{
if (PLCData.PLC != null)
{
PLCData.PLC.Close();
PLCData.PLC = null;
}
}
public string GetRemoteEndpoint()
{
return _endpoint;
}
public Task<byte[]> Read(string addressWithinAsset, byte unitID, string function, ushort count)
{
PLCData<byte> data = new PLCData<byte>((Mitsubishi.PlcDeviceType)unitID, int.Parse(addressWithinAsset), count);
data.ReadData();
byte[] result = new byte[count];
for (int i = 0; i < count; i++)
{
result[i] = data[i];
}
return Task.FromResult(result);
}
public Task Write(string addressWithinAsset, byte unitID, string function, byte[] values, bool singleBitOnly)
{
PLCData<byte> data = new PLCData<byte>((Mitsubishi.PlcDeviceType)unitID, int.Parse(addressWithinAsset), values.Length);
for (int i = 0; i < values.Length; i++)
{
data[i] = values[i];
}
data.WriteData();
return Task.CompletedTask;
}
}
}