版博士V2.0程序
Você não pode selecionar mais de 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.
 
 
 
 

204 linhas
6.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.Device.Scanner;
  19. using static ProductionControl.UI.UIAxis;
  20. namespace ProductionControl.UI
  21. {
  22. public partial class UIScanner : UserControl
  23. {
  24. SynchronizationContext SyncContext = null;
  25. public Action<int, string> log;
  26. //
  27. private ScannerProp prop;
  28. private Scanner dev;
  29. public UIScanner()
  30. {
  31. InitializeComponent();
  32. //获取UI线程同步上下文
  33. SyncContext = SynchronizationContext.Current;
  34. //init();
  35. }
  36. public void init(ScannerType devType)
  37. {
  38. try
  39. {
  40. prop = new ScannerProp(devType);
  41. this.propertyGrid1.SelectedObject = prop;
  42. dev = new Scanner(prop.DeviceType);
  43. dev.log = log;
  44. //DATA
  45. dev.ScanEvent += new System.Action<int, string>((num, path) => {
  46. prop.ImageList[num - 1] = path;
  47. this.refreshUI();
  48. });
  49. dev.open();
  50. dev.start(IntPtr.Zero);
  51. dev.getParam();
  52. prop.ExposureTime = dev.ExposureTime;
  53. prop.Gain= dev.Gain;
  54. prop.ResultingFrameRate = dev.ResultingFrameRate;
  55. this.toolStrip1.Enabled = true;
  56. this.propertyGrid1.Enabled = true;
  57. }
  58. catch (Exception ex)
  59. {
  60. this.toolStrip1.Enabled = false;
  61. this.propertyGrid1.Enabled = false;
  62. //MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  63. this.closeDev();
  64. }
  65. }
  66. private void refreshUI()
  67. {
  68. SyncContext.Post(m =>
  69. {
  70. var result = m as string;
  71. propertyGrid1.Refresh();
  72. //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
  73. //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
  74. }, "异步操作完成结果");
  75. }
  76. private void closeDev()
  77. {
  78. try
  79. {
  80. if (dev == null)
  81. return;
  82. dev.stop();
  83. dev.close();
  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.setParam(prop.ExposureTime,prop.Gain,prop.ResultingFrameRate);
  103. dev.getParam();
  104. prop.ExposureTime = dev.ExposureTime;
  105. prop.Gain = dev.Gain;
  106. prop.ResultingFrameRate = dev.ResultingFrameRate;
  107. }
  108. catch(Exception ex)
  109. {
  110. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  111. }
  112. }
  113. private void tbtnExport_Click(object sender, EventArgs e)
  114. {
  115. string filePath = FileUtil.saveAsFile($"Scanner_config.json", "JSON|*.json");
  116. if (filePath != "")
  117. {
  118. string jsonText = prop.serialize();
  119. File.WriteAllText(filePath, jsonText);
  120. MessageBox.Show("保存成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  121. }
  122. }
  123. private void tbtnImport_Click(object sender, EventArgs e)
  124. {
  125. string filePath = FileUtil.openFile("JSON|*.json");
  126. if (filePath != "")
  127. {
  128. string jsonText = File.ReadAllText(filePath);
  129. prop.deserialize(jsonText);
  130. this.propertyGrid1.Refresh();
  131. //this.propertyGrid1.SelectedObject= prop;
  132. //MessageBox.Show("加载成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  133. }
  134. }
  135. private void tbtnScan_Click(object sender, EventArgs e)
  136. {
  137. prop.ImageList = new string[dev.isContinuousMode ? 2 : 1];
  138. dev.scan(prop.ImageList.Length);
  139. }
  140. private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  141. {
  142. //其中包含了两个重要的属性:OldValue和ChangeItem。
  143. //ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。
  144. //而通过ChangeItem的Label,则可以查看到当前在属性编辑输入框中显示的值。
  145. string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name;
  146. string propertyName = e.ChangedItem.PropertyDescriptor.Name;
  147. var oldValue = e.OldValue;
  148. var newValue = e.ChangedItem.Value;
  149. switch (propertyName)
  150. {
  151. //case "ChannelIndex":
  152. // if((int)newValue <1 || (int)newValue >4)
  153. // {
  154. // prop.ChannelIndex = (int)oldValue;
  155. // this.refreshUI();
  156. // }
  157. // else
  158. // {
  159. // dev.getDigitalValue((int)newValue);
  160. // }
  161. // break;
  162. }
  163. }
  164. private void tbtnMode_Click(object sender, EventArgs e)
  165. {
  166. if (!dev.isContinuousMode)
  167. {
  168. dev.setMode(true);
  169. tbtnMode.Text = "软触发";
  170. }
  171. else
  172. {
  173. dev.setMode(false);
  174. tbtnMode.Text = "连续";
  175. }
  176. }
  177. FrmScannerShow frm = new FrmScannerShow();
  178. private void tbtnShow_Click(object sender, EventArgs e)
  179. {
  180. if(!frm.IsDisposed)
  181. frm.Close();
  182. frm = new FrmScannerShow();
  183. frm.Show();
  184. dev.setPreviewWin(frm.Handle);
  185. }
  186. }
  187. }