FrmPressure.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Sunny.UI;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace HRT_Measure
  13. {
  14. public partial class FrmPressure : Sunny.UI.UIForm
  15. {
  16. public FrmPressure()
  17. {
  18. InitializeComponent();
  19. }
  20. private Thread thMonitor = null;
  21. private bool enable = false;
  22. private void FrmPressure_Load(object sender, EventArgs e)
  23. {
  24. thMonitor = new Thread(new ThreadStart(ThMonitor));
  25. thMonitor.IsBackground = true;
  26. thMonitor.Start();
  27. }
  28. private void ThMonitor()
  29. {
  30. while(true)
  31. {
  32. if(!ProgramHelp.Instance.comm压机.isPLCConnected )
  33. {
  34. Thread.Sleep(1000);
  35. continue;
  36. }
  37. if(enable)
  38. {
  39. Thread.Sleep(1000);
  40. continue;
  41. }
  42. foreach(var txt in uiTitlePanel压机.Controls)
  43. {
  44. if (txt is UITextBox)
  45. {
  46. string name = ((UITextBox)txt).Name.Remove(0, 3);
  47. ((UITextBox)txt).BeginInvoke(new Action(() => { ((UITextBox)txt).Text = ReadPLCHelp.dictPressure[name].value; }));
  48. }
  49. }
  50. Thread.Sleep(100);
  51. }
  52. }
  53. private void btnWrite(object sender, EventArgs e)
  54. {
  55. try
  56. {
  57. if(!enable)
  58. {
  59. MessageBox.Show("请点击写入开关按钮!");
  60. return;
  61. }
  62. var btn = (UISymbolButton)sender;
  63. string name = btn.Name.Remove(0, 3);
  64. string strValue = "";
  65. foreach (var ctl in uiTitlePanel压机.Controls)
  66. {
  67. if (ctl is UITextBox)
  68. {
  69. if (((UITextBox)ctl).Name == $"txt{name}")
  70. {
  71. strValue = ((UITextBox)ctl).Text;
  72. }
  73. }
  74. }
  75. switch (ReadPLCHelp.dictPressure[name].type)
  76. {
  77. case "short":
  78. ProgramHelp.Instance.comm压机.WriteShort(ReadPLCHelp.dictPressure[name].Address,Convert.ToInt16( Convert.ToSingle(strValue) / ReadPLCHelp.dictPressure[name].Scale), out string str);
  79. break;
  80. case "float":
  81. ProgramHelp.Instance.comm压机.WriteFloat(ReadPLCHelp.dictPressure[name].Address, Convert.ToSingle(strValue) / ReadPLCHelp.dictPressure[name].Scale, out str);
  82. break;
  83. case "int":
  84. ProgramHelp.Instance.comm压机.WriteInt(ReadPLCHelp.dictPressure[name].Address, Convert.ToInt32(Convert.ToSingle(strValue) / ReadPLCHelp.dictPressure[name].Scale), out str);
  85. break;
  86. }
  87. }
  88. catch (Exception ex)
  89. {
  90. MessageBox.Show(ex.ToString());
  91. }
  92. }
  93. private void switchWrite_ValueChanged(object sender, bool value)
  94. {
  95. enable = value;
  96. }
  97. }
  98. }