From 807dfe60e65b9790014c0029dbe46381f959b145 Mon Sep 17 00:00:00 2001
From: hutongqing <hutongqing@hnkhzn.com>
Date: 星期四, 31 十月 2024 17:29:35 +0800
Subject: [PATCH] Create OmronEtherNetCommunicator.cs
---
WIDESEAWCS_Server/WIDESEAWCS_Communicator/Omron/OmronEtherNetCommunicator.cs | 198 +++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 198 insertions(+), 0 deletions(-)
diff --git a/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Omron/OmronEtherNetCommunicator.cs b/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Omron/OmronEtherNetCommunicator.cs
new file mode 100644
index 0000000..324bd0a
--- /dev/null
+++ b/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Omron/OmronEtherNetCommunicator.cs
@@ -0,0 +1,198 @@
+锘縰sing 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>
+ /// 瑗块棬瀛怱7閫氳绫�
+ /// </summary>
+ [Description("娆у榫橢therNet/IP(CIP)")]
+ public class OmronEtherNetCommunicator : BaseCommunicator
+ {
+ #region Private Member
+ /// <summary>
+ /// HSLCommunication鐨勮タ闂ㄥ瓙鐨凷7鍗忚鐨勯�氳绫�
+ /// </summary>
+ private OmronCipNet plc;
+
+ /// <summary>
+ /// 璁惧鐨処P鍦板潃銆�
+ /// </summary>
+ private string _ipAddress;
+
+ /// <summary>
+ /// 杩炴帴浣跨敤鐨勭鍙e彿銆�
+ /// </summary>
+ private int _port;
+
+ /// <summary>
+ /// 褰撳墠閫氳鍣ㄦ槸鍚﹀凡杩炴帴鍒癙LC銆�
+ /// </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">璁惧鐨処P鍦板潃</param>
+ /// <param name="port">杩炴帴浣跨敤鐨勭鍙e彿</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
+ {
+ //瀹炰緥鍖栦竴涓タ闂ㄥ瓙鐨凷7鍗忚鐨勯�氳瀵硅薄
+ plc = new OmronCipNet()
+ {
+ IpAddress = _ipAddress,
+ Port = _port
+ };
+ OperateResult operateResult = plc.ConnectServer();//杩炴帴PLC
+ _connected = operateResult.IsSuccess;//灏嗚繛鎺ユ槸鍚︽垚鍔熻祴鍊肩粰褰撳墠閫氳鍣ㄦ槸鍚﹀凡杩炴帴鍒癙LC
+
+ 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
+ }
+}
--
Gitblit v1.9.3