IOMonitorView.xaml.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.ComponentModel;
  4. using System.Reflection;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Input;
  8. using Aitex.Core.RT.IOCore;
  9. using Aitex.Core.UI.MVVM;
  10. using MECF.Framework.Common.IOCore;
  11. namespace VirgoRT.Backends
  12. {
  13. /// <summary>
  14. /// IOView.xaml 的交互逻辑
  15. /// </summary>
  16. public partial class IOView : UserControl
  17. {
  18. public IOView()
  19. {
  20. InitializeComponent();
  21. DataContext = new IOMonitorViewModel();
  22. this.IsVisibleChanged += IOView_IsVisibleChanged;
  23. }
  24. private void IOView_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  25. {
  26. (DataContext as TimerViewModelBase).EnableTimer(IsVisible);
  27. }
  28. }
  29. public class IoValues
  30. {
  31. public ObservableCollection<NotifiableIoItem> DiItemList { get; set; }
  32. public ObservableCollection<NotifiableIoItem> DoItemList { get; set; }
  33. public ObservableCollection<NotifiableIoItem> AiItemList { get; set; }
  34. public ObservableCollection<NotifiableIoItem> AoItemList { get; set; }
  35. #region InitDefine
  36. public IoValues(string source)
  37. {
  38. DiItemList = new ObservableCollection<NotifiableIoItem>();
  39. List<DIAccessor> diItems = IO.GetDiList(source);
  40. if (diItems != null)
  41. {
  42. foreach (var diItem in diItems)
  43. {
  44. NotifiableIoItem item = new NotifiableIoItem()
  45. {
  46. Name = diItem.Name,
  47. Index = diItem.Index,
  48. Description = diItem.Description,
  49. BoolValue = diItem.Value,
  50. Address = diItem.Addr,
  51. };
  52. DiItemList.Add(item);
  53. }
  54. }
  55. DoItemList = new ObservableCollection<NotifiableIoItem>();
  56. List<DOAccessor> doItems = IO.GetDoList(source);
  57. if (doItems != null)
  58. {
  59. foreach (var doItem in doItems)
  60. {
  61. NotifiableIoItem item = new NotifiableIoItem()
  62. {
  63. Name = doItem.Name,
  64. Index = doItem.Index,
  65. Description = doItem.Description,
  66. BoolValue = doItem.Value,
  67. Address = doItem.Addr,
  68. };
  69. DoItemList.Add(item);
  70. }
  71. }
  72. AiItemList = new ObservableCollection<NotifiableIoItem>();
  73. List<AIAccessor> aiItems = IO.GetAiList(source);
  74. if (aiItems != null)
  75. {
  76. foreach (var aiItem in aiItems)
  77. {
  78. NotifiableIoItem item = new NotifiableIoItem()
  79. {
  80. Name = aiItem.Name,
  81. Index = aiItem.Index,
  82. Description = aiItem.Description,
  83. ShortValue = aiItem.Value,
  84. Address = aiItem.Addr,
  85. };
  86. AiItemList.Add(item);
  87. }
  88. }
  89. AoItemList = new ObservableCollection<NotifiableIoItem>();
  90. List<AOAccessor> aoItems = IO.GetAoList(source);
  91. if (aoItems != null)
  92. {
  93. foreach (var aoItem in aoItems)
  94. {
  95. NotifiableIoItem item = new NotifiableIoItem()
  96. {
  97. Name = aoItem.Name,
  98. Index = aoItem.Index,
  99. Description = aoItem.Description,
  100. ShortValue = aoItem.Value,
  101. Address = aoItem.Addr,
  102. };
  103. AoItemList.Add(item);
  104. }
  105. }
  106. }
  107. #endregion
  108. public void UpdateData()
  109. {
  110. foreach (var item in DiItemList)
  111. {
  112. item.BoolValue = IO.DI[item.Name].Value;
  113. item.InvokePropertyChanged("BoolValue");
  114. }
  115. foreach (var item in DoItemList)
  116. {
  117. item.BoolValue = IO.DO[item.Name].Value;
  118. }
  119. foreach (var item in AiItemList)
  120. {
  121. item.ShortValue = IO.AI[item.Name].Value;
  122. }
  123. foreach (var item in AoItemList)
  124. {
  125. item.ShortValue = IO.AO[item.Name].Value;
  126. }
  127. }
  128. }
  129. public class IOMonitorViewModel : TimerViewModelBase
  130. {
  131. public IoValues[] IoList { get; set; }
  132. public IOMonitorViewModel():base(nameof(IOMonitorViewModel))
  133. {
  134. IoList = new IoValues[2];
  135. for (int i = 0; i < IoList.Length; i++)
  136. {
  137. IoList[i] = new IoValues("System.io"+(i+1));
  138. }
  139. }
  140. protected override void Poll()
  141. {
  142. for (int i = 0; i < IoList.Length; i++)
  143. {
  144. IoList[i].UpdateData();
  145. }
  146. //string[] values = new string[128];
  147. //int i = 0;
  148. //foreach (var notifiableIoItem in Card1.DiItemList)
  149. //{
  150. // notifiableIoItem.BoolValue = IO.DI[notifiableIoItem.Name].Value;
  151. // notifiableIoItem.InvokePropertyChanged(nameof(notifiableIoItem.BoolValue));
  152. // values[i++] = notifiableIoItem.BoolValue ? "1" : "0";
  153. //}
  154. //System.Diagnostics.Trace.WriteLine(string.Join(",", values));
  155. }
  156. }
  157. }