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; } foreach(var txt in uiTitlePanel压机.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 = ""; foreach (var ctl in uiTitlePanel压机.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; } } }