FrmPressure.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. TabPage targetTab = tbp油缸自动参数设定.SelectedTab; // 获取当前选中的TabPage
  43. foreach (var txt in targetTab.Controls)
  44. {
  45. if (txt is UITextBox)
  46. {
  47. string name = ((UITextBox)txt).Name.Remove(0, 3);
  48. ((UITextBox)txt).BeginInvoke(new Action(() => { ((UITextBox)txt).Text = ReadPLCHelp.dictPressure[name].value; }));
  49. }
  50. }
  51. foreach (var txt in gpb伺服电机参数设定.Controls)
  52. {
  53. if (txt is UITextBox)
  54. {
  55. string name = ((UITextBox)txt).Name.Remove(0, 3);
  56. ((UITextBox)txt).BeginInvoke(new Action(() => { ((UITextBox)txt).Text = ReadPLCHelp.dictPressure[name].value; }));
  57. }
  58. }
  59. Thread.Sleep(100);
  60. }
  61. }
  62. private void btnWrite(object sender, EventArgs e)
  63. {
  64. try
  65. {
  66. if(!enable)
  67. {
  68. MessageBox.Show("请点击写入开关按钮!");
  69. return;
  70. }
  71. var btn = (UISymbolButton)sender;
  72. string name = btn.Name.Remove(0, 3);
  73. string strValue = "";
  74. if (btn.Parent is UIGroupBox) //伺服电机参数设定
  75. {
  76. foreach (var ctl in gpb伺服电机参数设定.Controls)
  77. {
  78. if (ctl is UITextBox)
  79. {
  80. if (((UITextBox)ctl).Name == $"txt{name}")
  81. {
  82. strValue = ((UITextBox)ctl).Text;
  83. }
  84. }
  85. }
  86. }
  87. else //油缸自动参数设定
  88. {
  89. TabPage targetTab = tbp油缸自动参数设定.SelectedTab; // 获取当前选中的TabPage
  90. foreach (var ctl in targetTab.Controls)
  91. {
  92. if (ctl is UITextBox)
  93. {
  94. if (((UITextBox)ctl).Name == $"txt{name}")
  95. {
  96. strValue = ((UITextBox)ctl).Text;
  97. }
  98. }
  99. }
  100. }
  101. switch (ReadPLCHelp.dictPressure[name].type)
  102. {
  103. case "short":
  104. ProgramHelp.Instance.comm压机.WriteShort(ReadPLCHelp.dictPressure[name].Address,Convert.ToInt16( Convert.ToSingle(strValue) / ReadPLCHelp.dictPressure[name].Scale), out string str);
  105. break;
  106. case "float":
  107. ProgramHelp.Instance.comm压机.WriteFloat(ReadPLCHelp.dictPressure[name].Address, Convert.ToSingle(strValue) / ReadPLCHelp.dictPressure[name].Scale, out str);
  108. break;
  109. case "int":
  110. ProgramHelp.Instance.comm压机.WriteInt(ReadPLCHelp.dictPressure[name].Address, Convert.ToInt32(Convert.ToSingle(strValue) / ReadPLCHelp.dictPressure[name].Scale), out str);
  111. break;
  112. }
  113. }
  114. catch (Exception ex)
  115. {
  116. MessageBox.Show(ex.ToString());
  117. }
  118. }
  119. private void switchWrite_ValueChanged(object sender, bool value)
  120. {
  121. enable = value;
  122. }
  123. }
  124. }