hutongqing
2024-10-31 807dfe60e65b9790014c0029dbe46381f959b145
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
using HslCommunication;
using HslCommunication.LogNet;
using HslCommunication.Profinet.Omron;
using HslCommunication.Profinet.Siemens;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
 
namespace WIDESEAWCS_Communicator
{
    /// <summary>
    /// 西门子S7通讯类
    /// </summary>
    [Description("欧姆龙EtherNet/IP(CIP)")]
    public class OmronEtherNetCommunicator : BaseCommunicator
    {
        #region Private Member
        /// <summary>
        /// HSLCommunication的西门子的S7协议的通讯类
        /// </summary>
        private OmronCipNet plc;
 
        /// <summary>
        /// 设备的IP地址。
        /// </summary>
        private string _ipAddress;
 
        /// <summary>
        /// 连接使用的端口号。
        /// </summary>
        private int _port;
 
        /// <summary>
        /// 当前通讯器是否已连接到PLC。  
        /// </summary>
        private bool _connected;
 
        /// <summary>
        /// PLC名称
        /// </summary>
        private string _name;
 
        private ILogNet _logNet;
 
        private bool _isPing = true;
        #endregion Private Member
 
        #region Public Member
        public override ILogNet LogNet => _logNet;
 
        public override string Name => _name;
 
        public override bool IsConnected => _connected;
 
        #endregion Public Member
 
        #region Constructor Function
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="ipAddress">设备的IP地址</param>
        /// <param name="port">连接使用的端口号</param>
        /// <param name="name">设备名称</param>
        public OmronEtherNetCommunicator(string ipAddress, int port, string name)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + $"Log_PLCReadWrite\\{name}";
            _logNet = new LogNetFileSize(path, 10 * 1024 * 1024, 100);
 
            bool ipCheck = IPAddress.TryParse(ipAddress, out IPAddress? address);
            if (!ipCheck)
            {
                _logNet.WriteError(name, string.Format(CommunicationExceptionMessage.IpAddressErrorException, ipAddress));
                throw new CommunicationException(string.Format(CommunicationExceptionMessage.IpAddressErrorException, ipAddress), CommunicationErrorType.IpAddressError);
            }
 
            _ipAddress = ipAddress;//通过构造函数赋值设备的IP地址
            _port = port;//通过构造函数赋值连接使用的端口号
            _name = name;
        }
        #endregion
 
        #region Private Method
        private void Ping()
        {
            Task.Run(() =>
            {
                while (_isPing)
                {
                    try
                    {
                        IPStatus status = plc.IpAddressPing();
                        if (status == IPStatus.Success)
                            _connected = true;
                        else
                            _connected = false;
                    }
                    finally
                    {
                        Thread.Sleep(100);
                    }
                }
            });
 
        }
        #endregion
 
        #region Public Method
        public override bool Connect()
        {
            try
            {
                //实例化一个西门子的S7协议的通讯对象
                plc = new OmronCipNet()
                {
                    IpAddress = _ipAddress,
                    Port = _port
                };
                OperateResult operateResult = plc.ConnectServer();//连接PLC
                _connected = operateResult.IsSuccess;//将连接是否成功赋值给当前通讯器是否已连接到PLC
 
                if (_connected)
                    LogNet.WriteInfo(Name, string.Format(CommunicationInfoMessage.ConnectSuccess, _ipAddress, _port));
                else
                    LogNet.WriteError(Name, string.Format(CommunicationExceptionMessage.ConnectFaild, _ipAddress, _port, operateResult.Message));
                Ping();
                return operateResult.IsSuccess;
            }
            catch (Exception ex)
            {
                LogNet.WriteException(Name, string.Format(CommunicationExceptionMessage.ConnectFaild, _ipAddress, _port, ex.Message), ex);
                //连接异常时抛出自定义异常类
                throw new CommunicationException(ex.Message, CommunicationErrorType.ConnectionFailed, innerException: ex);
            }
        }
 
        public override bool Disconnect()
        {
            throw new NotImplementedException();
        }
 
        public override void Dispose()
        {
            throw new NotImplementedException();
        }
 
        public override byte[] Read(string address, int length)
        {
            throw new NotImplementedException();
        }
 
        public override T Read<T>(string address)
        {
            throw new NotImplementedException();
        }
 
        public override object ReadAsObj(string address, string dataType)
        {
            throw new NotImplementedException();
        }
 
        public override T ReadCustomer<T>(string address)
        {
            throw new NotImplementedException();
        }
 
        public override OperateResult<TimeSpan> Wait<T>(string address, int readInterval, int waitTimeout, T value)
        {
            throw new NotImplementedException();
        }
 
        public override bool Write(string address, byte[] data)
        {
            throw new NotImplementedException();
        }
 
        public override bool Write<T>(string address, T value)
        {
            throw new NotImplementedException();
        }
 
        public override bool WriteCustomer<T>(string address, [NotNull] T value)
        {
            throw new NotImplementedException();
        }
 
        public override bool WriteObj(string address, string dataType, object value)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}