版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 2 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. using static ProductionControl.Device.Axis;
  18. using static ProductionControl.UI.UIAxis;
  19. namespace ProductionControl.UI
  20. {
  21. public partial class UISmallAxis : UserControl
  22. {
  23. SynchronizationContext SyncContext = null;
  24. public Action<int, string> log;
  25. //
  26. private SmallAxisProp prop = new SmallAxisProp(Config.SmallAxis_ComName);
  27. private SmallAxis dev;
  28. public UISmallAxis()
  29. {
  30. InitializeComponent();
  31. //获取UI线程同步上下文
  32. SyncContext = SynchronizationContext.Current;
  33. //init();
  34. }
  35. public void init()
  36. {
  37. try
  38. {
  39. this.propertyGrid1.SelectedObject = prop;
  40. dev = new SmallAxis(prop.ComName,9600);
  41. dev.log = log;
  42. //DATA
  43. dev.PPUChangeEvent += new System.Action<int, int>((prePPU, currPPU) =>
  44. {
  45. prop.ActualPPU = currPPU;
  46. this.refreshUI();
  47. });
  48. dev.StateChangeEvent += new System.Action<bool>((moving) =>
  49. {
  50. prop.Moving= moving;
  51. this.refreshUI();
  52. });
  53. dev.start();
  54. prop.MaxPPU= dev.getMaxPPU();
  55. this.toolStrip1.Enabled = true;
  56. this.propertyGrid1.Enabled = true;
  57. }
  58. catch (Exception ex)
  59. {
  60. this.toolStrip1.Enabled = false;
  61. this.propertyGrid1.Enabled = false;
  62. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  63. this.closeDev();
  64. }
  65. }
  66. private void refreshUI()
  67. {
  68. SyncContext.Post(m =>
  69. {
  70. var result = m as string;
  71. propertyGrid1.Refresh();
  72. //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
  73. //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
  74. }, "异步操作完成结果");
  75. }
  76. private void closeDev()
  77. {
  78. try
  79. {
  80. if (dev == null)
  81. return;
  82. dev.stop();
  83. }
  84. catch (Exception ex)
  85. {
  86. this.toolStrip1.Enabled = false;
  87. this.propertyGrid1.Enabled = false;
  88. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  89. }
  90. }
  91. protected override void OnHandleDestroyed(EventArgs e)
  92. {
  93. base.OnHandleDestroyed(e);
  94. // 在此添加需要手动释放资源的代码
  95. this.closeDev();
  96. }
  97. private void tbtnRun_Click(object sender, EventArgs e)
  98. {
  99. try
  100. {
  101. dev.gotoPos(prop.CmdPos,false);
  102. //if (!res)
  103. // throw new Exception("设置失败!");
  104. }
  105. catch(Exception ex)
  106. {
  107. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  108. }
  109. }
  110. private void tbtnHome_Click(object sender, EventArgs e)
  111. {
  112. dev.home();
  113. }
  114. private void tbtnExport_Click(object sender, EventArgs e)
  115. {
  116. string filePath = FileUtil.saveAsFile($"SamllAxis config.json", "JSON|*.json");
  117. if (filePath != "")
  118. {
  119. string jsonText = prop.serialize();
  120. File.WriteAllText(filePath, jsonText);
  121. MessageBox.Show("保存成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  122. }
  123. }
  124. private void tbtnImport_Click(object sender, EventArgs e)
  125. {
  126. string filePath = FileUtil.openFile("JSON|*.json");
  127. if (filePath != "")
  128. {
  129. string jsonText = File.ReadAllText(filePath);
  130. prop.deserialize(jsonText);
  131. this.propertyGrid1.Refresh();
  132. //this.propertyGrid1.SelectedObject= prop;
  133. //MessageBox.Show("加载成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  134. }
  135. }
  136. private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  137. {
  138. //其中包含了两个重要的属性:OldValue和ChangeItem。
  139. //ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。
  140. //而通过ChangeItem的Label,则可以查看到当前在属性编辑输入框中显示的值。
  141. string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name;
  142. string propertyName = e.ChangedItem.PropertyDescriptor.Name;
  143. var oldValue = e.OldValue;
  144. var newValue = e.ChangedItem.Value;
  145. switch (propertyName)
  146. {
  147. //case "ChannelIndex":
  148. // if((int)newValue <1 || (int)newValue >4)
  149. // {
  150. // prop.ChannelIndex = (int)oldValue;
  151. // this.refreshUI();
  152. // }
  153. // else
  154. // {
  155. // dev.getDigitalValue((int)newValue);
  156. // }
  157. // break;
  158. }
  159. }
  160. }
  161. }