| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 | 
							- using Aitex.Core.RT.IOCore;
 
- using MECF.Framework.Common.IOCore;
 
- using MECF.Framework.RT.Core.IoProviders;
 
- using MECF.Framework.Simulator.Core.IoProviders;
 
- using System;
 
- using System.Collections.Generic;
 
- using System.Collections.ObjectModel;
 
- using System.Linq;
 
- using System.Text;
 
- using System.Threading.Tasks;
 
- namespace FurnaceSimulator.Instances
 
- {
 
-     public class SimulatorPlc : MCProtocolPlcSimulator
 
-     {
 
-         private Random _rd = new Random();
 
-         public ObservableCollection<NotifiableIoItem> DiItemList { get; set; }
 
-         public ObservableCollection<NotifiableIoItem> DoItemList { get; set; }
 
-         public ObservableCollection<NotifiableIoItem> AiItemList { get; set; }
 
-         public ObservableCollection<NotifiableIoItem> AoItemList { get; set; }
 
-         private string _source;
 
-         public const int DIOBufferSize = 10000;
 
-         public const int AIOBufferSize = 60000;
 
-         public SimulatorPlc(int port, string source, string ioMapPathFile, string module)
 
-             : base("127.0.0.1", port)
 
-         {
 
-             _source = source;
 
-             _buffers.Add(new PlcBuffer() { Buffer = new byte[DIOBufferSize], Type = IoType.DI, Offset = 0, Size = DIOBufferSize, BoolValue = new bool[DIOBufferSize] });
 
-             _buffers.Add(new PlcBuffer() { Buffer = new byte[DIOBufferSize], Type = IoType.DO, Offset = 0, Size = DIOBufferSize, BoolValue = new bool[DIOBufferSize] });
 
-             _buffers.Add(new PlcBuffer() { Buffer = new byte[AIOBufferSize], Type = IoType.AI, Offset = 0, Size = AIOBufferSize, ShortValue = new ushort[AIOBufferSize], FloatValue = new float[AIOBufferSize] });
 
-             _buffers.Add(new PlcBuffer() { Buffer = new byte[AIOBufferSize], Type = IoType.AO, Offset = 0, Size = AIOBufferSize, ShortValue = new ushort[AIOBufferSize], FloatValue = new float[AIOBufferSize] });
 
-             List<IoBlockItem> lstBuffers = new List<IoBlockItem>();
 
-             lstBuffers.Add(new IoBlockItem() { Type = IoType.DI, Offset = 0, Size = DIOBufferSize });
 
-             lstBuffers.Add(new IoBlockItem() { Type = IoType.DO, Offset = 0, Size = DIOBufferSize });
 
-             lstBuffers.Add(new IoBlockItem() { Type = IoType.AI, Offset = 0, Size = AIOBufferSize });
 
-             lstBuffers.Add(new IoBlockItem() { Type = IoType.AO, Offset = 0, Size = AIOBufferSize });
 
-             IoManager.Instance.SetBufferBlock(source, lstBuffers);
 
-             IoManager.Instance.SetIoMap(source, 0, ioMapPathFile, module);
 
-             Init();
 
-             SetDefaultValue();
 
-             //SetScCommand = new DelegateCommand<string>(SetSc);
 
-         }
 
-         //private void SetSc(string obj)
 
-         //{
 
-         //    NotifiableSCConfigItem item = ScItemList.First(x => x.Name == obj);
 
-         //    if (item.Type != SCConfigType.StringType.ToString() && string.IsNullOrEmpty(item.SetPoint))
 
-         //        return;
 
-         //    SC.SetItemValue(obj, item.SetPoint == null ? "" : item.SetPoint);
 
-         //    item.StringValue = SC.GetItemValue(obj).ToString();
 
-         //    item.InvokePropertyChanged(nameof(item.StringValue));
 
-         //}
 
-         void Init()
 
-         {
 
-             if (DiItemList == null)
 
-             {
 
-                 List<DIAccessor> diItems = IO.GetDiList(_source);
 
-                 if (diItems != null)
 
-                 {
 
-                     DiItemList = new ObservableCollection<NotifiableIoItem>();
 
-                     foreach (var diItem in diItems)
 
-                     {
 
-                         NotifiableIoItem item = new NotifiableIoItem()
 
-                         {
 
-                             Name = diItem.Name,
 
-                             Index = DiItemList.Count,
 
-                             Description = diItem.Description,
 
-                             BoolValue = diItem.Value,
 
-                             Address = diItem.Addr,
 
-                             BlockOffset = diItem.BlockOffset,
 
-                             BlockIndex = diItem.Index,
 
-                         };
 
-                         DiItemList.Add(item);
 
-                     }
 
-                 }
 
-             }
 
-             if (DoItemList == null)
 
-             {
 
-                 List<DOAccessor> doItems = IO.GetDoList(_source);
 
-                 if (doItems != null)
 
-                 {
 
-                     DoItemList = new ObservableCollection<NotifiableIoItem>();
 
-                     foreach (var ioItem in doItems)
 
-                     {
 
-                         NotifiableIoItem item = new NotifiableIoItem()
 
-                         {
 
-                             Name = ioItem.Name,
 
-                             Index = DoItemList.Count,
 
-                             Description = ioItem.Description,
 
-                             BoolValue = ioItem.Value,
 
-                             Address = ioItem.Addr,
 
-                             BlockOffset = ioItem.BlockOffset,
 
-                             BlockIndex = ioItem.Index,
 
-                         };
 
-                         DoItemList.Add(item);
 
-                     }
 
-                 }
 
-             }
 
-             if (AiItemList == null)
 
-             {
 
-                 List<AIAccessor> aiItems = IO.GetAiList(_source);
 
-                 if (aiItems != null)
 
-                 {
 
-                     AiItemList = new ObservableCollection<NotifiableIoItem>();
 
-                     foreach (var ioItem in aiItems)
 
-                     {
 
-                         NotifiableIoItem item = new NotifiableIoItem()
 
-                         {
 
-                             Name = ioItem.Name,
 
-                             Index = AiItemList.Count,
 
-                             Description = ioItem.Description,
 
-                             ShortValue = ioItem.Value,
 
-                             Address = ioItem.Addr,
 
-                             BlockOffset = ioItem.BlockOffset,
 
-                             BlockIndex = ioItem.Index,
 
-                         };
 
-                         AiItemList.Add(item);
 
-                     }
 
-                 }
 
-             }
 
-             if (AoItemList == null)
 
-             {
 
-                 List<AOAccessor> aoItems = IO.GetAoList(_source);
 
-                 if (aoItems != null)
 
-                 {
 
-                     AoItemList = new ObservableCollection<NotifiableIoItem>();
 
-                     foreach (var ioItem in aoItems)
 
-                     {
 
-                         NotifiableIoItem item = new NotifiableIoItem()
 
-                         {
 
-                             Name = ioItem.Name,
 
-                             Index = AoItemList.Count,
 
-                             Description = ioItem.Description,
 
-                             ShortValue = ioItem.Value,
 
-                             Address = ioItem.Addr,
 
-                             BlockOffset = ioItem.BlockOffset,
 
-                             BlockIndex = ioItem.Index,
 
-                         };
 
-                         AoItemList.Add(item);
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         private void SetDefaultValue()
 
-         {
 
-             //IO.DI[IoNameSorter6LP.DI_Maintenance].Value = true;
 
-         }
 
-         protected override bool OnTimer()
 
-         {
 
-             bool[] diBuffer = new bool[DIOBufferSize];
 
-             if (DiItemList != null)
 
-             {
 
-                 foreach (var notifiableIoItem in DiItemList)
 
-                 {
 
-                     if (notifiableIoItem.HoldValue)
 
-                     {
 
-                         IO.DI[notifiableIoItem.Name].Value = notifiableIoItem.BoolValue;
 
-                         diBuffer[notifiableIoItem.BlockIndex] = notifiableIoItem.BoolValue;
 
-                     }
 
-                     else
 
-                     {
 
-                         notifiableIoItem.BoolValue = IO.DI[notifiableIoItem.Name].Value;
 
-                         diBuffer[notifiableIoItem.BlockIndex] = IO.DI[notifiableIoItem.Name].Value;
 
-                     }
 
-                     notifiableIoItem.InvokePropertyChanged("BoolValue");
 
-                 }
 
-             }
 
-             if (DoItemList != null)
 
-             {
 
-                 foreach (var notifiableIoItem in DoItemList)
 
-                 {
 
-                     notifiableIoItem.BoolValue = IO.DO[notifiableIoItem.Name].Value;
 
-                     notifiableIoItem.InvokePropertyChanged("BoolValue");
 
-                 }
 
-             }
 
-             if (AiItemList != null)
 
-             {
 
-                 foreach (var notifiableIoItem in AiItemList)
 
-                 {
 
-                     if (notifiableIoItem.HoldValue)
 
-                     {
 
-                         IO.AI[notifiableIoItem.Name].Value = notifiableIoItem.ShortValue;
 
-                         IO.AI[notifiableIoItem.Name].FloatValue = notifiableIoItem.FloatValue;
 
-                     }
 
-                     notifiableIoItem.ShortValue = IO.AI[notifiableIoItem.Name].Value;
 
-                     notifiableIoItem.InvokePropertyChanged("ShortValue");
 
-                     notifiableIoItem.FloatValue = IO.AI[notifiableIoItem.Name].FloatValue;
 
-                     notifiableIoItem.InvokePropertyChanged("FloatValue");
 
-                 }
 
-             }
 
-             if (AoItemList != null)
 
-             {
 
-                 foreach (var notifiableIoItem in AoItemList)
 
-                 {
 
-       
 
-                     notifiableIoItem.ShortValue = IO.AO[notifiableIoItem.Name].Value;
 
-                     notifiableIoItem.InvokePropertyChanged("ShortValue");
 
-                     notifiableIoItem.FloatValue = IO.AO[notifiableIoItem.Name].FloatValue;
 
-                     notifiableIoItem.InvokePropertyChanged("FloatValue");
 
-                 }
 
-             }
 
-             foreach (var plcBuffer in _buffers)
 
-             {
 
-                 //IO修改 ---> PLC
 
-                 if (plcBuffer.Type == IoType.DI)
 
-                 {
 
-                     var ioBuffers = IoManager.Instance.GetDiBuffer(_source);
 
-                     if (ioBuffers != null)
 
-                     {
 
-                         foreach (var ioBuffer in ioBuffers)
 
-                         {
 
-                             if (plcBuffer.Offset == ioBuffer.Key)
 
-                             {
 
-                                 //plcBuffer.BoolValue = ioBuffer.Value;
 
-                                 for (int i = 0; i < plcBuffer.BoolValue.Length; i++)
 
-                                 {
 
-                                     plcBuffer.BoolValue[i] = diBuffer[i];
 
-                                 }
 
-                             }
 
-                         }
 
-                     }
 
-                 }
 
-                 // PLC --> IO
 
-                 if (plcBuffer.Type == IoType.DO)
 
-                 {
 
-                     var ioBuffers = IoManager.Instance.GetDoBuffer(_source);
 
-                     if (ioBuffers != null)
 
-                     {
 
-                         foreach (var buffer in ioBuffers)
 
-                         {
 
-                             if (plcBuffer.Offset == buffer.Key)
 
-                             {
 
-                                 //IoManager.Instance.SetDoBuffer(_source, plcBuffer.Offset, plcBuffer.BoolValue);
 
-                             }
 
-                         }
 
-                     }
 
-                 }
 
-                 //IO修改 ---> PLC
 
-                 if (plcBuffer.Type == IoType.AI)
 
-                 {
 
-                     var ioBuffers = IoManager.Instance.GetAiBuffer(_source);
 
-                     if (ioBuffers != null)
 
-                     {
 
-                         foreach (var buffer in ioBuffers)
 
-                         {
 
-                             if (plcBuffer.Offset == buffer.Key)
 
-                             {
 
-                                 plcBuffer.ShortValue = Array.ConvertAll<short, ushort>(buffer.Value, x => (ushort)x);
 
-                             }
 
-                         }
 
-                     }
 
-                     var ioBuffersFloat = IoManager.Instance.GetAiBufferFloat(_source);
 
-                     if (ioBuffers != null)
 
-                     {
 
-                         foreach (var buffer in ioBuffersFloat)
 
-                         {
 
-                             if (plcBuffer.Offset == buffer.Key)
 
-                             {
 
-                                 plcBuffer.FloatValue = buffer.Value;
 
-                             }
 
-                         }
 
-                     }
 
-                 }
 
-                 // PLC --> IO
 
-                 if (plcBuffer.Type == IoType.AO)
 
-                 {
 
-                     var ioBuffers = IoManager.Instance.GetAoBuffer(_source);
 
-                     if (ioBuffers != null)
 
-                     {
 
-                         foreach (var buffer in ioBuffers)
 
-                         {
 
-                             if (plcBuffer.Offset == buffer.Key)
 
-                             {
 
-                                 //IoManager.Instance.SetAoBuffer(_source, plcBuffer.Offset, Array.ConvertAll<ushort, short>(plcBuffer.ShortValue, x => (short)x));
 
-                             }
 
-                         }
 
-                     }
 
-                 }
 
-             }
 
-             return true;
 
-         }
 
-     }
 
- }
 
 
  |