PlcOptionSelection.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using PlcCom;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Diagnostics;
  6. using System.Drawing.Design;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using PlcUiControl;
  12. using PlcUiForm;
  13. using Sunny.UI;
  14. namespace PlcComponent
  15. {
  16. public partial class PlcOptionSelection : Component, ISingleOutputComponent
  17. {
  18. public PlcOptionSelection()
  19. {
  20. InitializeComponent();
  21. }
  22. public PlcOptionSelection(IContainer container)
  23. {
  24. container.Add(this);
  25. InitializeComponent();
  26. }
  27. [Category("PLC")]
  28. [Description("PLC读地址,必须为Bool类型")]
  29. [Editor(typeof(NodeTypeEditor), typeof(UITypeEditor))]
  30. public Node ReadNode { get; set; }
  31. [Category("PLC")]
  32. [Description("确认地址1,必须为Bool类型")]
  33. [Editor(typeof(NodeTypeEditor), typeof(UITypeEditor))]
  34. public Node EnsureNode1 { get; set; }
  35. [Category("PLC")]
  36. [Description("确认地址2,必须为Bool类型")]
  37. [Editor(typeof(NodeTypeEditor), typeof(UITypeEditor))]
  38. public Node EnsureNode2 { get; set; }
  39. [Category("PLC")]
  40. [Description("窗体名称")]
  41. [DefaultValue("请选择")]
  42. public string FormCaption { get; set; }
  43. [Category("PLC")]
  44. [Description("提示信息")]
  45. [DefaultValue("提示")]
  46. public string Message { get; set; }
  47. [Category("PLC")]
  48. [Description("选项1的名称")]
  49. public string Text1 { get; set; }
  50. [Category("PLC")]
  51. [Description("选项2的名称")]
  52. public string Text2 { get; set; }
  53. [Category("PLC")]
  54. [Description("第一个按钮按下")]
  55. public event EventHandler Button1MouseDown;
  56. [Category("PLC")]
  57. [Description("第二个按钮按下")]
  58. public event EventHandler Button2MouseDown;
  59. [Category("PLC")]
  60. [Description("第一个按钮抬起")]
  61. public event EventHandler Button1MouseUp;
  62. [Category("PLC")]
  63. [Description("第二个按钮抬起")]
  64. public event EventHandler Button2MouseUp;
  65. [Browsable(false)]
  66. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  67. public IPlcComProtocol Protocol { get; set; }
  68. [Browsable(false)]
  69. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  70. public object State
  71. {
  72. get => state;
  73. set
  74. {
  75. if (!object.Equals(state, value))
  76. {
  77. state = value;
  78. OnPLCRegisterValueChanged?.Invoke(this, ReadNode, state);
  79. }
  80. if (value is bool b)
  81. {
  82. if (b)
  83. {
  84. if (optionSelectionForm == null || optionSelectionForm.IsDisposed)
  85. ShowOptionSelectionForm();
  86. }
  87. else
  88. {
  89. if (optionSelectionForm != null && !optionSelectionForm.IsDisposed)
  90. {
  91. if(plcUiButton1.Read().Equals(true))
  92. plcUiButton1.WriteValue(false);
  93. if (plcUiButton2.Read().Equals(true))
  94. plcUiButton2.WriteValue(false);
  95. optionSelectionForm.Close();
  96. }
  97. }
  98. }
  99. }
  100. }
  101. private object state;
  102. PlcUiButton plcUiButton1, plcUiButton2;
  103. PlcBaseForm optionSelectionForm;
  104. void ShowOptionSelectionForm()
  105. {
  106. optionSelectionForm = new PlcBaseForm();
  107. optionSelectionForm.Name = FormCaption;
  108. UILabel uILabel = new UILabel();
  109. uILabel.AutoSize = true;
  110. uILabel.Text = Message;
  111. uILabel.Left = 40;
  112. uILabel.Top = 60;
  113. optionSelectionForm.Controls.Add(uILabel);
  114. plcUiButton1 = new PlcUiButton();
  115. plcUiButton1.ButtonType = ButtonType.保持型;
  116. plcUiButton1.WriteNode = EnsureNode1;
  117. plcUiButton1.Name = Text1;
  118. plcUiButton1.Text = Text1;
  119. plcUiButton1.Size = new System.Drawing.Size(80, 40);
  120. plcUiButton1.Left = 80;
  121. plcUiButton1.Top = 120;
  122. plcUiButton1.Height = 40;
  123. plcUiButton1.OnSymbol = 0;
  124. plcUiButton1.OffSymbol = 0;
  125. plcUiButton1.Symbol = 0;
  126. plcUiButton1.Radius = 20;
  127. plcUiButton1.AfterMouseDown += PlcUiButton1_AfterMouseDown;
  128. plcUiButton1.AfterMouseUp += PlcUiButton1_AfterMouseUp;
  129. optionSelectionForm.Controls.Add(plcUiButton1);
  130. plcUiButton2 = new PlcUiButton();
  131. plcUiButton2.ButtonType = ButtonType.保持型;
  132. plcUiButton2.WriteNode = EnsureNode2;
  133. plcUiButton2.Name = Text2;
  134. plcUiButton2.Text = Text2;
  135. plcUiButton2.Size = new System.Drawing.Size(80, 40);
  136. plcUiButton2.Left = 220;
  137. plcUiButton2.Top = 120;
  138. plcUiButton2.Height = 40;
  139. plcUiButton2.OnSymbol = 0;
  140. plcUiButton2.OffSymbol = 0;
  141. plcUiButton2.Symbol = 0;
  142. plcUiButton2.Radius = 20;
  143. plcUiButton2.AfterMouseDown += PlcUiButton2_AfterMouseDown;
  144. plcUiButton2.AfterMouseUp += PlcUiButton2_AfterMouseUp;
  145. optionSelectionForm.Controls.Add(plcUiButton2);
  146. optionSelectionForm.TopMost = true;
  147. optionSelectionForm.Text = FormCaption;
  148. optionSelectionForm.Width = 400;
  149. optionSelectionForm.Height = 200;
  150. optionSelectionForm.MaximizeBox = false;
  151. optionSelectionForm.MinimizeBox = false;
  152. optionSelectionForm.Show();
  153. }
  154. private void PlcUiButton1_AfterMouseDown(object obj)
  155. {
  156. Button1MouseDown?.Invoke(this, EventArgs.Empty);
  157. }
  158. private void PlcUiButton2_AfterMouseDown(object obj)
  159. {
  160. Button2MouseDown?.Invoke(this, EventArgs.Empty);
  161. }
  162. private void PlcUiButton1_AfterMouseUp(object obj)
  163. {
  164. Button1MouseUp?.Invoke(this, EventArgs.Empty);
  165. }
  166. private void PlcUiButton2_AfterMouseUp(object obj)
  167. {
  168. Button2MouseUp?.Invoke(this, EventArgs.Empty);
  169. }
  170. public event RegisterValueChangedHandler OnPLCRegisterValueChanged;
  171. public event ReadRegisterFailedHandler OnReadRegisterFailed;
  172. public event UpdateComponentStateFailedHandler OnUpdateComponentStateFailed;
  173. public object Read()
  174. {
  175. if (Protocol != null && ReadNode != null)
  176. {
  177. object result;
  178. var rt = Protocol.ReadValue(ReadNode.Value, out result);
  179. if (rt == 0)
  180. {
  181. UpdateValue(result);
  182. return result;
  183. }
  184. else
  185. {
  186. OnReadRegisterFailed?.Invoke(this, ReadNode, rt);
  187. }
  188. }
  189. return null;
  190. }
  191. public void UpdateValue(object value)
  192. {
  193. if (value is bool)
  194. State = value;
  195. else
  196. OnUpdateComponentStateFailed?.Invoke(this, ReadNode, value, "读取到的值不是Bool类型");
  197. }
  198. }
  199. }