版博士V2.0程序
Não pode escolher mais do que 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.
 
 
 
 

139 linhas
4.3 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. namespace ProductionControl.UI
  19. {
  20. public partial class UICodeScannerDev : UserControl
  21. {
  22. SynchronizationContext SyncContext = null;
  23. //
  24. public class CodeScannerProp
  25. {
  26. [PropertyOrder(1), Browsable(true), Category("数据"), DisplayName("Code"), Description("Code"), ReadOnly(true), JsonIgnore]
  27. public string code { get; set; }
  28. public string serialize()
  29. {
  30. return JsonConvert.SerializeObject(this);
  31. }
  32. }
  33. private CodeScannerProp prop = new CodeScannerProp();
  34. private CodeScannerDev dev;
  35. public UICodeScannerDev()
  36. {
  37. InitializeComponent();
  38. propertyGrid1.PropertyValueChanged += propertyGrid1_PropertyValueChanged;
  39. //this.toolStrip1.Enabled = false;
  40. this.propertyGrid1.Enabled = false;
  41. //获取UI线程同步上下文
  42. SyncContext = SynchronizationContext.Current;
  43. init();
  44. }
  45. //public string getParamsData()
  46. //{
  47. // return prop.serialize();
  48. //}
  49. //public void setParamsData(string json)
  50. //{
  51. //if (json == "") return;
  52. // prop.deserialize(json);
  53. // this.propertyGrid1.Refresh();
  54. //}
  55. public void init()
  56. {
  57. this.propertyGrid1.SelectedObject = prop;
  58. dev = new CodeScannerDev();
  59. dev.WarningEvent = (level, info) =>
  60. {
  61. txtLog.Text = $"({level}){info}";
  62. };
  63. //IN
  64. dev.ScanerEvent += new System.Action<string>((data) =>
  65. {
  66. prop.code = data;
  67. this.refreshUI();
  68. });
  69. if (!dev.start())
  70. {
  71. this.closeDev();
  72. return;
  73. }
  74. //this.toolStrip1.Enabled = true;
  75. this.propertyGrid1.Enabled = true;
  76. }
  77. private void refreshUI()
  78. {
  79. SyncContext.Post(m =>
  80. {
  81. var result = m as string;
  82. propertyGrid1.Refresh();
  83. //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
  84. //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
  85. }, "异步操作完成结果");
  86. }
  87. private void closeDev()
  88. {
  89. try
  90. {
  91. if (dev == null)
  92. return;
  93. dev.stop();
  94. }
  95. catch (Exception ex)
  96. {
  97. //this.toolStrip1.Enabled = false;
  98. this.propertyGrid1.Enabled = false;
  99. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  100. }
  101. }
  102. protected override void OnHandleDestroyed(EventArgs e)
  103. {
  104. base.OnHandleDestroyed(e);
  105. // 在此添加需要手动释放资源的代码
  106. this.closeDev();
  107. }
  108. private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  109. {
  110. //其中包含了两个重要的属性:OldValue和ChangeItem。
  111. //ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。
  112. //而通过ChangeItem的Label,则可以查看到当前在属性编辑输入框中显示的值。
  113. string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name;
  114. string propertyName = e.ChangedItem.PropertyDescriptor.Name;
  115. var oldValue = e.OldValue;
  116. var newValue = e.ChangedItem.Value;
  117. switch (propertyName)
  118. {
  119. case "xxx":
  120. //refreshAxisVelParam();
  121. break;
  122. }
  123. }
  124. }
  125. }