IOMonitorView.xaml.cs 7.1 KB

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