版博士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.
 
 
 
 

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