终端一体化运控平台
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

129 linhas
4.7 KiB

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