终端一体化运控平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

132 строки
4.9 KiB

  1. using BPA.Helper;
  2. using BPASmartClient.JXJFoodBigStation.Model;
  3. using BPASmartClient.JXJFoodBigStation.Model.HK_PLC;
  4. using BPA.Helper;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.ComponentModel;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. namespace BPASmartClient.JXJFoodBigStation.ViewModel
  15. {
  16. public class HKPlcCommMonitorViewModel:NotifyBase
  17. {
  18. public HKPlcCommMonitorViewModel()
  19. {
  20. Type type = typeof(DB_Read);
  21. PropertyInfo[] properties = type.GetProperties();
  22. double Offset = 0.0;
  23. string addressPos = "";
  24. TaskManage.GetInstance.StartLong(new Action(() =>
  25. {
  26. foreach (PropertyInfo mi in properties)
  27. {
  28. PropertyInfo a = GVL_BigStation.HKPlc_Read.GetType().GetProperty(mi.Name);
  29. object ab = a.GetValue(GVL_BigStation.HKPlc_Read, null);
  30. int index = Array.FindIndex(PlcInfo.ToArray(), p => p.Variable == mi.Name);
  31. if (index != -1)
  32. {
  33. PlcInfo.ElementAt(index).NowValue = ab.ToString();
  34. }
  35. }
  36. Thread.Sleep(100);
  37. }), "plc变量读取",true);
  38. if (PlcInfo.Count == 0)
  39. {
  40. foreach (PropertyInfo mi in properties)
  41. {
  42. PropertyInfo a = GVL_BigStation.HKPlc_Read.GetType().GetProperty(mi.Name);
  43. object ab = a.GetValue(GVL_BigStation.HKPlc_Read, null);
  44. if (mi.PropertyType.Name == "Int16")
  45. {
  46. addressPos = "DBW";
  47. Offset = Math.Ceiling(Offset);
  48. if (Offset / 2.0 - Math.Floor(Offset / 2.0) > 0.0)
  49. {
  50. Offset += 1.0;
  51. }
  52. }
  53. else if (mi.PropertyType.Name == "Boolean")
  54. {
  55. addressPos = "DBX";
  56. Offset = Math.Round(Offset, 1);
  57. if ((Offset - Math.Floor(Offset)) >= 0.8)
  58. {
  59. Offset = Math.Ceiling(Offset);
  60. }
  61. }
  62. else if (mi.PropertyType.Name == "Single")
  63. {
  64. addressPos = "DBD";
  65. Offset = Math.Ceiling(Offset);
  66. if (Offset / 2.0 - Math.Floor(Offset / 2.0) > 0.0)
  67. {
  68. Offset += 1.0;
  69. }
  70. }
  71. var num = GVL_BigStation.HKPlc_Read;
  72. var res = a.GetCustomAttribute<PlcCommAttribute>();
  73. string describe = "";
  74. if (res != null)
  75. {
  76. describe = res.Describe;
  77. }
  78. else
  79. {
  80. describe = "";
  81. }
  82. PlcInfo.Add(new PlcVarMonitor()
  83. {
  84. SerialNum = PlcInfo.Count + 1,
  85. Variable = mi.Name,
  86. Address = "DB98." + addressPos + string.Format("{0:N1}", Offset),
  87. Type = mi.PropertyType.Name,
  88. Describe = describe,
  89. NowValue = ab.ToString(),
  90. });
  91. if (mi.PropertyType.Name == "Int16")
  92. {
  93. Offset += 2;
  94. }
  95. else if (mi.PropertyType.Name == "Boolean")
  96. {
  97. Offset += 0.1;
  98. }
  99. else if (mi.PropertyType.Name == "Single")
  100. {
  101. Offset += 4;
  102. }
  103. }
  104. }
  105. SetValueCommand = new BPARelayCommand<object>((o) =>
  106. {
  107. /*if (o != null && o is String address)
  108. {
  109. int index = Array.FindIndex(PlcInfo.ToArray(), p => p.Address == address);
  110. if (index != -1)
  111. {
  112. if (ProcessControl.GetInstance.HKDevice.IsConnected) ProcessControl.GetInstance.HKDevice.HK_PLC_S7.Write(address, PlcInfo.ElementAt(index).SetValue);
  113. }
  114. }*/
  115. });
  116. }
  117. /*public static string GetPropertyValue<T>( T t,string PropertyName)
  118. {
  119. Type type = typeof(T);
  120. }*/
  121. public static ObservableCollection<PlcVarMonitor> PlcInfo { get; set; } = new ObservableCollection<PlcVarMonitor>();
  122. public BPARelayCommand<object> SetValueCommand { get; set; }
  123. }
  124. }