using Aitex.Core.RT.DataCenter; using Aitex.Core.RT.Event; using Aitex.Core.RT.IOCore; using Aitex.Core.RT.Log; using Aitex.Core.RT.OperationCenter; using Aitex.Core.RT.SCCore; using Aitex.Core.Util; using HslCommunication; using HslCommunication.Profinet.Omron; using MECF.Framework.Common.Communications; using MECF.Framework.Common.Event; using MECF.Framework.Common.PLC; using MECF.Framework.RT.Core.IoProviders; using MECF.Framework.RT.Core.IoProviders.Mitsubishis; using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Xml; namespace FurnaceRT.Equipments.Systems { public class FinsPlc : IPlc { public AlarmEventItem AlarmConnectFailed { get; set; } public AlarmEventItem AlarmCommunicationError { get; set; } public string Module { get; set; } public string Name { get; set; } public bool HasAlarm { get; set; } public string Address { get; set; } public bool IsConnected { get { if (_omronFins == null) return false; return _isOpened; } } public event Action OnConnected = null; public event Action OnDisconnected = null; public event Action OnDeviceAlarmStateChanged = null; private OmronFinsNet _omronFins = null; private bool _isOpened = false; private string _ip; private int _port; private R_TRIG _failedTrigger = new R_TRIG(); //private List _blockSectionsDemand; public FinsPlc(string ip, int port) { _port = port; _ip = ip; } #region IDevice interface public bool Initialize() { return true; } public void Monitor() { } public void Reset() { } public void Terminate() { } #endregion public bool CheckIsConnected() { if (_omronFins == null) return false; return _isOpened; } public bool Read(string variable, out object data, string type, int length, out string reason) { throw new NotImplementedException(); } public bool WriteArrayElement(string variable, int index, object value, out string reason) { throw new NotImplementedException(); } public bool ReadBool(string address, out bool[] data, int length, out string reason) { OperateResult result = _omronFins.ReadBool(address, (ushort)length); if (!result.IsSuccess) { reason = result.Message; data = null; return false; } data = result.Content; reason = ""; return true; } public bool ReadInt16(string address, out short[] data, int length, out string reason) { OperateResult result = _omronFins.ReadInt16(address, (ushort)length); if (!result.IsSuccess) { reason = result.Message; data = null; return false; } data = result.Content; reason = ""; return true; } public bool ReadFloat(string address, out float[] data, int length, out string reason) { OperateResult result = _omronFins.ReadFloat(address, (ushort)length); if (!result.IsSuccess) { reason = result.Message; data = null; return false; } data = result.Content; reason = ""; return true; } public bool WriteBool(string address, bool[] data, out string reason) { OperateResult result = _omronFins.Write(address, data); if (!result.IsSuccess) { reason = result.Message; return false; } reason = ""; return true; } public bool WriteInt16(string address, short[] data, out string reason) { OperateResult result = _omronFins.Write(address, data); if (!result.IsSuccess) { reason = result.Message; return false; } reason = ""; return true; } public bool WriteFloat(string address, float[] data, out string reason) { OperateResult result = _omronFins.Write(address, data); if (!result.IsSuccess) { reason = result.Message; return false; } reason = ""; return true; } public bool Write(string variable, object value, out string reason) { throw new NotImplementedException(); } public bool ReadInt32(string address, out int[] data, int length, out string reason) { throw new NotImplementedException(); } public bool Connect() { try { LOG.Write(String.Format("试图连接PLC {0}:{1}", _ip, _port)); _isOpened = false; _omronFins = new OmronFinsNet(_ip, _port); OperateResult connect = _omronFins.ConnectServer(); if (connect.IsSuccess) { _isOpened = true; } } catch (Exception ex) { _failedTrigger.CLK = true; if (_failedTrigger.Q) { LOG.Write(ex, String.Format("Communication failed with PLC {0}:{1}", _ip, _port)); EV.PostMessage("System", EventEnum.DefaultWarning, String.Format("Communication failed with PLC {0}:{1}", _ip, _port)); } return false; } _failedTrigger.RST = true; return _isOpened; } public bool Disconnect() { try { if (_isOpened) { _isOpened = false; _omronFins.ConnectClose(); } } catch (Exception ex) { LOG.Write(ex); } return true; } } }