版博士V2.0程序
Não pode escolher mais do que 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.
 
 
 
 

165 linhas
5.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. using static ProductionControl.Device.Axis;
  18. using static ProductionControl.UI.UIAxis;
  19. namespace ProductionControl.UI
  20. {
  21. public partial class UIIOCard : UserControl
  22. {
  23. SynchronizationContext SyncContext = null;
  24. public Action<int, string> log;
  25. //
  26. private IOCardProp prop = new IOCardProp(Config.IOCard_DeviceNum);
  27. private IOCard dev;
  28. public UIIOCard()
  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 IOCard();
  41. dev.log = log;
  42. dev.start(prop.DeviceNum);
  43. prop.IN = new byte[dev.DIPortCount];
  44. prop.OUT = new byte[dev.DOPortCount];
  45. prop.OUT = dev.DOData;
  46. //IN
  47. dev.INEvent += new System.Action<int, byte>((portIndex, data) => {
  48. prop.IN[portIndex] = data;
  49. this.refreshUI();
  50. });
  51. //OUT
  52. dev.OUTEvent += new System.Action<int, byte>((portIndex, data) => {
  53. prop.OUT[portIndex] = data;
  54. this.refreshUI();
  55. });
  56. this.toolStrip1.Enabled = true;
  57. this.propertyGrid1.Enabled = true;
  58. }
  59. catch (Exception ex)
  60. {
  61. this.toolStrip1.Enabled = false;
  62. this.propertyGrid1.Enabled = false;
  63. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  64. this.closeDev();
  65. }
  66. }
  67. private void refreshUI()
  68. {
  69. SyncContext.Post(m =>
  70. {
  71. var result = m as string;
  72. propertyGrid1.Refresh();
  73. //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
  74. //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
  75. }, "异步操作完成结果");
  76. }
  77. private void closeDev()
  78. {
  79. try
  80. {
  81. if (dev == null)
  82. return;
  83. dev.stop();
  84. }
  85. catch (Exception ex)
  86. {
  87. this.toolStrip1.Enabled = false;
  88. this.propertyGrid1.Enabled = false;
  89. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  90. }
  91. }
  92. protected override void OnHandleDestroyed(EventArgs e)
  93. {
  94. base.OnHandleDestroyed(e);
  95. // 在此添加需要手动释放资源的代码
  96. this.closeDev();
  97. }
  98. private void tbtnRun_Click(object sender, EventArgs e)
  99. {
  100. try
  101. {
  102. dev.writeData(0, prop.OUT);
  103. //running...
  104. }
  105. catch(Exception ex)
  106. {
  107. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  108. }
  109. }
  110. private void tbtnExport_Click(object sender, EventArgs e)
  111. {
  112. string filePath = FileUtil.saveAsFile($"IOCard配置.json", "JSON|*.json");
  113. if (filePath != "")
  114. {
  115. string jsonText = prop.serialize();
  116. File.WriteAllText(filePath, jsonText);
  117. MessageBox.Show("保存成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  118. }
  119. }
  120. private void tbtnImport_Click(object sender, EventArgs e)
  121. {
  122. string filePath = FileUtil.openFile("JSON|*.json");
  123. if (filePath != "")
  124. {
  125. string jsonText = File.ReadAllText(filePath);
  126. prop.deserialize(jsonText);
  127. this.propertyGrid1.Refresh();
  128. //this.propertyGrid1.SelectedObject= prop;
  129. //MessageBox.Show("加载成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  130. }
  131. }
  132. private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  133. {
  134. //其中包含了两个重要的属性:OldValue和ChangeItem。
  135. //ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。
  136. //而通过ChangeItem的Label,则可以查看到当前在属性编辑输入框中显示的值。
  137. string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name;
  138. string propertyName = e.ChangedItem.PropertyDescriptor.Name;
  139. var oldValue = e.OldValue;
  140. var newValue = e.ChangedItem.Value;
  141. switch (propertyName)
  142. {
  143. case "AxisIndex"://prop.propName
  144. //refreshAxisVelParam();
  145. break;
  146. }
  147. }
  148. }
  149. }