FrmPressure.cs 5.2 KB

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