版博士V2.0程序
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

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