| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 | 
							- using System;
 
- using System.Collections.Generic;
 
- using System.Diagnostics;
 
- using System.Threading;
 
- using System.Xml;
 
- using Aitex.Core.RT.DataCenter;
 
- using Aitex.Core.RT.Event;
 
- using Aitex.Core.RT.Log;
 
- using Aitex.Core.Util;
 
- namespace MECF.Framework.RT.Core.IoProviders
 
- {
 
-     public abstract class IoProvider : IIoProvider
 
-     {
 
-         public string Module { get; set; }
 
-         public string Name { get; set; }
 
-         public bool IsOpened
 
-         {
 
-             get { return State == IoProviderStateEnum.Opened; }
 
-         }
 
-         public IoProviderStateEnum State { get; set; }
 
-  
 
-         protected PeriodicJob _thread;
 
-         protected IIoBuffer _buffer;
 
-         protected R_TRIG _trigError = new R_TRIG();
 
-         protected string _source;
 
-         protected XmlElement _nodeParameter;
 
-         protected List<IoBlockItem> _blockSections;
 
-         protected abstract void SetParameter(XmlElement nodeParameter);
 
-         protected abstract void Open();
 
-         protected abstract void Close();
 
-         protected abstract bool[] ReadDi(int offset, int size);
 
-         protected abstract short[] ReadAi(int offset, int size);
 
-         protected abstract void WriteDo(int offset, bool[] buffer);
 
-         protected abstract void WriteAo(int offset, short[] buffer);
 
-  
 
-         public virtual void Initialize(string module, string name,List<IoBlockItem> lstBuffers , IIoBuffer buffer, XmlElement nodeParameter, Dictionary<int, string> 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 virtual 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;
 
-             _thread = new PeriodicJob(50, OnTimer, name);
 
-         }
 
-         protected void SetState(IoProviderStateEnum newState)
 
-         {
 
-             State = newState;
 
-         }
 
-         void TraceArray(bool[] data)
 
-         {
 
-             string[] values = new string[data.Length];
 
-  
 
-             for (int i = 0; i < data.Length; i++)
 
-             {
 
-                 values[i++] = data[i] ? "1" : "0";
 
-             }
 
-             System.Diagnostics.Trace.WriteLine(string.Join(",", values));
 
-         }
 
-         protected virtual bool OnTimer()
 
-         {
 
-             if (State == IoProviderStateEnum.Uninitialized)
 
-             {
 
-                 SetState(IoProviderStateEnum.Opening);
 
-                 Open();
 
-             }
 
-             if (State == IoProviderStateEnum.Opened)
 
-             {
 
-                 try
 
-                 {
 
-                     foreach (var bufferSection in _blockSections)
 
-                     {
 
-                         if (bufferSection.Type == IoType.DI)
 
-                         {
 
-                             bool[] diBuffer = ReadDi(bufferSection.Offset, bufferSection.Size);
 
-                             if (diBuffer != null)
 
-                             {
 
-                                 _buffer.SetDiBuffer(_source, bufferSection.Offset, diBuffer);
 
-                                 //TraceArray(diBuffer);
 
-                             }
 
-                         }
 
-                         else if (bufferSection.Type == IoType.AI)
 
-                         {
 
-                             short[] aiBuffer = ReadAi(bufferSection.Offset, bufferSection.Size);
 
-                             if (aiBuffer != null)
 
-                             {
 
-                                 _buffer.SetAiBuffer(_source, bufferSection.Offset, aiBuffer);
 
-                             }
 
-                         }
 
-                     }
 
-                     Dictionary<int, short[]> aos = _buffer.GetAoBuffer(_source);
 
-                     if (aos != null)
 
-                     {
 
-                         foreach (var ao in aos)
 
-                         {
 
-                             WriteAo(ao.Key, ao.Value);
 
-                         }
 
-                     }
 
-                     Dictionary<int, bool[]> dos = _buffer.GetDoBuffer(_source);
 
-                     if (dos != null)
 
-                     {
 
-                         foreach (var doo in dos)
 
-                         {
 
-                             WriteDo(doo.Key, doo.Value);
 
-                         }
 
-                     }
 
-                 }
 
-                 catch (Exception ex)
 
-                 {
 
-                     LOG.WriteExeption(ex);
 
-                     SetState(IoProviderStateEnum.Error);
 
-                     Close();
 
-                 }
 
-             }
 
-             _trigError.CLK = State == IoProviderStateEnum.Error;
 
-             if (_trigError.Q)
 
-             {
 
-                 EV.PostMessage(Module, EventEnum.DefaultAlarm, string.Format("{0} error", _source));
 
-             }
 
-             return true;
 
-         }
 
-  
 
-         public void Reset()
 
-         {
 
-             _trigError.RST = true;
 
-         }
 
-  
 
-         public void Start()
 
-         {
 
-             if(_thread != null)
 
-                 _thread.Start();
 
-         }
 
-         public void Stop()
 
-         {
 
-             SetState(IoProviderStateEnum.Closing);
 
-             Close();
 
-             _thread.Stop();
 
-         }
 
-     }
 
- }
 
 
  |