alielie
2026-06-04 771a0e7b6b1276087515bbe94de28358cb92963b
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
using HMI.Core.Util;
using HslCommunication;
using HslCommunication.Core.Pipe;
using HslCommunication.Profinet.Siemens;
using PLCHelper.Entity;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
 
namespace PLCHelper
{
    public class S7Context
    {
        private static ConcurrentDictionary<string, SiemensS7Net> cache = new ConcurrentDictionary<string, SiemensS7Net>();
        private static ConcurrentDictionary<SiemensS7Net, int> failedCount = new ConcurrentDictionary<SiemensS7Net, int>();
 
        private static Dictionary<string, PLCTagData> dataCache = new Dictionary<string, PLCTagData>();
        public static ObservableCollection<PLCTagData> Datasource = new ObservableCollection<PLCTagData>();
 
        private static string Ip;
        private static int Port;
 
        private static readonly object lockObj = new object();
 
        public static void Init(string ip, int port)
        {
            Ip = ip;
            Port = port;
            LoadTagData();
        }
 
        public static void LoadTagData()
        {
            lock (lockObj)
            {
                dataCache.Clear();
                try
                {
                    string path = Path.Combine(PathHelper.ConfigFolder, "CurrentSensorControl", "BurnInSettingData.data");
                    if (File.Exists(path))
                    {
                        Datasource = XmlHelper.DeserializeFromXml<ObservableCollection<PLCTagData>>(path);
                    }
                    else
                    {
                        Datasource = new ObservableCollection<PLCTagData>();
                    }
                }
                catch (Exception ex)
                {
                    Datasource = new ObservableCollection<PLCTagData>();
                }
                foreach (var data in Datasource)
                {
                    dataCache[data.Name] = data;
                }
            }
        }
 
        public static SiemensS7Net Get(string name)
        {
            if (!cache.ContainsKey(name))
            {
                var client = new SiemensS7Net(SiemensPLCS.S1500);
                cache[name] = client;
                PipeTcpNet pipeTcpNet = new PipeTcpNet(Ip, Port);
                pipeTcpNet.ConnectTimeOut = 200;
                pipeTcpNet.ReceiveTimeOut = 200;
                client.CommunicationPipe = pipeTcpNet;
                client.ConnectServer();
                failedCount[client] = 0;
            }
            return cache[name];
        }
 
        private static bool GetS7Data(SiemensS7Net api, PLCTagData data, string name, out object val)
        {
            val = null;
            string address = data.Address;
 
            if (data.Type == PLCTagType.BOOL)
            {
                var res = api.ReadBool(address);
                val = res.Content;
                return res.IsSuccess;
            }
            else if (data.Type == PLCTagType.SHORT)
            {
                var res = api.ReadInt16(address);
                val = res.Content;
                return res.IsSuccess;
            }
            else if (data.Type == PLCTagType.INT)
            {
                var res = api.ReadInt32(address);
                val = res.Content;
                return res.IsSuccess;
            }
            else if (data.Type == PLCTagType.FLOAT)
            {
                var res = api.ReadFloat(address);
                val = res.Content;
                return res.IsSuccess;
            }
            return false;
        }
 
        public static bool GetData(SiemensS7Net api, string name, out object val)
        {
            val = null;
            if (dataCache.ContainsKey(name))
            {
                var data = dataCache[name];
                if (data.ConnectionType == ConnectionType.PLC_S7)
                {
                    if (failedCount[api] >= 3)
                    {
                        return false;
                    }
                    var res = GetS7Data(api, data, name, out val);
                    if (!res) failedCount[api]++;
                    return res;
                }
                else if (data.ConnectionType == ConnectionType.MODBUS)
                {
                    return ModbusHelper.ReadData(data, out val, out _);
                }
                else if (data.ConnectionType == ConnectionType.NXI_5010)
                {
                    return NXI_5010Helper.ReadData(data, name, out val);
                }
                return false;
            }
            else
            {
                return false;
            }
        }
 
        public static bool GetData(string apiName, string name, out object val)
        {
            val = null;
            if (dataCache.ContainsKey(name))
            {
                var data = dataCache[name];
                if (data.ConnectionType == ConnectionType.PLC_S7)
                {
                    var api = Get(apiName);
                    if (failedCount[api] >= 3)
                    {
                        return false;
                    }
                    var res = GetS7Data(api, data, name, out val);
                    if (!res) failedCount[api]++;
                    return res;
                }
                else if (data.ConnectionType == ConnectionType.MODBUS)
                {
                    return ModbusHelper.ReadData(data, out val, out _);
                }
                else if (data.ConnectionType == ConnectionType.NXI_5010)
                {
                    return NXI_5010Helper.ReadData(data, name, out val);
                }
                return false;
            }
            else
            {
                return false;
            }
        }
 
        private static bool WriteS7Data(SiemensS7Net api, PLCTagData data, string name, object val)
        {
            string address = data.Address;
            OperateResult res;
            switch (data.Type)
            {
                case PLCTagType.BOOL:
                    res = api.Write(address, (bool)val);
                    break;
                case PLCTagType.SHORT:
                    res = api.Write(address, (short)val);
                    break;
                case PLCTagType.USHORT:
                    res = api.Write(address, (ushort)val);
                    break;
                case PLCTagType.INT:
                    res = api.Write(address, (int)val);
                    break;
                case PLCTagType.FLOAT:
                    res = api.Write(address, (float)val);
                    break;
                default:
                    return false;
            }
            return res.IsSuccess;
        }
 
        private static bool WriteS7Data2(SiemensS7Net api, PLCTagData data, string name, object val, out string errMsg)
        {
            errMsg = string.Empty;
            string address = data.Address;
            OperateResult res;
            switch (data.Type)
            {
                case PLCTagType.BOOL:
                    res = api.Write(address, (bool)val);
                    break;
                case PLCTagType.SHORT:
                    res = api.Write(address, (short)val);
                    break;
                case PLCTagType.USHORT:
                    res = api.Write(address, (ushort)val);
                    break;
                case PLCTagType.INT:
                    res = api.Write(address, (int)val);
                    break;
                case PLCTagType.FLOAT:
                    res = api.Write(address, (float)val);
                    break;
                default:
                    return false;
            }
            errMsg = res.Message;
            return res.IsSuccess;
        }
 
        public static bool WriteData(SiemensS7Net api, string name, object val)
        {
            if (dataCache.ContainsKey(name))
            {
                var data = dataCache[name];
 
                string address = data.Address;
                if (data.ConnectionType == ConnectionType.PLC_S7)
                {
                    if (failedCount[api] >= 3)
                    {
                        return false;
                    }
                    var res = WriteS7Data(api, data, name, val);
                    if (!res) failedCount[api]++;
                    return res;
                }
                else if (data.ConnectionType == ConnectionType.MODBUS)
                {
                    return ModbusHelper.WriteData(data, val, out _);
                }
                return false;
            }
            else
            {
                return false;
            }
        }
 
        public static bool WriteData(string apiName, string name, object val, out string errMsg)
        {
            errMsg = string.Empty;
            if (dataCache.ContainsKey(name))
            {
                var data = dataCache[name];
                string address = data.Address;
                if (data.ConnectionType == ConnectionType.PLC_S7)
                {
                    var api = Get(apiName);
                    if (failedCount[api] >= 3)
                    {
                        errMsg = "超过重试次数";
                        return false;
                    }
                    var res = WriteS7Data2(api, data, name, val, out errMsg);
                    if (!res) failedCount[api]++;
                    return res;
                }
                else if (data.ConnectionType == ConnectionType.MODBUS)
                {
                    return ModbusHelper.WriteData(data, val, out errMsg);
                }
                errMsg = "不支持的类型";
                return false;
            }
            else
            {
                errMsg = $"未找到标签[{name}]";
                return false;
            }
        }
 
        public static void Release()
        {
            foreach (var client in cache.Values)
            {
                client.Dispose();
            }
            cache.Clear();
 
            ModbusHelper.Release();
            NXI_5010Helper.Release();
        }
 
        public static void ResetFailCount()
        {
            foreach (var client in failedCount.Keys)
            {
                failedCount[client] = 0;
            }
        }
    }
}