using Sunny.UI; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace HRT_Measure { public partial class FrmPressure : Sunny.UI.UIForm { public FrmPressure() { InitializeComponent(); } private Thread thMonitor = null; private bool enable = false; private void FrmPressure_Load(object sender, EventArgs e) { thMonitor = new Thread(new ThreadStart(ThMonitor)); thMonitor.IsBackground = true; thMonitor.Start(); } private void ThMonitor() { while(true) { if(!ProgramHelp.Instance.comm压机.isPLCConnected ) { Thread.Sleep(1000); continue; } if(enable) { Thread.Sleep(1000); continue; } TabPage targetTab = tbp油缸自动参数设定.SelectedTab; // 获取当前选中的TabPage foreach (var txt in targetTab.Controls) { if (txt is UITextBox) { string name = ((UITextBox)txt).Name.Remove(0, 3); ((UITextBox)txt).BeginInvoke(new Action(() => { ((UITextBox)txt).Text = ReadPLCHelp.dictPressure[name].value; })); } } foreach (var txt in gpb伺服电机参数设定.Controls) { if (txt is UITextBox) { string name = ((UITextBox)txt).Name.Remove(0, 3); ((UITextBox)txt).BeginInvoke(new Action(() => { ((UITextBox)txt).Text = ReadPLCHelp.dictPressure[name].value; })); } } Thread.Sleep(100); } } private void btnWrite(object sender, EventArgs e) { try { if(!enable) { MessageBox.Show("请点击写入开关按钮!"); return; } var btn = (UISymbolButton)sender; string name = btn.Name.Remove(0, 3); string strValue = ""; if (btn.Parent is UIGroupBox) //伺服电机参数设定 { foreach (var ctl in gpb伺服电机参数设定.Controls) { if (ctl is UITextBox) { if (((UITextBox)ctl).Name == $"txt{name}") { strValue = ((UITextBox)ctl).Text; } } } } else //油缸自动参数设定 { TabPage targetTab = tbp油缸自动参数设定.SelectedTab; // 获取当前选中的TabPage foreach (var ctl in targetTab.Controls) { if (ctl is UITextBox) { if (((UITextBox)ctl).Name == $"txt{name}") { strValue = ((UITextBox)ctl).Text; } } } } switch (ReadPLCHelp.dictPressure[name].type) { case "short": ProgramHelp.Instance.comm压机.WriteShort(ReadPLCHelp.dictPressure[name].Address,Convert.ToInt16( Convert.ToSingle(strValue) / ReadPLCHelp.dictPressure[name].Scale), out string str); break; case "float": ProgramHelp.Instance.comm压机.WriteFloat(ReadPLCHelp.dictPressure[name].Address, Convert.ToSingle(strValue) / ReadPLCHelp.dictPressure[name].Scale, out str); break; case "int": ProgramHelp.Instance.comm压机.WriteInt(ReadPLCHelp.dictPressure[name].Address, Convert.ToInt32(Convert.ToSingle(strValue) / ReadPLCHelp.dictPressure[name].Scale), out str); break; } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void switchWrite_ValueChanged(object sender, bool value) { enable = value; } } }