PlcMessageBox.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 PlcUiForm;
  12. namespace PlcComponent
  13. {
  14. public partial class PlcMessageBox : Component, ISingleOutputComponent
  15. {
  16. public PlcMessageBox()
  17. {
  18. InitializeComponent();
  19. }
  20. public PlcMessageBox(IContainer container)
  21. {
  22. container.Add(this);
  23. InitializeComponent();
  24. }
  25. [Category("PLC")]
  26. [Description("PLC读地址,必须为Bool类型")]
  27. [Editor(typeof(NodeTypeEditor), typeof(UITypeEditor))]
  28. public Node ReadNode { get; set; }
  29. [Category("PLC")]
  30. [Description("PLC写地址,当窗体关闭时写入的数据,必须为Bool类型")]
  31. [Editor(typeof(NodeTypeEditor), typeof(UITypeEditor))]
  32. public Node WriteNode { get; set; }
  33. [Category("PLC")]
  34. [Description("当窗体关闭时写入的值")]
  35. [DefaultValue(false)]
  36. public bool WriteValue { get; set; } = false;
  37. [Category("PLC")]
  38. [Description("窗体名称")]
  39. public string FormCaption { get; set; }
  40. [Category("PLC")]
  41. [Description("消息字符串")]
  42. public string Message { get; set; }
  43. [Browsable(false)]
  44. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  45. public IPlcComProtocol Protocol { get; set; }
  46. [Browsable(false)]
  47. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  48. public object State
  49. {
  50. get => state;
  51. set
  52. {
  53. if (!object.Equals(state, value))
  54. {
  55. state = value;
  56. OnPLCRegisterValueChanged?.Invoke(this, ReadNode, state);
  57. }
  58. if (value is bool b)
  59. {
  60. if (b)
  61. {
  62. if (messageBoxForm == null || messageBoxForm.IsDisposed)
  63. ShowForm();
  64. }
  65. else
  66. {
  67. if (messageBoxForm != null && !messageBoxForm.IsDisposed)
  68. messageBoxForm.Close();
  69. }
  70. }
  71. }
  72. }
  73. public bool SupportMultiNodes { get; } = false;
  74. private object state;
  75. PlcBaseForm messageBoxForm;
  76. void ShowForm()
  77. {
  78. messageBoxForm = new PlcBaseForm();
  79. Label label = new Label();
  80. label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  81. label.Dock = DockStyle.Fill;
  82. label.Text = Message;
  83. label.Font = new System.Drawing.Font("宋体", 20);
  84. messageBoxForm.Controls.Add(label);
  85. messageBoxForm.FormClosing += MessageBoxForm_FormClosing;
  86. messageBoxForm.TopMost = true;
  87. messageBoxForm.Text = FormCaption;
  88. messageBoxForm.Width = 400;
  89. messageBoxForm.Height = 200;
  90. messageBoxForm.MaximizeBox = false;
  91. messageBoxForm.MinimizeBox = false;
  92. messageBoxForm.Show();
  93. }
  94. private void MessageBoxForm_FormClosing(object sender, FormClosingEventArgs e)
  95. {
  96. if (WriteNode != null)
  97. {
  98. uint result = 0;
  99. var rt = Protocol.WriteValue(WriteNode.Value, WriteValue, ref result);
  100. }
  101. }
  102. public event RegisterValueChangedHandler OnPLCRegisterValueChanged;
  103. public event ReadRegisterFailedHandler OnReadRegisterFailed;
  104. public event UpdateComponentStateFailedHandler OnUpdateComponentStateFailed;
  105. public object Read()
  106. {
  107. if (Protocol != null && ReadNode != null)
  108. {
  109. object result;
  110. var rt = Protocol.ReadValue(ReadNode.Value, out result);
  111. if (rt == 0)
  112. {
  113. UpdateValue(result);
  114. return result;
  115. }
  116. else
  117. {
  118. OnReadRegisterFailed?.Invoke(this, ReadNode, rt);
  119. }
  120. }
  121. return null;
  122. }
  123. public void UpdateValue(object value)
  124. {
  125. if (value is bool)
  126. State = value;
  127. else
  128. OnUpdateComponentStateFailed?.Invoke(this, ReadNode, value, "读取到的值不是Bool类型");
  129. }
  130. public void UpdateValues(object value)
  131. {
  132. throw new NotImplementedException();
  133. }
  134. }
  135. }