123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- 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<string, AlarmEventItem> 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<IoBlockItem> _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<bool[]> 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<short[]> 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<float[]> 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;
- }
- }
- }
|