using Aitex.Core.RT.Log; using Aitex.Core.Util; using MECF.Framework.RT.Core.IoProviders; using System; using System.Collections.Generic; using System.Xml; namespace athosRT.Devices.IO { public class AdvantecIoCard : IoProvider { private DICard _diCard; private DOCard _doCard; private IOCard _ioCard; private string _type; protected override void SetParameter(XmlElement nodeParameter) { string attribute1 = nodeParameter.GetAttribute("name"); int deviceID = int.Parse(nodeParameter.GetAttribute("device_id")); this._type = nodeParameter.GetAttribute("type"); string attribute2 = nodeParameter.GetAttribute("description"); int port = int.Parse(nodeParameter.GetAttribute("port")); if (this._type == "di") { this._diCard = new DICard(attribute1, deviceID); this._diCard.Open(); } else if (this._type == "do") { this._doCard = new DOCard(attribute1, deviceID); this._doCard.Open(); } else { if (!(this._type == "dio")) return; this._ioCard = new IOCard(attribute2, port); } } protected override bool OnTimer() { try { foreach (IoBlockItem blockSection in this._blockSections) { if (blockSection.Type == IoType.DI) { bool[] buffer = this.ReadDi(blockSection.Offset, (int) blockSection.Size); if (buffer != null) this._buffer.SetDiBuffer(this._source, blockSection.Offset, buffer); } } Dictionary doBuffer = this._buffer.GetDoBuffer(this._source); if (doBuffer != null) { foreach (KeyValuePair keyValuePair in doBuffer) this.WriteDo(keyValuePair.Key, keyValuePair.Value); } } catch (Exception ex) { LOG.Write(ex, 2, "D:\\sorter\\trunk\\Efem\\Jet\\Jet_001_2P_Jet\\EfemRT\\Devices\\AdvantecIoCard.cs", nameof (OnTimer), 75); this.Close(); } return true; } public override void Initialize(string module, string name, List lstBuffers, IIoBuffer buffer, XmlElement nodeParameter, Dictionary ioMappingPathFile) { Module = module; Name = name; _source = module + "." + name; _buffer = buffer; _nodeParameter = nodeParameter; _blockSections = lstBuffers; buffer.SetBufferBlock(_source, lstBuffers); buffer.SetIoMap(_source, ioMappingPathFile); SetParameter(nodeParameter); State = IoProviderStateEnum.Uninitialized; _thread = new PeriodicJob(50, OnTimer, name); } public override void Initialize(string module, string name, List 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; _thread = new PeriodicJob(50, OnTimer, name); } protected override void Open() { } protected override void Close() { } protected override bool[] ReadDi(int offset, int size) { if (this._type == "dio") { byte[] buffer; if (this._ioCard == null || !this._ioCard.Read(out buffer)) return (bool[]) null; bool[] flagArray = new bool[size]; for (int index = 0; index < size && index < buffer.Length; ++index) flagArray[index] = buffer[index] == (byte) 1; return flagArray; } if (this._type != "di" || this._diCard == null) return (bool[]) null; byte[] di = this._diCard.DI; if (di == null || di.Length == 0) return (bool[]) null; bool[] flagArray1 = new bool[size]; for (int index = 0; index < size && index < di.Length; ++index) flagArray1[index] = di[index] == (byte) 1; return flagArray1; } protected override short[] ReadAi(int offset, int size) => (short[]) null; protected override void WriteDo(int offset, bool[] buffer) { if (this._type == "dio") { if (this._ioCard == null || buffer == null || buffer.Length == 0) return; this._ioCard.Write(Array.ConvertAll(buffer, (Converter) (x => !x ? (byte) 0 : (byte) 1))); } else { if (this._type != "do" || this._doCard == null || buffer == null || buffer.Length == 0) return; this._doCard.Write(Array.ConvertAll(buffer, (Converter) (x => !x ? (byte) 0 : (byte) 1))); } } protected override void WriteAo(int offset, short[] buffer) { } } }