版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

201 строка
6.5 KiB

  1. using Advantech.Motion;
  2. using Newtonsoft.Json;
  3. using ProductionControl.Device;
  4. using ProductionControl.Utils;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. using System.Xml.Linq;
  17. namespace ProductionControl.UI
  18. {
  19. public partial class UISmallAxisDev : UserControl
  20. {
  21. SynchronizationContext SyncContext = null;
  22. public Action<string> GetParamsEvent;
  23. //
  24. private SmallAxisDevProp prop = new SmallAxisDevProp(Config.SmallAxis_ComName);
  25. private SmallAxisDev dev;
  26. public UISmallAxisDev()
  27. {
  28. InitializeComponent();
  29. propertyGrid1.PropertyValueChanged += propertyGrid1_PropertyValueChanged;
  30. refreshUIState();
  31. //获取UI线程同步上下文
  32. SyncContext = SynchronizationContext.Current;
  33. //init();
  34. }
  35. public string getParamsData()
  36. {
  37. return prop.serialize();
  38. }
  39. public void setParamsData(string json)
  40. {
  41. if (json == "") return;
  42. prop.deserialize(json);
  43. this.propertyGrid1.Refresh();
  44. }
  45. private void refreshUIState()
  46. {
  47. foreach (ToolStripItem item in this.toolStrip1.Items)
  48. {
  49. if (item.Text == "打开设备")
  50. item.Visible = (dev == null || !dev.IsInit);
  51. else
  52. this.propertyGrid1.Enabled = item.Enabled = !(dev == null || !dev.IsInit);
  53. }
  54. }
  55. public void init()
  56. {
  57. this.tbtnSave.Visible = tssSave.Visible = (GetParamsEvent != null);
  58. this.propertyGrid1.SelectedObject = prop;
  59. dev = new SmallAxisDev(true);
  60. dev.WarningEvent = (level, info) =>
  61. {
  62. txtLog.Text = $"({level}){info}";
  63. };
  64. //DATA
  65. dev.PPUChangeEvent += new System.Action<int, int>((prePPU, currPPU) =>
  66. {
  67. prop.ActualPPU = currPPU;
  68. this.refreshUI();
  69. });
  70. dev.StateChangeEvent += new System.Action<bool>((moving) =>
  71. {
  72. prop.Moving = moving;
  73. this.refreshUI();
  74. });
  75. if (!dev.start(prop.ComName, 9600))
  76. {
  77. this.closeDev();
  78. return;
  79. }
  80. prop.MaxPPU = dev.getMaxPPU();
  81. refreshUIState();
  82. }
  83. private void refreshUI()
  84. {
  85. SyncContext.Post(m =>
  86. {
  87. var result = m as string;
  88. propertyGrid1.Refresh();
  89. //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
  90. //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
  91. }, "异步操作完成结果");
  92. }
  93. private void closeDev()
  94. {
  95. try
  96. {
  97. if (dev == null)
  98. return;
  99. dev.stop();
  100. }
  101. catch (Exception ex)
  102. {
  103. refreshUIState();
  104. //MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  105. }
  106. }
  107. protected override void OnHandleDestroyed(EventArgs e)
  108. {
  109. base.OnHandleDestroyed(e);
  110. // 在此添加需要手动释放资源的代码
  111. this.closeDev();
  112. }
  113. private void tbtnRun_Click(object sender, EventArgs e)
  114. {
  115. try
  116. {
  117. dev.gotoPos((int)prop.CmdPos,false);
  118. //if (!res)
  119. // throw new Exception("设置失败!");
  120. }
  121. catch(Exception ex)
  122. {
  123. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  124. }
  125. }
  126. private void tbtnHome_Click(object sender, EventArgs e)
  127. {
  128. dev.home();
  129. }
  130. private void tbtnExport_Click(object sender, EventArgs e)
  131. {
  132. string filePath = FileUtil.saveAsFile($"SamllAxis config.json", "JSON|*.json");
  133. if (filePath != "")
  134. {
  135. string jsonText = prop.serialize();
  136. File.WriteAllText(filePath, jsonText);
  137. MessageBox.Show("保存成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  138. }
  139. }
  140. private void tbtnImport_Click(object sender, EventArgs e)
  141. {
  142. string filePath = FileUtil.selectFile("JSON|*.json");
  143. if (filePath != "")
  144. {
  145. string jsonText = File.ReadAllText(filePath);
  146. prop.deserialize(jsonText);
  147. this.propertyGrid1.Refresh();
  148. //this.propertyGrid1.SelectedObject= prop;
  149. //MessageBox.Show("加载成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  150. }
  151. }
  152. private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  153. {
  154. //其中包含了两个重要的属性:OldValue和ChangeItem。
  155. //ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。
  156. //而通过ChangeItem的Label,则可以查看到当前在属性编辑输入框中显示的值。
  157. string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name;
  158. string propertyName = e.ChangedItem.PropertyDescriptor.Name;
  159. var oldValue = e.OldValue;
  160. var newValue = e.ChangedItem.Value;
  161. switch (propertyName)
  162. {
  163. //case "ChannelIndex":
  164. // if((int)newValue <1 || (int)newValue >4)
  165. // {
  166. // prop.ChannelIndex = (int)oldValue;
  167. // this.refreshUI();
  168. // }
  169. // else
  170. // {
  171. // dev.getDigitalValue((int)newValue);
  172. // }
  173. // break;
  174. }
  175. }
  176. private void tbtnSave_Click(object sender, EventArgs e)
  177. {
  178. GetParamsEvent?.Invoke(prop.serialize());
  179. }
  180. private void tsbtnOpenDev_Click(object sender, EventArgs e)
  181. {
  182. this.init();
  183. }
  184. }
  185. }