版博士V2.0程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

292 wiersze
9.7 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.Drawing.Imaging;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using System.Web.UI.WebControls;
  17. using System.Windows.Forms;
  18. using System.Xml.Linq;
  19. using static ProductionControl.Device.ScannerDev;
  20. namespace ProductionControl.UI
  21. {
  22. public partial class UIScannerDev : UserControl
  23. {
  24. SynchronizationContext SyncContext = null;
  25. public Action<string> GetParamsEvent;
  26. public ScannerType devType { get; set; }
  27. private List<string> pathList = new List<string>();
  28. //
  29. private ScannerDevProp prop;
  30. private ScannerDev dev;
  31. public UIScannerDev()
  32. {
  33. InitializeComponent();
  34. propertyGrid1.PropertyValueChanged += propertyGrid1_PropertyValueChanged;
  35. refreshUIState();
  36. //获取UI线程同步上下文
  37. SyncContext = SynchronizationContext.Current;
  38. //init();
  39. }
  40. public string getParamsData()
  41. {
  42. return prop.serialize();
  43. }
  44. public void setParamsData(string json)
  45. {
  46. if (json == "") return;
  47. prop.deserialize(json);
  48. this.propertyGrid1.Refresh();
  49. }
  50. private void refreshUIState()
  51. {
  52. foreach (ToolStripItem item in this.toolStrip1.Items)
  53. {
  54. if (item.Text == "打开设备")
  55. item.Visible = (dev == null || !dev.IsInit);
  56. else
  57. this.propertyGrid1.Enabled = item.Enabled = !(dev == null || !dev.IsInit);
  58. }
  59. }
  60. public void init(ScannerType devType)
  61. {
  62. pathList.Clear();
  63. this.tbtnSave.Visible = tssSave.Visible = (GetParamsEvent != null);
  64. prop = new ScannerDevProp(devType);
  65. this.propertyGrid1.SelectedObject = prop;
  66. dev = new ScannerDev(prop.DeviceType);
  67. dev.WarningEvent = (level, info) =>
  68. {
  69. txtLog.Text = $"({level}){info}";
  70. };
  71. //DATA
  72. dev.ScanEventPath += new System.Action<int, string>((num, path) =>
  73. {
  74. //string path = Config.Scnaner_Bmp_SavePath + "\\" + DateTime.Now.Ticks + ".bmp";
  75. //bmp.Save(path, ImageFormat.Bmp);
  76. //MemoryStream ms = new MemoryStream();
  77. //bmp.Save(ms, ImageFormat.Bmp);
  78. //byte[] bs = ms.ToArray(); //GetBuffer();
  79. //ms.Close();
  80. //ms.Dispose();
  81. pathList.Add(path);
  82. prop.ImageList=pathList.ToArray();
  83. this.refreshUI();
  84. if (!dev.isContinuousMode)
  85. updateViewWin(read2Bmp(path));
  86. });
  87. dev.ScanEvent += new System.Action<int, Bitmap>((num, bmp) =>
  88. {
  89. string path;
  90. if(prop.DeviceType== ScannerType.GENTL)
  91. path = Config.appBasePath + "\\temp\\" + DateTime.Now.Ticks + ".bmp";
  92. else
  93. path = Config.appBasePath + "\\temp\\" + DateTime.Now.Ticks + ".bmp";
  94. bmp.Save(path, ImageFormat.Bmp);
  95. //MemoryStream ms = new MemoryStream();
  96. //bmp.Save(ms, ImageFormat.Bmp);
  97. //byte[] bs = ms.ToArray(); //GetBuffer();
  98. //ms.Close();
  99. //ms.Dispose();
  100. pathList.Add(path);
  101. prop.ImageList = pathList.ToArray();
  102. this.refreshUI();
  103. if (!dev.isContinuousMode)
  104. updateViewWin(bmp);
  105. });
  106. if (!dev.open())
  107. {
  108. this.closeDev();
  109. return;
  110. }
  111. if (!dev.start(IntPtr.Zero, Config.appBasePath + "\\temp\\"))
  112. {
  113. this.closeDev();
  114. return;
  115. }
  116. dev.getParam();
  117. prop.ExposureTime = dev.ExposureTime;
  118. prop.Gain = dev.Gain;
  119. prop.ResultingFrameRate = dev.ResultingFrameRate;
  120. refreshUIState();
  121. }
  122. private void updateViewWin(Bitmap bmp)
  123. {
  124. if(frm!=null && API.IsWindow(frm.Handle))
  125. frm.updateBmp(bmp);
  126. }
  127. private Bitmap read2Bmp(string path)
  128. {
  129. MemoryStream ms = new System.IO.MemoryStream(File.ReadAllBytes(path));
  130. Bitmap bmp = new Bitmap(ms);
  131. ms.Close();
  132. ms.Dispose();
  133. return bmp;
  134. }
  135. private void refreshUI()
  136. {
  137. SyncContext.Post(m =>
  138. {
  139. var result = m as string;
  140. propertyGrid1.Refresh();
  141. //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
  142. //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
  143. }, "异步操作完成结果");
  144. }
  145. private void closeDev()
  146. {
  147. try
  148. {
  149. if (dev == null)
  150. return;
  151. dev.stop();
  152. dev.close();
  153. }
  154. catch (Exception ex)
  155. {
  156. refreshUIState();
  157. //MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  158. }
  159. }
  160. protected override void OnHandleDestroyed(EventArgs e)
  161. {
  162. base.OnHandleDestroyed(e);
  163. // 在此添加需要手动释放资源的代码
  164. this.closeDev();
  165. }
  166. private void tbtnRun_Click(object sender, EventArgs e)
  167. {
  168. try
  169. {
  170. dev.setParam(prop.ExposureTime,prop.Gain,prop.ResultingFrameRate);
  171. dev.getParam();
  172. prop.ExposureTime = dev.ExposureTime;
  173. prop.Gain = dev.Gain;
  174. prop.ResultingFrameRate = dev.ResultingFrameRate;
  175. }
  176. catch(Exception ex)
  177. {
  178. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  179. }
  180. }
  181. private void tbtnExport_Click(object sender, EventArgs e)
  182. {
  183. string filePath = FileUtil.saveAsFile($"Scanner_config.json", "JSON|*.json");
  184. if (filePath != "")
  185. {
  186. string jsonText = prop.serialize();
  187. File.WriteAllText(filePath, jsonText);
  188. MessageBox.Show("保存成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  189. }
  190. }
  191. private void tbtnImport_Click(object sender, EventArgs e)
  192. {
  193. string filePath = FileUtil.selectFile("JSON|*.json");
  194. if (filePath != "")
  195. {
  196. string jsonText = File.ReadAllText(filePath);
  197. prop.deserialize(jsonText);
  198. this.propertyGrid1.Refresh();
  199. //this.propertyGrid1.SelectedObject= prop;
  200. //MessageBox.Show("加载成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  201. }
  202. }
  203. private void tbtnScan_Click(object sender, EventArgs e)
  204. {
  205. prop.ImageList = new string[dev.isContinuousMode ? 2 : 1];
  206. dev.scan(prop.ImageList.Length);
  207. }
  208. private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  209. {
  210. //其中包含了两个重要的属性:OldValue和ChangeItem。
  211. //ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。
  212. //而通过ChangeItem的Label,则可以查看到当前在属性编辑输入框中显示的值。
  213. string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name;
  214. string propertyName = e.ChangedItem.PropertyDescriptor.Name;
  215. var oldValue = e.OldValue;
  216. var newValue = e.ChangedItem.Value;
  217. switch (propertyName)
  218. {
  219. //case "ChannelIndex":
  220. // if((int)newValue <1 || (int)newValue >4)
  221. // {
  222. // prop.ChannelIndex = (int)oldValue;
  223. // this.refreshUI();
  224. // }
  225. // else
  226. // {
  227. // dev.getDigitalValue((int)newValue);
  228. // }
  229. // break;
  230. }
  231. }
  232. private void tbtnMode_Click(object sender, EventArgs e)
  233. {
  234. if (!dev.isContinuousMode)
  235. {
  236. dev.setMode(true);
  237. tbtnMode.Text = "软触发";
  238. updateViewWin(null);
  239. dev.setPreviewWin(frm.picHwnd);
  240. }
  241. else
  242. {
  243. dev.setMode(false);
  244. tbtnMode.Text = "连续";
  245. dev.setPreviewWin(IntPtr.Zero);
  246. }
  247. }
  248. FrmScannerShow frm;
  249. private void tbtnShow_Click(object sender, EventArgs e)
  250. {
  251. if(frm!=null && !frm.IsDisposed)
  252. frm.Close();
  253. frm = new FrmScannerShow(dev.size);
  254. frm.Show();
  255. if(dev.isContinuousMode)
  256. dev.setPreviewWin(frm.picHwnd);
  257. }
  258. private void tbtnSave_Click(object sender, EventArgs e)
  259. {
  260. GetParamsEvent?.Invoke(prop.serialize());
  261. }
  262. private void tsbtnOpenDev_Click(object sender, EventArgs e)
  263. {
  264. init(devType);
  265. }
  266. }
  267. }