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

183 line
6.3 KiB

  1. using MaiMuAOI.SysCtrl;
  2. using MaiMuAOI.SysUI.DefectPicShow;
  3. using MaiMuAOI.SysUI.SysSet;
  4. using MaiMuControl.Device.LightDev;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Data;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Security.Cryptography;
  14. using System.Text;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. using System.Windows.Forms;
  18. using System.Xml.Linq;
  19. namespace ProductionControl.UI
  20. {
  21. public partial class UILightDev : UserControl
  22. {
  23. SynchronizationContext SyncContext = null;
  24. public Action<string> GetParamsEvent;
  25. //
  26. private LightDevProp prop = new LightDevProp(int.Parse(ConfMgr.Instance.SysConfigParams.LightCom.Remove(0,3)));
  27. private LightDev _dev;
  28. public UILightDev(LightDev dev)
  29. {
  30. _dev = dev;
  31. InitializeComponent();
  32. propertyGrid1.PropertyValueChanged += propertyGrid1_PropertyValueChanged;
  33. refreshUIState();
  34. //获取UI线程同步上下文
  35. SyncContext = SynchronizationContext.Current;
  36. //init();
  37. }
  38. public string getParamsData()
  39. {
  40. return prop.serialize();
  41. }
  42. public void setParamsData(string json)
  43. {
  44. if (json == "") return;
  45. prop.deserialize(json);
  46. this.propertyGrid1.Refresh();
  47. }
  48. private void refreshUIState()
  49. {
  50. foreach (ToolStripItem item in this.toolStrip1.Items)
  51. {
  52. if (item.Text == "打开设备")
  53. item.Visible = (_dev == null);
  54. else
  55. this.propertyGrid1.Enabled = item.Enabled = !(_dev == null );
  56. }
  57. }
  58. public void init()
  59. {
  60. this.tbtnSave.Visible = tssSave.Visible = (GetParamsEvent != null);
  61. txtLog.Text = "";
  62. this.propertyGrid1.SelectedObject = prop;
  63. refreshUIState();
  64. }
  65. private void refreshUI()
  66. {
  67. SyncContext.Post(m =>
  68. {
  69. var result = m as string;
  70. propertyGrid1.Refresh();
  71. //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
  72. //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
  73. }, "异步操作完成结果");
  74. }
  75. protected override void OnHandleDestroyed(EventArgs e)
  76. {
  77. base.OnHandleDestroyed(e);
  78. // 在此添加需要手动释放资源的代码
  79. if(frm != null)
  80. frm.Close();
  81. }
  82. private void tbtnExport_Click(object sender, EventArgs e)
  83. {
  84. string filePath = ConfMgr.SaveAsFile($"Light配置.json", "JSON|*.json");
  85. if (filePath != "")
  86. {
  87. string jsonText = prop.serialize();
  88. File.WriteAllText(filePath, jsonText);
  89. MessageBox.Show("保存成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  90. }
  91. }
  92. private void tbtnImport_Click(object sender, EventArgs e)
  93. {
  94. string filePath = ConfMgr.SelectFile("JSON|*.json");
  95. if (filePath != "")
  96. {
  97. string jsonText = File.ReadAllText(filePath);
  98. prop.deserialize(jsonText);
  99. this.propertyGrid1.Refresh();
  100. //this.propertyGrid1.SelectedObject= prop;
  101. //MessageBox.Show("加载成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  102. }
  103. }
  104. private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  105. {
  106. //其中包含了两个重要的属性:OldValue和ChangeItem。
  107. //ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。
  108. //而通过ChangeItem的Label,则可以查看到当前在属性编辑输入框中显示的值。
  109. string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name;
  110. string propertyName = e.ChangedItem.PropertyDescriptor.Name;
  111. var oldValue = e.OldValue;
  112. var newValue = e.ChangedItem.Value;
  113. switch (propertyName)
  114. {
  115. case "ChannelIndex":
  116. if((int)newValue <1 || (int)newValue >6)
  117. {
  118. prop.ChannelIndex = (int)oldValue;
  119. this.refreshUI();
  120. }
  121. else
  122. {
  123. //dev.getDigitalValue((int)newValue);
  124. }
  125. break;
  126. case "DigitalValue":
  127. if ((int)newValue < 0 || (int)newValue > 255)
  128. {
  129. prop.DigitalValue = (int)oldValue;
  130. this.refreshUI();
  131. }
  132. break;
  133. }
  134. }
  135. private void tbtnSave_Click(object sender, EventArgs e)
  136. {
  137. GetParamsEvent?.Invoke(prop.serialize());
  138. }
  139. private void tsbtnOpenDev_Click(object sender, EventArgs e)
  140. {
  141. this.init();
  142. }
  143. DebugTestFrm frm;
  144. private void tsbDebug_Click(object sender, EventArgs e)
  145. {
  146. if ((frm == null) || (frm.IsDisposed))
  147. frm = new DebugTestFrm();
  148. frm.TopMost = true;
  149. frm.Show();
  150. }
  151. private void tbtnRun_Click(object sender, EventArgs e)
  152. {
  153. try
  154. {
  155. var res = SysMgr.Instance.LightDev.SetLightDigitalValue(prop.ChannelIndex, prop.DigitalValue);
  156. if (res < 0)
  157. throw new Exception("设置失败!");
  158. }
  159. catch (Exception ex)
  160. {
  161. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  162. }
  163. }
  164. }
  165. }