版博士V2.0程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

127 líneas
4.0 KiB

  1. using Advantech.Motion;
  2. using Newtonsoft.Json;
  3. using ProductionControl.Device;
  4. using ProductionControl.UI.PropExtend;
  5. using ProductionControl.Utils;
  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.Text;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using System.Windows.Forms;
  17. using System.Xml.Linq;
  18. using static ProductionControl.Device.Axis;
  19. using static ProductionControl.UI.UIAxis;
  20. namespace ProductionControl.UI
  21. {
  22. public partial class UICodeScanner : UserControl
  23. {
  24. SynchronizationContext SyncContext = null;
  25. public Action<int, string> log;
  26. //
  27. public class CodeScannerProp
  28. {
  29. [PropertyOrder(1), Browsable(true), Category("数据"), DisplayName("Code"), Description("Code"), ReadOnly(true), JsonIgnore]
  30. public string code { get; set; }
  31. }
  32. private CodeScannerProp prop = new CodeScannerProp();
  33. private CodeScanner dev;
  34. public UICodeScanner()
  35. {
  36. InitializeComponent();
  37. //获取UI线程同步上下文
  38. SyncContext = SynchronizationContext.Current;
  39. //init();
  40. }
  41. public void init()
  42. {
  43. try
  44. {
  45. this.propertyGrid1.SelectedObject = prop;
  46. dev = new CodeScanner();
  47. dev.log = log;
  48. dev.start();
  49. //IN
  50. dev.ScanerEvent += new System.Action<string>((data) => {
  51. prop.code = data;
  52. this.refreshUI();
  53. });
  54. //this.toolStrip1.Enabled = true;
  55. this.propertyGrid1.Enabled = true;
  56. }
  57. catch (Exception ex)
  58. {
  59. //this.toolStrip1.Enabled = false;
  60. this.propertyGrid1.Enabled = false;
  61. //MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  62. this.closeDev();
  63. }
  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. private void closeDev()
  76. {
  77. try
  78. {
  79. if (dev == null)
  80. return;
  81. dev.stop();
  82. }
  83. catch (Exception ex)
  84. {
  85. //this.toolStrip1.Enabled = false;
  86. this.propertyGrid1.Enabled = false;
  87. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  88. }
  89. }
  90. protected override void OnHandleDestroyed(EventArgs e)
  91. {
  92. base.OnHandleDestroyed(e);
  93. // 在此添加需要手动释放资源的代码
  94. this.closeDev();
  95. }
  96. private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  97. {
  98. //其中包含了两个重要的属性:OldValue和ChangeItem。
  99. //ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。
  100. //而通过ChangeItem的Label,则可以查看到当前在属性编辑输入框中显示的值。
  101. string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name;
  102. string propertyName = e.ChangedItem.PropertyDescriptor.Name;
  103. var oldValue = e.OldValue;
  104. var newValue = e.ChangedItem.Value;
  105. switch (propertyName)
  106. {
  107. case "xxx":
  108. //refreshAxisVelParam();
  109. break;
  110. }
  111. }
  112. }
  113. }