123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711 |
- 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 MECF.Framework.Common.Communications;
- 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;
- using MECF.Framework.RT.Core.IoProviders.Melsec;
- using MECF.Framework.RT.Core.IoProviders.Common;
- namespace MECF.Framework.RT.Core.IoProviders.Mitsubishis
- {
- public class HSLMCProtocolPlc : IoProvider, IConnection
- {
- public string Address
- {
- get { return $"{_ip}:{_port}"; }
- }
- public bool IsConnected
- {
- get { return IsOpened; }
- }
- public bool Connect()
- {
- return true;
- }
- public bool Disconnect()
- {
- return true;
- }
- private string _ip = "127.0.0.1";
- //private string _localIp = "127.0.0.1";
- private int _port = 6731;
- private int _socketId = 101;
- private int _stationId = 102;
- private byte[] _bufferIn;
- private byte[] _bufferOut;
- private MelsecMcNet melsec_net = null;
- //private int demandAiFrom; //should be the same the offset in lstBuffers
- //private int demandAiSize;
- //private int demandAoFrom; //should be the same the offset in lstBuffers
- //private int demandAoSize;
- //private bool inDemanding = false;
- private string aiStoragename = "D";
- private string aoStoragename = "D";
- private string diStoragename = "B";
- private string doStoragename = "B";
- private ushort perIoLength = 960;
- private Stopwatch stopwatchDi = new Stopwatch();
- private Stopwatch stopwatchDo = new Stopwatch();
- private Stopwatch stopwatchAi = new Stopwatch();
- private Stopwatch stopwatchAo = new Stopwatch();
- private int comunicationSpanDi;
- private int comunicationSpanDo;
- private int comunicationSpanAi;
- private int comunicationSpanAo;
- protected int comunicationSpanTotal;
- private Stopwatch stopwatchTotal = new Stopwatch();
- private int diStartAddress;
- private int doStartAddress;
- private int aiStartAddress;
- private int aoStartAddress;
- //private List<IoBlockItem> _blockSectionsDemand;
- private int plcCollectionInterval;
- private int connectionNumber;
- private bool isWriteRecipe;
- private int recipeOffset;
- private short[] recipeData = new short[50 * 15];
- public override void Initialize(string module, string name, List<IoBlockItem> lstBuffers, IIoBuffer buffer, XmlElement nodeParameter, string ioMappingPathFile, string ioModule)
- {
- Module = module;
- Name = name;
- _source = module + "." + name;
- _buffer = buffer;
- _nodeParameter = nodeParameter;
- _blockSections = lstBuffers;
- buffer.SetBufferBlock(_source, lstBuffers);
- buffer.SetIoMapByModule(_source, 0, ioMappingPathFile, ioModule);
- SetParameter(nodeParameter);
- State = IoProviderStateEnum.Uninitialized;
- plcCollectionInterval = SC.ContainsItem("System.PlcCollectionInterval") ? SC.GetValue<int>("System.PlcCollectionInterval") : 50;
- _thread = new PeriodicJob(plcCollectionInterval, OnTimer, name);
- ConnectionManager.Instance.Subscribe(Name, this);
- DATA.Subscribe($"{Module}.{Name}.IsConnected", () => melsec_net == null ? false : melsec_net.IsConnected);
- OP.Subscribe($"{Name}.Reconnect", (string cmd, object[] args) =>
- {
- Close();
- Open();
- return true;
- });
- }
-
- protected override bool OnTimer()
- {
- if (State == IoProviderStateEnum.Uninitialized)
- {
- SetState(IoProviderStateEnum.Opening);
- Open();
- }
- if (State == IoProviderStateEnum.Opened)
- {
- try
- {
- stopwatchTotal.Start();
- foreach (var bufferSection in _blockSections)
- {
- if (bufferSection.Type == IoType.DI)
- {
- stopwatchDi.Start();
- bool[] diBuffer = ReadDi(bufferSection.Offset, bufferSection.Size);
- if (diBuffer != null)
- {
- _buffer.SetDiBuffer(_source, bufferSection.Offset, diBuffer);
- //TraceArray(diBuffer);
- }
- stopwatchDi.Stop();
- }
- else if (bufferSection.Type == IoType.AI)
- {
- stopwatchAi.Start();
- short[] aiBuffer = ReadAi(bufferSection.Offset, bufferSection.Size);
- if (aiBuffer != null)
- {
- _buffer.SetAiBuffer(_source, bufferSection.Offset, aiBuffer);
- }
- stopwatchAi.Stop();
- }
- //else if (bufferSection.Type == IoType.DO)
- //{
- // bool[] doBuffer = ReadDo(bufferSection.Offset, bufferSection.Size);
- // if (doBuffer != null)
- // {
- // _buffer.SetDoBuffer(_source, bufferSection.Offset, doBuffer);
- // }
- //}
- //else if (bufferSection.Type == IoType.AO)
- //{
- // short[] aoBuffer = ReadAo(bufferSection.Offset, bufferSection.Size);
- // if (aoBuffer != null)
- // {
- // _buffer.SetAoBuffer(_source, bufferSection.Offset, aoBuffer);
- // if (bufferSection.AIOType == typeof(float))
- // {
- // _isAIOFloatType = true;
- // _buffer.SetAoBufferFloat(_source, bufferSection.Offset, Array.ConvertAll(aoBuffer, x => (float)x).ToArray());
- // }
- // }
- //}
- }
- comunicationSpanDi = (int)stopwatchDi.ElapsedMilliseconds;
- stopwatchDi.Reset();
- comunicationSpanAi = (int)stopwatchAi.ElapsedMilliseconds;
- stopwatchAi.Reset();
- stopwatchAo.Start();
- Dictionary<int, short[]> aos = _buffer.GetAoBuffer(_source);
- if (aos != null)
- {
- foreach (var ao in aos)
- {
- WriteAo(aoStartAddress, ao.Value);
- }
- }
- stopwatchAo.Stop();
- comunicationSpanAo = (int)stopwatchAo.ElapsedMilliseconds;
- stopwatchAo.Reset();
- stopwatchDo.Start();
- Dictionary<int, bool[]> dos = _buffer.GetDoBuffer(_source);
- if (dos != null)
- {
- foreach (var doo in dos)
- {
- WriteDo(doStartAddress, doo.Value);
- }
- }
- stopwatchDo.Stop();
- comunicationSpanDo = (int)stopwatchDo.ElapsedMilliseconds;
- stopwatchDo.Reset();
- if (isWriteRecipe)
- {
- isWriteRecipe = false;
- WriteRecipe(recipeOffset, recipeData);
- }
- stopwatchTotal.Stop();
- comunicationSpanTotal = (int)stopwatchTotal.ElapsedMilliseconds;
- stopwatchTotal.Reset();
- }
- catch (Exception ex)
- {
- LOG.Error($"{Name} {ex}");
- //SetState(IoProviderStateEnum.Error);
- Thread.Sleep(1000);
- Close();
- Open();
- }
- }
-
- _trigError.CLK = State == IoProviderStateEnum.Error;
- if (_trigError.Q)
- {
- EV.PostAlarmLog(Module, $"{_source} PLC {_ip}:{_port} error");
- }
- _trigNotConnected.CLK = State != IoProviderStateEnum.Opened;
- if (_trigNotConnected.T)
- {
- EV.PostInfoLog(Module, $"{_source} connected");
- }
- if (_trigNotConnected.R)
- {
- EV.PostAlarmLog(Module, $"{_source} PLC {_ip}:{_port} not connected");
- }
- if (State != IoProviderStateEnum.Opened && connectionNumber < 3)
- {
- Thread.Sleep(1000);
- Close();
- Open();
- connectionNumber++;
- EV.PostInfoLog(Module, $"{_source} PLC {_ip}:{_port} reconnection {connectionNumber}");
- if (connectionNumber >=3)
- {
- EV.PostAlarmLog(Module, $"{_source} PLC {_ip}:{_port} not connected");
- }
- }
- return true;
- }
- public override void Reset()
- {
- if (State != IoProviderStateEnum.Opened)
- {
- Close();
- Open();
- connectionNumber = 0;
- }
- }
- protected override void SetParameter(XmlElement nodeParameter)
- {
- string strIp = nodeParameter.GetAttribute("ip");
- //_localIp = nodeParameter.GetAttribute("localIp");
- string strPort = nodeParameter.GetAttribute("port");
- string networkId = nodeParameter.GetAttribute("network_id");
- string stationId = nodeParameter.GetAttribute("station_id");
- aiStoragename = nodeParameter.GetAttribute("aiStoragename");
- aoStoragename = nodeParameter.GetAttribute("aoStoragename");
- diStoragename = nodeParameter.GetAttribute("diStoragename");
- doStoragename = nodeParameter.GetAttribute("doStoragename");
- ushort.TryParse(nodeParameter.GetAttribute("perIoLength"), out perIoLength);
- perIoLength = perIoLength == 0 ? (ushort)960 : perIoLength;
- int.TryParse(nodeParameter.GetAttribute("doStartAddress"), out doStartAddress);
- int.TryParse(nodeParameter.GetAttribute("diStartAddress"), out diStartAddress);
- int.TryParse(nodeParameter.GetAttribute("aoStartAddress"), out aoStartAddress);
- int.TryParse(nodeParameter.GetAttribute("aiStartAddress"), out aiStartAddress);
- _port = int.Parse(strPort);
- _ip = strIp;
- _socketId = int.Parse(networkId);
- _stationId = int.Parse(stationId);
- }
- protected override void Open()
- {
- melsec_net = new MelsecMcNet(_ip, _port);
- _bufferOut = new byte[2048];
- _bufferIn = new byte[2048];
- melsec_net.ConnectClose();
- try
- {
- OperateResult connect = melsec_net.ConnectServer();
- if (connect.IsSuccess)
- {
- SetState(IoProviderStateEnum.Opened);
- }
- }
- catch (Exception ex)
- {
- LOG.Error(ex.Message);
- }
- }
- protected override void Close()
- {
- melsec_net.ConnectClose();
- SetState(IoProviderStateEnum.Closed);
- }
- protected override bool[] ReadDi(int offset, int size)
- {
- bool[] buff = new bool[size];
- int count = size / perIoLength;
- if (count < 1)
- {
- bool[] dibuffer = DoReadDi(diStartAddress, (ushort)size);
- if (dibuffer != null)
- Array.Copy(dibuffer, 0, buff, 0, dibuffer.Length);
- else
- return null;
- }
- else
- {
- bool[] dibuffer;
- for (int i = 0; i < count; i++)
- {
- dibuffer = DoReadDi(i * perIoLength + diStartAddress, perIoLength);
- if (dibuffer != null)
- Array.Copy(dibuffer, 0, buff, i * perIoLength, dibuffer.Length);
- else
- return null;
- }
- if (size % perIoLength != 0)
- {
- dibuffer = DoReadDi(diStartAddress + perIoLength * count, (ushort)(size % perIoLength));
- if (dibuffer != null)
- Array.Copy(dibuffer, 0, buff, size - size % perIoLength, dibuffer.Length);
- else
- return null;
- }
- }
- return buff;
- }
- protected bool[] ReadDo(int offset, int size)
- {
- bool[] buff = new bool[size];
- int count = size / perIoLength;
- if (count < 1)
- {
- bool[] dobuffer = DoReadDo(doStartAddress, (ushort)size);
- if (dobuffer != null)
- Array.Copy(dobuffer, 0, buff, 0, dobuffer.Length);
- else
- return null;
- }
- else
- {
- bool[] dobuffer;
- for (int i = 0; i < count; i++)
- {
- dobuffer = DoReadDo(i * perIoLength + doStartAddress, perIoLength);
- if (dobuffer != null)
- Array.Copy(dobuffer, 0, buff, i * perIoLength, dobuffer.Length);
- else
- return null;
- }
- if (size % perIoLength != 0)
- {
- dobuffer = DoReadDo(doStartAddress + perIoLength * count, (ushort)(size % perIoLength));
- if (dobuffer != null)
- Array.Copy(dobuffer, 0, buff, size - size % perIoLength, dobuffer.Length);
- else
- return null;
- }
- }
- return buff;
- }
- protected override short[] ReadAi(int offset, int size)
- {
- short[] buff = new short[size];
- int count = size / perIoLength;
- if (count < 1)
- {
- short[] aibuffer = DoReadAi(aiStartAddress, size);
- if (aibuffer != null)
- Array.Copy(aibuffer, 0, buff, 0, aibuffer.Length);
- }
- else
- {
- short[] aibuffer;
- for (int i = 0; i < count; i++)
- {
- aibuffer = DoReadAi(i * perIoLength + aiStartAddress, perIoLength);
- if (aibuffer != null)
- Array.Copy(aibuffer, 0, buff, i * perIoLength, aibuffer.Length);
- }
- if (size % perIoLength != 0)
- {
- aibuffer = DoReadAi(aiStartAddress + perIoLength * count, (ushort)(size % perIoLength));
- if (aibuffer != null)
- Array.Copy(aibuffer, 0, buff, size - size % perIoLength, aibuffer.Length);
- }
- }
- return buff;
- }
- protected short[] ReadAo(int offset, int size)
- {
- short[] buff = new short[size];
- int count = size / perIoLength;
- if (count < 1)
- {
- short[] aobuffer = DoReadAo(aoStartAddress, (ushort)size);
- if (aobuffer != null)
- Array.Copy(aobuffer, 0, buff, 0, aobuffer.Length);
- }
- else
- {
- short[] aobuffer;
- for (int i = 0; i < count; i++)
- {
- aobuffer = DoReadAo(i * perIoLength + aoStartAddress, perIoLength);
- if (aobuffer != null)
- Array.Copy(aobuffer, 0, buff, i * perIoLength, aobuffer.Length);
- }
- if (size % perIoLength != 0)
- {
- aobuffer = DoReadAo(aoStartAddress + perIoLength * count, (ushort)(size % perIoLength));
- if (aobuffer != null)
- Array.Copy(aobuffer, 0, buff, size - size % perIoLength, aobuffer.Length);
- }
- }
- return buff;
- }
- protected override void WriteDo(int offset, bool[] data)
- {
- bool[] databuffer = new bool[perIoLength];
- int count = data.Length / perIoLength;
- if (count < 1)
- {
- Array.Copy(data, 0, databuffer, 0, data.Length);
- Array.Resize(ref databuffer, data.Length);
- DoWriteDo(offset, databuffer);
- }
- else
- {
- for (int i = 0; i < count; i++)
- {
- Array.Copy(data, i * perIoLength, databuffer, 0, databuffer.Length);
- DoWriteDo(offset + perIoLength * i, databuffer);
- }
- if (data.Length % perIoLength != 0)
- {
- Array.Copy(data, perIoLength * count, databuffer, 0, data.Length % perIoLength);
- Array.Resize(ref databuffer, data.Length % perIoLength);
- DoWriteDo(offset + perIoLength * count, databuffer);
- }
-
- }
- }
- protected override void WriteAo(int offset, short[] data)
- {
- short[] databuffer = new short[perIoLength];
- int count = data.Length / perIoLength;
- if (count < 1)
- {
- Array.Copy(data, 0, databuffer, 0, data.Length);
- Array.Resize(ref databuffer, data.Length);
- DoWriteAo(offset, databuffer);
- }
- else
- {
- for (int i = 0; i < count; i++)
- {
- Array.Copy(data, i * perIoLength, databuffer, 0, databuffer.Length);
- DoWriteAo(offset + perIoLength * i, databuffer, data.Length, i);
- }
- if(data.Length % perIoLength != 0)
- {
- Array.Copy(data, perIoLength * count, databuffer, 0, data.Length % perIoLength);
- Array.Resize(ref databuffer, data.Length % perIoLength);
- DoWriteAo(offset + perIoLength * count, databuffer, data.Length);
- }
-
- }
- }
- public void RecipeData(int offset, short[] data)
- {
- recipeOffset = offset;
- recipeData = data;
- isWriteRecipe = true;
- }
- public void WriteRecipe(int offset, short[] data)
- {
- short[] databuffer = new short[perIoLength];
- int count = data.Length / perIoLength;
- if (count < 1)
- {
- Array.Copy(data, 0, databuffer, 0, data.Length);
- Array.Resize(ref databuffer, data.Length);
- DoWriteAo(offset, databuffer);
- }
- else
- {
- for (int i = 0; i < count; i++)
- {
- Array.Copy(data, i * perIoLength, databuffer, 0, databuffer.Length);
- DoWriteAo(offset + perIoLength * i, databuffer, data.Length, i);
- }
- if (data.Length % perIoLength != 0)
- {
- Array.Copy(data, perIoLength * count, databuffer, 0, data.Length % perIoLength);
- Array.Resize(ref databuffer, data.Length % perIoLength);
- DoWriteAo(offset + perIoLength * count, databuffer, data.Length);
- }
- }
- }
- private short[] DoReadAi(int offset, int size)
- {
- OperateResult<short[]> result = melsec_net.ReadInt16($"{aiStoragename}{offset}",(ushort)size);
- if (!result.IsSuccess)
- {
- LOG.Error(result.Message);
- SetState(IoProviderStateEnum.Error);
- return null;
- }
- return result.Content;
- }
- private short[] DoReadAo(int offset, ushort size)
- {
- OperateResult<short[]> result = melsec_net.ReadInt16($"{aoStoragename}{offset}", size);
- if (!result.IsSuccess)
- {
- LOG.Error(result.Message);
- SetState(IoProviderStateEnum.Error);
- return null;
- }
- return result.Content;
- }
- private bool DoWriteAo(int offset, short[] data, int total = 0, int count = 100)
- {
- OperateResult result = melsec_net.Write($"{aoStoragename}{offset}", data);
- if (!result.IsSuccess)
- {
- LOG.Error(result.Message);
- SetState(IoProviderStateEnum.Error);
- }
- return result.IsSuccess;
- }
- private bool[] DoReadDi(int offset, ushort size)
- {
- OperateResult<bool[]> result = melsec_net.ReadBool($"{diStoragename}{offset.ToString("X")}", size);
- if (!result.IsSuccess)
- {
- LOG.Error(result.Message);
- SetState(IoProviderStateEnum.Error);
- return null;
- }
- return result.Content;
- }
- private bool[] DoReadDo(int offset, ushort size)
- {
- OperateResult<bool[]> result = melsec_net.ReadBool($"{doStoragename}{offset.ToString("X")}", size);
- if (!result.IsSuccess)
- {
- LOG.Error(result.Message);
- SetState(IoProviderStateEnum.Error);
- return null;
- }
- return result.Content;
- }
- private bool DoWriteDo(int offset, bool[] data)
- {
- OperateResult result = melsec_net.Write($"{doStoragename}{offset.ToString("X")}", data);
- if (!result.IsSuccess)
- {
- LOG.Error(result.Message);
- SetState(IoProviderStateEnum.Error);
- }
- return result.IsSuccess;
- }
- public override bool SetValue(AOAccessor aoItem, short value)
- {
- if (State != IoProviderStateEnum.Opened)
- return false;
- //WriteAo(aoStartAddress + aoItem.Index, new short[] { value });
- return true;
- }
- public override bool SetValueFloat(AOAccessor aoItem, float value)
- {
- if (State != IoProviderStateEnum.Opened)
- return false;
- return true;
- }
- public override bool SetValue(DOAccessor doItem, bool value)
- {
- //if (State != IoProviderStateEnum.Opened)
- // return false;
- //if (!IO.CanSetDO(doItem.Name, value, out string reason))
- //{
- // LOG.Error(reason);
- // return false;
- //}
- ////foreach (var bufferSection in _blockSections)
- ////{
- //// if (bufferSection.Type == IoType.DO)
- //// {
- //// bool[] doBuffer = ReadDo(bufferSection.Offset, bufferSection.Size);
- //// if (doBuffer != null)
- //// {
- //// _buffer.SetDoBuffer(_source, bufferSection.Offset, doBuffer);
- //// }
- //// }
- ////}
- //Dictionary<int, bool[]> dos = _buffer.GetDoBuffer(_source);
- //if (dos != null)
- //{
- // foreach (var doo in dos)
- // {
- // //doo.Value[doItem.Index] = value;
- // //WriteDo(doStartAddress, doo.Value);
- // WriteDo(doStartAddress + doItem.Index, new bool[] { value });
- // }
- //}
- return true;
- }
- }
-
- }
|