wangxinhui
2024-11-06 8f392cc88b0768b74efca3b68785cf5aa1c38e70
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
using HslCommunication;
using HslCommunication.LogNet;
using HslCommunication.Profinet.Siemens;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
 
namespace WIDESEA_WCS.WCSClient.Siemens
{
    /// <summary>
    /// 西门子PLC
    /// </summary>
    public class SiemensPLCClient
    {
        ILogNet logNet;
        /// <summary>
        /// 西门子PLC连接
        /// </summary>
        public SiemensS7Net SiemensS7NetClient { get; set; }
        /// <summary>
        /// PLC连接对应的地址
        /// </summary>
        public List<DBItemGroup> PLCDBItems { get; set; }
        /// <summary>
        /// 连接名称
        /// </summary>
        public string PLCName { get; set; }
        /// <summary>
        /// IP地址
        /// </summary>
        public string PLCIPAddress { get; set; }
        /// <summary>
        /// 连接说明
        /// </summary>
        public string PLCDescroption { get; set; }
        /// <summary>
        /// 端口号(默认102)
        /// </summary>
        public int Port { get; set; } = 102;
        /// <summary>
        /// 
        /// </summary>
        public byte Rack { get; set; } = 0;
        /// <summary>
        /// 
        /// </summary>
        public byte Slot { get; set; } = 0;
 
        /// <summary>
        /// 是否已连接
        /// </summary>
        public bool IsConnected { get; set; } = false;
 
        /// <summary>
        /// 连接信息
        /// </summary>
        public string ConnectionMessage { get; set; }
 
        /// <summary>
        /// 构造方法,实例化连接对象及PLC连接对应的地址
        /// </summary>
        /// <param name="siemensPLCS"></param>
        public SiemensPLCClient(SiemensPLCS siemensPLCS = SiemensPLCS.S1200)
        {
            this.SiemensS7NetClient = new SiemensS7Net(siemensPLCS);
            PLCDBItems = new List<DBItemGroup>();
        }
 
        /// <summary>
        /// PLC连接内置线程
        /// </summary>
        Thread Thread { get; set; }
 
        /// <summary>
        /// 内置线程是否运行
        /// </summary>
        bool isRun { get; set; }
 
        #region 内置线程 PING IP
        /// <summary>
        /// 内置线程 PING IP
        /// </summary>
        public void Start(Action action)
        {
            Thread = new Thread(() =>
            {
                while (isRun)
                {
                    if (isRun)
                    {
                        //IPStatus status = this.SiemensS7NetClient.IpAddressPing();
                        IPStatus status = IPStatus.Success;
                        if (status == IPStatus.Success)
                        {
                            ConnectionMessage = status.ToString();
                            if (action != null)
                                action();
                        }
                        //if (false)
                        //{
 
                        //}
                        else
                        {
                            IsConnected = false;
                            //WIDESEA_Common.Tools.WriteLog.GetLog().Write($"Error:{PLCName}连接连接掉线,200ms后自动重连", PLCName);
                            OperateResult result = this.SiemensS7NetClient.ConnectServer();
                            if (result.IsSuccess)
                                IsConnected = true;
                        }
                        Thread.Sleep(100);
                    }
 
                }
            });
            Thread.Start();
        }
        #endregion
 
        #region 连接PLC,将短连接切换成长连接模式
        /// <summary>
        /// 连接PLC
        /// </summary>
        public string Connect()
        {
            this.SiemensS7NetClient.IpAddress = PLCIPAddress;
            this.SiemensS7NetClient.Port = Port;
            this.SiemensS7NetClient.Rack = Rack;
            this.SiemensS7NetClient.Slot = Slot;
            this.SiemensS7NetClient.ConnectTimeOut = 2000;
            this.SiemensS7NetClient.ReceiveTimeOut = 2000;
            this.SiemensS7NetClient?.ConnectClose();
 
            logNet = new LogNetFileSize(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log2"), 2 * 1024 * 1024);
 
            OperateResult result = this.SiemensS7NetClient.ConnectServer();
            isRun = true;
 
            if (!result.IsSuccess)
            {
                WIDESEA_Common.Tools.WriteLog.GetLog().Write($"Error:{PLCName}连接失败---{DateTime.Now}{Environment.NewLine}错误信息:{result.Message}", PLCName);
                IsConnected = false;
                logNet.WriteInfo($"{PLCIPAddress}连接失败,错误信息:{result.Message}");
                return $"连接失败,错误信息:{result.Message}";
            }
            else
            {
                IsConnected = true;
                WIDESEA_Common.Tools.WriteLog.GetLog().Write($"Info:{PLCName}连接成功!---{DateTime.Now}", PLCName);
                logNet.WriteInfo($"{PLCIPAddress}连接成功");
                return $"连接成功";
            }
        }
        #endregion
 
        #region 断开与PLC的连接,将长连接切换成短连接模式
        /// <summary>
        /// 断开与PLC的连接
        /// </summary>
        public void Disconnect()
        {
            this.SiemensS7NetClient?.ConnectClose();
            isRun = false;
            WIDESEA_Common.Tools.WriteLog.GetLog().Write($"Info:已断开与{PLCName}的连接!", PLCName);
        }
        #endregion
 
        #region 根据名称/描述读取PLC数据
        /// <summary>
        /// 根据名称/描述读取PLC数据
        /// </summary>
        /// <param name="itemName">名称/描述(数据库中plcdetail_name列)</param>
        /// <returns></returns>
        public virtual object ReadValue(string itemName)
        {
            lock (this.SiemensS7NetClient)
            {
                object result = null;
                DBItemGroup item = this.PLCDBItems.Where(x => x.ItemName == itemName).FirstOrDefault();
                if (item == null)
                    throw new Exception($"未定义读取指令{itemName}");
                string itemAddress = item.ItemAddress;
                string itemDatatype = item.ItemDataType;
                try
                {
                    result = Read(itemAddress, itemDatatype);
                }
                catch (Exception ex)
                {
                    WIDESEA_Common.Tools.WriteLog.GetLog().Write($"Error:{Environment.NewLine}{PLCName}读取数据失败{Environment.NewLine}数据类型:{item.ItemDataType},{item.Remark}({itemAddress}):{Environment.NewLine}错误信息:{ex.Message}", PLCName);
 
                    throw new Exception("读取数据失败");
                }
                //try
                //{
                //    WIDESEA_Common.Tools.WriteLog.GetLog().Write($"Info:{Environment.NewLine}{PLCName}读取数据{Environment.NewLine}数据类型:{item.ItemDataType}{Environment.NewLine}{item.Remark}({itemAddress}):" + result, PLCName);
                //}
                //catch
                //{
 
                //}
                return result;
            }
 
        }
 
        /// <summary>
        /// 根据名称/描述读取PLC数据
        /// </summary>
        /// <param name="itemName">名称/描述(数据库中plcdetail_name列)</param>
        /// <returns></returns>
        public virtual object ReadDBValue(string address, int len)
        {
            lock (this.SiemensS7NetClient)
            {
                object result = null;
 
                string itemAddress = address;
                string itemDatatype = "byte";
                try
                {
                    result = Read(itemAddress, itemDatatype, len);
                }
                catch (Exception ex)
                {
                    WIDESEA_Common.Tools.WriteLog.GetLog().Write($"Error:{Environment.NewLine}{PLCName}读取数据失败{Environment.NewLine}数据类型:{address},{address}({itemAddress}):{Environment.NewLine}错误信息:{ex.Message}", PLCName);
                    throw new Exception("读取数据失败");
                }
                return result;
            }
 
        }
        #endregion
 
        #region 根据PLCDBItem读取PLC数据
        /// <summary>
        /// 根据PLCDBItem读取PLC数据
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public virtual object ReadValue(DBItemGroup item)
        {
            lock (this.SiemensS7NetClient)
            {
                object result = null;
                if (item == null)
                    return -1;
                string itemAddress = item.ItemAddress;
                string itemDatatype = item.ItemDataType;
                try
                {
                    result = Read(itemAddress, itemDatatype);
                }
                catch (Exception ex)
                {
                    WIDESEA_Common.Tools.WriteLog.GetLog().Write($"Error:{Environment.NewLine}{PLCName}读取数据失败{Environment.NewLine}数据类型:{item.ItemDataType},{item.Remark}({itemAddress}):{Environment.NewLine}错误信息:{ex.Message}", PLCName);
                }
                try
                {
                    //WIDESEA_Common.Tools.WriteLog.GetLog().Write($"Info:{Environment.NewLine}{PLCName}读取数据{Environment.NewLine}数据类型:{item.ItemDataType}{Environment.NewLine}{item.Remark}({itemAddress}):" + result, PLCName);
                }
                catch
                {
 
                }
                return result;
            }
 
        }
        #endregion
 
        #region 根据设备编号读取PLC数据
        /// <summary>
        /// 根据设备编号读取PLC数据
        /// </summary>
        /// <param name="equipNum">设备编号</param>
        /// <returns>json字符串</returns>
        public object ReadValues(string equipNum)
        {
            lock (this.SiemensS7NetClient)
            {
                object result = null;
                Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
                List<DBItemGroup> items = this.PLCDBItems.Where(x => x.EquipNum == equipNum).ToList();
                for (int i = 0; i < items.Count(); i++)
                {
                    keyValuePairs.Add(items[i].ItemName, ReadValue(items[i].ItemName));
                }
                result = JsonConvert.SerializeObject(keyValuePairs);
                return result;
            }
 
        }
        #endregion
 
        #region 根据名称/描述读取PLC数据返回设备编号和值的JSON字符串
        /// <summary>
        /// 根据名称/描述读取PLC数据返回设备编号和值的JSON字符串
        /// </summary>
        /// <param name="itemName">名称/描述(数据库中plcdetail_name列)</param>
        /// <returns>json字符串</returns>
        public string ReadValuesByItemName(string itemName)
        {
            lock (this.SiemensS7NetClient)
            {
                string result = null;
                Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
                List<DBItemGroup> items = this.PLCDBItems.Where(x => x.ItemName == itemName).ToList();
                for (int i = 0; i < items.Count(); i++)
                {
                    keyValuePairs.Add(items[i].EquipNum, ReadValue(items[i]));
                }
                result = JsonConvert.SerializeObject(keyValuePairs);
                return result;
            }
 
        }
        #endregion
 
        #region 根据名称/描述和设备编号读取PLC数据
        /// <summary>
        /// 根据名称/描述和设备编号读取PLC数据
        /// </summary>
        /// <param name="itemName">名称/描述(数据库中plcdetail_name列)</param>
        /// <param name="equipNum">设备编号</param>
        /// <returns></returns>
        public virtual object ReadValue(string itemName, string equipNum)
        {
            lock (this.SiemensS7NetClient)
            {
                object result = null;
                DBItemGroup item = this.PLCDBItems.Where(x => x.ItemName == itemName && x.EquipNum == equipNum).FirstOrDefault();
                if (item == null)
                {
                    try
                    {
                        WIDESEA_Common.Tools.WriteLog.GetLog().Write($"Info:{Environment.NewLine}{PLCName}读取数据{Environment.NewLine}{Environment.NewLine}({equipNum}):未在协议找到编号{equipNum}的地址", PLCName);
                    }
                    catch
                    {
 
                    }
                    return -1;
                }
 
                string itemAddress = item.ItemAddress;
                string itemDatatype = item.ItemDataType;
                try
                {
                    result = Read(itemAddress, itemDatatype);
                }
                catch (Exception ex)
                {
                    WIDESEA_Common.Tools.WriteLog.GetLog().Write($"Error:{Environment.NewLine}{PLCName}读取数据失败{Environment.NewLine}数据类型:{item.ItemDataType},{item.Remark}({itemAddress}):{Environment.NewLine}错误信息:{ex.Message}", PLCName);
                }
                try
                {
                    //WIDESEA_Common.Tools.WriteLog.GetLog().Write($"Info:{Environment.NewLine}{PLCName}读取数据{Environment.NewLine}数据类型:{item.ItemDataType}{Environment.NewLine}{item.Remark}({itemAddress}):" + result, PLCName);
                }
                catch
                {
 
                }
                return result;
            }
 
        }
        #endregion
 
        #region 将数据写入到PLC中
        /// <summary>
        /// 将数据写入到PLC中
        /// </summary>
        /// <param name="itemName">名称/描述(数据库中plcdetail_name列)</param>
        /// <param name="value">写入的值</param>
        /// <returns></returns>
        public virtual bool WriteValue(string itemName, object value)
        {
            lock (this.SiemensS7NetClient)
            {
                OperateResult operateResult = new OperateResult(); ;
                bool result = false;
                string writeResult = "";
                DBItemGroup item = this.PLCDBItems.Where(x => x.ItemName == itemName).FirstOrDefault();
                if (item == null)
                    throw new Exception($"未定义写入指令{itemName}");
                string itemAddress = item.ItemAddress;
                string itemDatatype = item.ItemDataType;
                try
                {
                    operateResult = Write(itemAddress, itemDatatype, value);
                    result = operateResult.IsSuccess;
                    writeResult = result ? "成功" : $"失败:{operateResult.Message}";
                    if (!result)
                    {
                        throw new Exception(operateResult.Message);
                    }
                }
                catch (Exception ex)
                {
                    WIDESEA_Common.Tools.WriteLog.GetLog().Write($"Error:{PLCName}写入数据失败{Environment.NewLine}数据类型:{item.ItemDataType}{Environment.NewLine}{item.Remark}({itemAddress}):{Environment.NewLine}错误信息:{ex.Message}", PLCName);
                }
                return result;
            }
        }
 
        public virtual bool WriteDBValue(string address, object value)
        {
            lock (this.SiemensS7NetClient)
            {
                OperateResult operateResult = new OperateResult(); ;
                bool result = false;
                string writeResult = "";
                string itemAddress = address;
                string itemDatatype = "byte";
                try
                {
                    operateResult = Write(itemAddress, itemDatatype, value);
                    result = operateResult.IsSuccess;
                    writeResult = result ? "成功" : $"失败:{operateResult.Message}";
                    if (!result)
                    {
                        throw new Exception(operateResult.Message);
                    }
                }
                catch (Exception ex)
                {
                    WIDESEA_Common.Tools.WriteLog.GetLog().Write($"Error:{PLCName}写入数据失败{Environment.NewLine}{address}({itemAddress}):{Environment.NewLine}错误信息:{ex.Message}", PLCName);
                }
                return result;
            }
        }
        #endregion
 
        #region 将数据写入到PLC中
        /// <summary>
        /// 将数据写入到PLC中
        /// </summary>
        /// <param name="itemName">名称/描述(数据库中plcdetail_name列)</param>
        /// <param name="value">写入的值</param>
        /// <returns></returns>
        public virtual bool WriteValue(string itemName, string equipNum, object value)
        {
            lock (this.SiemensS7NetClient)
            {
                OperateResult operateResult = new OperateResult(); ;
                bool result = false;
                string writeResult = "";
                DBItemGroup item = this.PLCDBItems.Where(x => x.ItemName == itemName && x.EquipNum == equipNum).FirstOrDefault();
                if (item == null)
                    return false;
                string itemAddress = item.ItemAddress;
                string itemDatatype = item.ItemDataType;
                try
                {
                    operateResult = Write(itemAddress, itemDatatype, value);
                    result = operateResult.IsSuccess;
                    writeResult = result ? "成功" : $"失败:{operateResult.Message}";
                }
                catch (Exception ex)
                {
                    WIDESEA_Common.Tools.WriteLog.GetLog().Write($"Error:{PLCName}写入数据失败{Environment.NewLine}数据类型:{item.ItemDataType}{Environment.NewLine}{item.Remark}({itemAddress}):{Environment.NewLine}错误信息:{ex.Message}", PLCName);
                }
                try
                {
                    WIDESEA_Common.Tools.WriteLog.GetLog().Write($"Info:{PLCName}写入数据{writeResult}{Environment.NewLine}数据类型:{item.ItemDataType}{Environment.NewLine}{item.Remark}({itemAddress}):" + value, PLCName);
                }
                catch
                {
 
                }
                return result;
            }
        }
        #endregion
 
        #region 批量写入数据
        /// <summary>
        /// 批量写入数据(数据类型必须一致)
        /// </summary>
        /// <param name="adress">DB块起始地址</param>
        /// <param name="type">数据类型</param>
        /// <param name="value">写入的值</param>
        /// <returns></returns>
        public OperateResult WriteArray(string adress, string type, object value)
        {
            OperateResult result = new OperateResult();
            try
            {
                switch (type.ToLower())
                {
                    case "int":
                        result = this.SiemensS7NetClient.Write(adress, value as ushort[]);
                        break;
                    case "dint":
                        result = this.SiemensS7NetClient.Write(adress, value as int[]);
                        break;
                    case "char":
                        string temp = value.ToString();
                        byte[] writeData = Encoding.Default.GetBytes(temp);
                        result = this.SiemensS7NetClient.Write(adress, writeData);
                        break;
                    case "bool":
                        result = this.SiemensS7NetClient.Write(adress, value as bool[]);
                        break;
                }
            }
            catch (Exception ex)
            {
                WIDESEA_Common.Tools.WriteLog.GetLog().Write($"Error:{PLCName}写入数据失败{Environment.NewLine}数据类型:{type}{Environment.NewLine}{adress}:{Environment.NewLine}错误信息:{ex.Message}", PLCName);
            }
            return result;
        }
        #endregion
 
        #region 批量读取数据
        /// <summary>
        /// 批量读取数据(数据类型必须一致)
        /// </summary>
        /// <param name="adress">DB块起始地址</param>
        /// <param name="type">数据类型</param>
        /// <param name="length">读取长度</param>
        /// <returns></returns>
        public object ReadArray(string adress, string type, int length)
        {
            object result = -1; ;
            try
            {
                switch (type.ToLower())
                {
                    case "int":
                        result = this.SiemensS7NetClient.ReadUInt16(adress, Convert.ToUInt16(length)).Content;
                        break;
                    case "dint":
                        result = this.SiemensS7NetClient.ReadUInt32(adress, Convert.ToUInt16(length)).Content;
                        break;
                    case "char":
                        byte[] temps = this.SiemensS7NetClient.Read(adress, Convert.ToUInt16(length)).Content;
                        result = Encoding.Default.GetString(temps).ToCharArray();
                        break;
                    case "bool":
                        result = this.SiemensS7NetClient.ReadBool(adress, Convert.ToUInt16(length)).Content;
                        break;
                    case "byte":
                        break;
                }
            }
            catch (Exception ex)
            {
 
                WIDESEA_Common.Tools.WriteLog.GetLog().Write($"Error:{Environment.NewLine}{PLCName}读取数据失败{Environment.NewLine}数据类型:{type},{adress}:{Environment.NewLine}错误信息:{ex.Message}", PLCName);
            }
            return result;
        }
        #endregion
 
 
        #region 根据DB地址及类型读取数据,最根本读取方法
 
 
        public T Read<T>(string address, int len = 1)
        {
            var Type = typeof(T);
            if (Type == typeof(bool))
            {
                return (T)GetContent(SiemensS7NetClient.ReadBool(address));
            }
            else if (Type == typeof(ushort))
            {
                return (T)GetContent(SiemensS7NetClient.ReadUInt16(address));
            }
            else if (Type == typeof(byte[]))
            {
                return (T)GetContent(SiemensS7NetClient.Read(address, (ushort)len));
            }
            else if (Type == typeof(float))
            {
                return (T)GetContent(SiemensS7NetClient.ReadFloat(address));
            }
            else if (Type == typeof(string))
            {
                return (T)GetContent(SiemensS7NetClient.ReadString(address, (ushort)len));
            }
            else
            {
                throw new Exception($"类型{typeof(T)},未定义!");
            }
        }
 
        /// <summary>
        /// 根据DB地址及类型读取数据
        /// </summary>
        /// <param name="adress">DB地址(DB块+.+偏移量)</param>
        /// <param name="type">数据类型(int/dint/string/bool)</param>
        /// <returns></returns>
        public object Read(string adress, string type, int len = 1)
        {
            object result = -1;
            try
            {
                switch (type.ToLower())
                {
                    case "int":
                        result = GetContent(this.SiemensS7NetClient.ReadUInt16(adress));
                        break;
                    case "dint":
                        result = GetContent(this.SiemensS7NetClient.ReadUInt32(adress));
                        break;
                    case "string":
                        OperateResult<string> operateResult = this.SiemensS7NetClient.ReadString(adress);
                        operateResult.Content = operateResult.Content?.Replace("\\", "")?.Replace("\0", "")?.Replace("\u0004", "")?.Replace("\u0003", "")?.Replace("\u0014", "")?.Replace("\u001e", "")?.Replace("\u0006", "")?.Replace("emptyID", "")?.Replace("mptyID", "")?.Replace("ptyID", "")?.Replace("tyID", "")?.Replace("yID", "")?.Replace("ID", "")?.Replace("\a", "");
                        result = operateResult.Content == null ? "" : operateResult.Content.Trim();
                        break;
                    case "bool":
                        result = GetContent(this.SiemensS7NetClient.ReadBool(adress));
                        break;
                    case "byte":
                        result = GetContent(this.SiemensS7NetClient.Read(adress, (ushort)len));
                        break;
                    default:
                        throw new Exception($"未找到该数据类型:{type}");
                }
            }
            catch (Exception ex)
            {
                logNet.WriteInfo($"{PLCIPAddress}读取失败,错误信息:{ex.Message}");
                WIDESEA_Common.Tools.WriteLog.GetLog().Write($"Error:{Environment.NewLine}{PLCName}读取数据失败{Environment.NewLine}数据类型:{type},{adress}:{Environment.NewLine}错误信息:{ex.Message}", PLCName);
            }
            return result;
        }
 
        /// <summary>
        /// 获取读取的内容
        /// </summary>
        /// <param name="operateResult"></param>
        /// <returns></returns>
        private object GetContent<T>(OperateResult<T> operateResult)
        {
            if (!operateResult.IsSuccess)
            {
                logNet.WriteInfo($"{PLCIPAddress}读取失败,错误信息:{operateResult.Message}");
                throw new Exception(operateResult.Message);
            }
            return operateResult.Content;
        }
        #endregion
 
        #region 根据DB地址及类型写入数据,最根本写入方法
        /// <summary>
        /// 根据DB地址及类型写入数据
        /// </summary>
        /// <param name="adress">DB地址(DB块+.+偏移量)</param>
        /// <param name="type">数据类型(int/dint/string/bool)</param>
        /// <param name="value">写入的值</param>
        /// <returns>返回成功或失败</returns>
        public OperateResult Write(string adress, string type, object value)
        {
            OperateResult result = new OperateResult();
            switch (type.ToLower())
            {
                case "int":
                    result = this.SiemensS7NetClient.Write(adress, Convert.ToUInt16(value));
                    break;
                case "dint":
                    result = this.SiemensS7NetClient.Write(adress, Convert.ToUInt32(value));
                    break;
                case "string":
                    string[] adressTmp = adress.Split('.');
                    result = this.SiemensS7NetClient.Write(adressTmp[0] + "." + (Convert.ToInt32(adressTmp[1]) - 2), value.ToString());
                    break;
                case "bool":
                    result = this.SiemensS7NetClient.Write(adress, Convert.ToBoolean(value));
                    break;
                case "byte":
                    result = this.SiemensS7NetClient.Write(adress, (byte[])value);
                    break;
                default:
                    throw new Exception($"未找到该数据类型:{type}");
            }
            return result;
        }
        #endregion 
    }
}