版博士V2.0程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

303 wiersze
11 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. using static ProductionControl.Device.Axis;
  18. using static ProductionControl.UI.UIAxis;
  19. namespace ProductionControl.UI
  20. {
  21. public partial class UIAxis : UserControl
  22. {
  23. SynchronizationContext SyncContext = null;
  24. public enum AxisName
  25. {
  26. [Description("0-Axis")]
  27. Axis0 = 0,
  28. [Description("1-Axis")]
  29. Axis1 = 1,
  30. [Description("2-Axis")]
  31. Axis2 = 2,
  32. [Description("3-Axis")]
  33. Axis3 = 3,
  34. }
  35. public enum AxisType
  36. {
  37. [Description("默认")]
  38. 默认 = 0,
  39. [Description("直线电机")]
  40. 直线电机 = 1,
  41. }
  42. public Action<int, string> log;
  43. //
  44. private AxisProp axisProp = new AxisProp();
  45. private Axis axis;
  46. public UIAxis()
  47. {
  48. InitializeComponent();
  49. //获取UI线程同步上下文
  50. SyncContext = SynchronizationContext.Current;
  51. //init();
  52. }
  53. public void init()
  54. {
  55. try
  56. {
  57. this.propertyGrid1.SelectedObject = axisProp;
  58. axis = new Axis();
  59. axis.log = log;
  60. axis.start();
  61. //STATE
  62. axis.axisStateEvent += new System.Action<int, AxisStateType, uint>((axisIndex, type, stateValue) => {
  63. if (axisIndex != (int)axisProp.AxisIndex)
  64. return;
  65. switch (type)
  66. {
  67. case AxisStateType.AxisState:
  68. if (axisProp.AxState != (AxisState)stateValue)
  69. {
  70. axisProp.AxState = (AxisState)stateValue;
  71. this.refreshUI();
  72. }
  73. break;
  74. case AxisStateType.AxisIOState:
  75. if (axisProp.AxIOStatus != stateValue)
  76. {
  77. axisProp.AxIOStatus = stateValue;
  78. axisProp.ALM = (stateValue & (uint)Ax_Motion_IO.AX_MOTION_IO_ALM) > 0;//需IO close
  79. if (axisProp.ALM) log?.Invoke(2, $"Axis{axisIndex} ALM!!!");
  80. axisProp.EMG = (stateValue & (uint)Ax_Motion_IO.AX_MOTION_IO_EMG) > 0;//需重置 state
  81. if (axisProp.EMG) log?.Invoke(2, $"Axis{axisIndex} EMG!!!");
  82. axisProp.LMTP = (stateValue & (uint)Ax_Motion_IO.AX_MOTION_IO_LMTP) > 0;//右极限 true->false
  83. axisProp.LMTN = (stateValue & (uint)Ax_Motion_IO.AX_MOTION_IO_LMTN) > 0;//左极限
  84. axisProp.ORG = (stateValue & (uint)Ax_Motion_IO.AX_MOTION_IO_ORG) > 0;
  85. axisProp.SVON = (stateValue & (uint)Ax_Motion_IO.AX_MOTION_IO_SVON) > 0;
  86. this.refreshUI();
  87. }
  88. break;
  89. case AxisStateType.AxisMotionState:
  90. if (axisProp.AxMotionState != stateValue)
  91. {
  92. axisProp.AxMotionState = stateValue;
  93. this.refreshUI();
  94. }
  95. break;
  96. }
  97. });
  98. //位置POS
  99. axis.axisPosEvent += new System.Action<int, AxisPosType, double>((axisIndex, type, pos) => {
  100. if (axisIndex != (int)axisProp.AxisIndex)
  101. return;
  102. switch (type)
  103. {
  104. case AxisPosType.CmdPos:
  105. if (axisProp.CmdPos != pos)
  106. {
  107. axisProp.CmdPos = pos;
  108. this.refreshUI();
  109. }
  110. break;
  111. case AxisPosType.ActualPos:
  112. if (axisProp.ActualPos != pos)
  113. {
  114. axisProp.ActualPos = pos;
  115. this.refreshUI();
  116. }
  117. break;
  118. }
  119. });
  120. refreshAxisVelParam();
  121. this.toolStrip1.Enabled = true;
  122. this.propertyGrid1.Enabled = true;
  123. }
  124. catch (Exception ex)
  125. {
  126. this.toolStrip1.Enabled = false;
  127. this.propertyGrid1.Enabled = false;
  128. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  129. this.closeDev();
  130. }
  131. }
  132. private void refreshUI()
  133. {
  134. SyncContext.Post(m =>
  135. {
  136. var result = m as string;
  137. propertyGrid1.Refresh();
  138. tbtnJogOnOff.Text = (axisProp.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
  139. tbtnLeft.Enabled = tbtnRight.Enabled = (axisProp.AxState == AxisState.STA_AX_EXT_JOG);
  140. }, "异步操作完成结果");
  141. }
  142. private void refreshAxisVelParam()
  143. {
  144. var values = axis.getAxisVelParam((int)axisProp.AxisIndex);
  145. axisProp.VelLow = values[0];
  146. axisProp.VelHigh = values[1];
  147. axisProp.Acc = values[2];
  148. axisProp.Dec = values[3];
  149. //axisProp.VelLow = values[4];
  150. }
  151. private void closeDev()
  152. {
  153. try
  154. {
  155. if (axis == null)
  156. return;
  157. axis.stop();
  158. }
  159. catch (Exception ex)
  160. {
  161. this.toolStrip1.Enabled = false;
  162. this.propertyGrid1.Enabled = false;
  163. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  164. }
  165. }
  166. protected override void OnHandleDestroyed(EventArgs e)
  167. {
  168. base.OnHandleDestroyed(e);
  169. // 在此添加需要手动释放资源的代码
  170. this.closeDev();
  171. }
  172. private void tbtnRun_Click(object sender, EventArgs e)
  173. {
  174. try
  175. {
  176. axis.setAxisVelParam(axisProp.VelLow, axisProp.VelHigh, axisProp.Acc, axisProp.Dec, (int)axisProp.AxisIndex);
  177. axis.move_ptp((int)axisProp.AxisIndex, axisProp.Value, axisProp.IsAbsPos);
  178. //running...
  179. }
  180. catch(Exception ex)
  181. {
  182. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  183. }
  184. }
  185. private void tbtnExport_Click(object sender, EventArgs e)
  186. {
  187. string filePath = FileUtil.saveAsFile($"Axis{(int)axisProp.AxisIndex}配置.json", "JSON|*.json");
  188. if (filePath != "")
  189. {
  190. string jsonText = axisProp.serialize();
  191. File.WriteAllText(filePath, jsonText);
  192. MessageBox.Show("保存成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  193. }
  194. }
  195. private void tbtnImport_Click(object sender, EventArgs e)
  196. {
  197. string filePath = FileUtil.openFile("JSON|*.json");
  198. if (filePath != "")
  199. {
  200. string jsonText = File.ReadAllText(filePath);
  201. axisProp.deserialize(jsonText);
  202. this.propertyGrid1.Refresh();
  203. //this.propertyGrid1.SelectedObject= axisProp;
  204. //MessageBox.Show("加载成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  205. }
  206. }
  207. private void tbtnHome_Click(object sender, EventArgs e)
  208. {
  209. if (axisProp.DeviceType == AxisType.默认)
  210. {
  211. axis.home((int)axisProp.AxisIndex);
  212. }
  213. else //直线电机
  214. {
  215. axis.move_ptp((int)axisProp.AxisIndex, - axisProp.ActualPos, false);
  216. }
  217. }
  218. private void tbtnLeft_MouseDown(object sender, MouseEventArgs e)
  219. {
  220. axis.jog((int)axisProp.AxisIndex, 0);
  221. }
  222. private void tbtnLeft_MouseUp(object sender, MouseEventArgs e)
  223. {
  224. axis.stopDec();
  225. }
  226. private void tbtnRight_MouseDown(object sender, MouseEventArgs e)
  227. {
  228. axis.jog((int)axisProp.AxisIndex, 1);
  229. }
  230. private void tbtnRight_MouseUp(object sender, MouseEventArgs e)
  231. {
  232. axis.stopDec();
  233. }
  234. private void tbtnJogOnOff_Click(object sender, EventArgs e)
  235. {
  236. if(axisProp.AxState==AxisState.STA_AX_EXT_JOG)
  237. {
  238. axis.closeJogMode((int)axisProp.AxisIndex);
  239. //tbtnJogOnOff.Text = "开启Jog";
  240. }
  241. else
  242. {
  243. axis.openJogMode((int)axisProp.AxisIndex);
  244. //tbtnJogOnOff.Text = "关闭Jog";
  245. }
  246. }
  247. private void tbtnStop_Click(object sender, EventArgs e)
  248. {
  249. axis.stopDec((int)axisProp.AxisIndex);
  250. }
  251. private void tbtnRestState_Click(object sender, EventArgs e)
  252. {
  253. axis.resetAxisState((int)axisProp.AxisIndex);
  254. }
  255. private void tbtnResetPos_Click(object sender, EventArgs e)
  256. {
  257. axis.resetCmdPosition((int)axisProp.AxisIndex);
  258. axis.resetActualPosition((int)axisProp.AxisIndex);
  259. }
  260. private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  261. {
  262. //其中包含了两个重要的属性:OldValue和ChangeItem。
  263. //ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。
  264. //而通过ChangeItem的Label,则可以查看到当前在属性编辑输入框中显示的值。
  265. string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name;
  266. string propertyName = e.ChangedItem.PropertyDescriptor.Name;
  267. var oldValue = e.OldValue;
  268. var newValue = e.ChangedItem.Value;
  269. switch (propertyName)
  270. {
  271. case "AxisIndex":
  272. refreshAxisVelParam();
  273. break;
  274. }
  275. }
  276. }
  277. }