123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading.Tasks;
- using Aitex.Core.RT.Event;
- using Aitex.Core.RT.Log;
- using Aitex.Core.Util;
- using MECF.Framework.Common.Event;
- using MECF.Framework.Common.PLC;
- using MECF.Framework.RT.Core.IoProviders.Common;
- using MECF.Framework.RT.Core.IoProviders.Common.Transfer;
- using MECF.Framework.RT.Core.IoProviders.Omron;
- namespace JetMainframe.Devices
- {
- public class FinsTcpPlc : IPlc
- {
- private OmronFinsNet omronFinsNet = null;
- private bool _isOpened = false;
- private string _ip = "192.168.10.10";
- private string _localIp = "192.168.10.199";
- private int _port = 9600;
- private int _aoBlockStartPosition = 1000;
- private int _aiBlockStartPosition = 2000;
- private int _doBlockStartPosition = 0;
- private int _diBlockStartPosition = 20;
-
- R_TRIG _failedTrigger = new R_TRIG();
- DeviceTimer _timerWrite = new DeviceTimer();
- DeviceTimer _timerRead = new DeviceTimer();
- private double _averageWriteTime = 0;
- private double _averageReadTime = 0;
- public FinsTcpPlc(string ip, int port,string localIp)
- {
- _ip = ip;
- _port = port;
- _localIp = localIp;
- }
-
- private void RaiseException(string p)
- {
- _isOpened = false;
- }
-
- public event Action<string, AlarmEventItem> OnDeviceAlarmStateChanged;
- public string Module { get; set; }
- public string Name { get; set; }
- public bool HasAlarm { get; }
- public bool Initialize()
- {
- throw new NotImplementedException();
- }
- public void Monitor()
- {
- throw new NotImplementedException();
- }
- public void Terminate()
- {
- throw new NotImplementedException();
- }
- public void Reset()
- {
- throw new NotImplementedException();
- }
- public string Address { get; }
- public bool IsConnected
- {
- get
- { if (omronFinsNet == null)
- return false;
- return omronFinsNet.IsConnected; ;
- }
- }
- public bool Connect()
- {
- try
- {
- LOG.Write(String.Format("试图连接PLC {0}:{1}", _ip, _port));
- _isOpened = false;
- omronFinsNet = new OmronFinsNet(_ip, _port, _localIp){ConnectTimeOut = 1000,ReceiveTimeOut = 1000};
- OperateResult connect = omronFinsNet.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 true;
- }
- public bool Disconnect()
- {
- try
- {
- if (_isOpened)
- {
- _isOpened = false;
- omronFinsNet.ConnectClose();
- }
- }
- catch (Exception ex)
- {
- LOG.Write(ex);
- }
- return true;
- }
- public AlarmEventItem AlarmConnectFailed { get; set; }
- public AlarmEventItem AlarmCommunicationError { get; set; }
- public event Action OnConnected;
- public event Action OnDisconnected;
- public bool CheckIsConnected()
- {
- if (omronFinsNet == null)
- return false;
- return omronFinsNet.IsConnected;
- }
- 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)
- {
- try
- {
- double interval = _timerRead.GetElapseTime();
- if (interval > _averageWriteTime)
- {
- //LOG.Write(_ip + ":Max read PLC interval : " + interval);
- if (_averageReadTime < 0.1)
- _averageReadTime = interval;
- _averageReadTime = (interval + _averageReadTime) / 2;
- }
-
- _timerRead.Start(0);
- var read = omronFinsNet.ReadBool(address, (ushort)length);
- if (read.IsSuccess)
- {
- data = (bool[])read.Content.Clone();
- }
- else
- {
- data = null;
- reason = read.Message;
- return false;
- }
- }
- catch (Exception ex)
- {
- LOG.Error($"PLC ({_ip}) Read exception.", ex);
- reason = $"PLC ReadBool exception, {ex.Message}";
- data = null;
- return false;
- }
- reason = string.Empty;
- return true;
- }
- public bool ReadInt16(string address, out short[] data, int length, out string reason)
- {
- try
- {
- double interval = _timerRead.GetElapseTime();
- if (interval > _averageWriteTime)
- {
- //LOG.Write(_ip + ":Max read PLC interval : " + interval);
- if (_averageReadTime < 0.1)
- _averageReadTime = interval;
- _averageReadTime = (interval + _averageReadTime) / 2;
- }
- _timerRead.Start(0);
- var read = omronFinsNet.ReadInt16(address, (ushort)length);
- if (read.IsSuccess)
- {
- data = (short[])read.Content.Clone();
- }
- else
- {
- data = null;
- reason = read.Message;
- return false;
- }
- }
- catch (Exception ex)
- {
- LOG.Error($"PLC ({_ip}) ReadInt16 exception.", ex);
- reason = $"PLC ReadInt16 exception, {ex.Message}";
- data = null;
- return false;
- }
- reason = string.Empty;
- return true;
- }
- public bool ReadFloat(string address, out float[] data, int length, out string reason)
- {
- try
- {
- double interval = _timerRead.GetElapseTime();
- if (interval > _averageWriteTime)
- {
- //LOG.Write(_ip + ":Max read PLC interval : " + interval);
- if (_averageReadTime < 0.1)
- _averageReadTime = interval;
- _averageReadTime = (interval + _averageReadTime) / 2;
- }
- _timerRead.Start(0);
- var read = omronFinsNet.ReadFloat(address, (ushort)length);
- if (read.IsSuccess)
- {
- data = (float[])read.Content.Clone();
- }
- else
- {
- data = null;
- reason = read.Message;
- return false;
- }
- }
- catch (Exception ex)
- {
- LOG.Error($"PLC ({_ip}) ReadInt16 exception.", ex);
- reason = $"PLC ReadInt16 exception, {ex.Message}";
- data = null;
- return false;
- }
- reason = string.Empty;
- return true;
- }
- public bool WriteBool(string address, bool[] data, out string reason)
- {
- try
- {
- _timerWrite.Start(0);
-
- var write = omronFinsNet.Write(address, data);
- if (!write.IsSuccess)
- {
- reason = write.Message;
- return false;
- }
- double interval = _timerWrite.GetElapseTime();
- if (interval > _averageWriteTime)
- {
- LOG.Write(_ip + ":Max write PLC interval : " + interval);
- if (_averageWriteTime < 0.1)
- _averageWriteTime = interval;
- _averageWriteTime = (_averageWriteTime + interval) / 2;
- }
- }
- catch (Exception ex)
- {
- LOG.Error($"PLC ({_ip}) WriteBool exception.", ex);
- reason = $"PLC WriteBool exception, {ex.Message}";
-
- return false;
- }
- reason = string.Empty;
- return true;
- }
- public bool WriteInt16(string address, short[] data, out string reason)
- {
- try
- {
- _timerWrite.Start(0);
- var write = omronFinsNet.Write(address, data);
- if (!write.IsSuccess)
- {
- reason = write.Message;
- return false;
- }
- double interval = _timerWrite.GetElapseTime();
- if (interval > _averageWriteTime)
- {
- LOG.Write(_ip + ":Max write PLC interval : " + interval);
- if (_averageWriteTime < 0.1)
- _averageWriteTime = interval;
- _averageWriteTime = (_averageWriteTime + interval) / 2;
- }
- }
- catch (Exception ex)
- {
- LOG.Error($"PLC ({_ip}) WriteBool exception.", ex);
- reason = $"PLC WriteBool exception, {ex.Message}";
- return false;
- }
- reason = string.Empty;
- return true;
- }
- public bool WriteFloat(string address, float[] data, out string reason)
- {
- try
- {
- _timerWrite.Start(0);
- var write = omronFinsNet.Write(address, data);
- if (!write.IsSuccess)
- {
- reason = write.Message;
- return false;
- }
- double interval = _timerWrite.GetElapseTime();
- if (interval > _averageWriteTime)
- {
- LOG.Write(_ip + ":Max write PLC interval : " + interval);
- if (_averageWriteTime < 0.1)
- _averageWriteTime = interval;
- _averageWriteTime = (_averageWriteTime + interval) / 2;
- }
- }
- catch (Exception ex)
- {
- LOG.Error($"PLC ({_ip}) WriteBool exception.", ex);
- reason = $"PLC WriteBool exception, {ex.Message}";
- return false;
- }
- reason = string.Empty;
- return true;
- }
- }
- }
|