using Sunny.UI; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics.Eventing.Reader; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace HRT_Measure { public partial class UCHMI : UserControl { public UCHMI() { InitializeComponent(); } public string DisplayLabel { set { uiTitlePanel1.Text = value; } get { return uiTitlePanel1.Text; } } public Thread thMonitor ; public string strPLC = ""; private void UCHMI_Load(object sender, EventArgs e) { this.Dock = DockStyle.Fill; //timer1.Enabled = true; //splitContainer1.SplitterDistance = this.Height / 2; switch (DisplayLabel) { case "上料房 称重房": strPLC = "M01M02"; break; case "压机房": strPLC = "M03"; break; case "检测房 装箱房": strPLC = "M04M05"; break; } thMonitor = new Thread(new ThreadStart(ThMonitor)); thMonitor.IsBackground = true; txtAlm.Start(); } #region Button private void btnInitilize_Click(object sender, EventArgs e) { //switch (strPLC) //{ // case "M01M02": // ProgramHelp.Instance.commPLC_M01M02.WriteCoin("DB20.494.1", true, out string str); // break; // case "M03": // ProgramHelp.Instance.commPLC_M03.WriteCoin("DB20.4.1", true, out str); // break; // case "M04M05": // ProgramHelp.Instance.commPLC_M04M05.WriteCoin("DB20.0.1", true, out str); // break; //} } private void btnInitilize_MouseDown(object sender, MouseEventArgs e) { switch (strPLC) { case "M01M02": ProgramHelp.Instance.commPLC_M01M02.WriteCoin("DB20.494.1", true, out string str); break; case "M03": ProgramHelp.Instance.commPLC_M03.WriteCoin("DB20.4.1", true, out str); break; case "M04M05": ProgramHelp.Instance.commPLC_M04M05.WriteCoin("DB20.0.1", true, out str); break; } } private void btnInitilize_MouseUp(object sender, MouseEventArgs e) { switch (strPLC) { case "M01M02": ProgramHelp.Instance.commPLC_M01M02.WriteCoin("DB20.494.1", false, out string str); break; case "M03": ProgramHelp.Instance.commPLC_M03.WriteCoin("DB20.4.1", false, out str); break; case "M04M05": ProgramHelp.Instance.commPLC_M04M05.WriteCoin("DB20.0.1", false, out str); break; } } private void btnClearException_Click(object sender, EventArgs e) { switch (strPLC) { case "M01M02": ProgramHelp.Instance.commPLC_M01M02.WriteCoin("DB20.494.2", true, out string str); break; case "M03": ProgramHelp.Instance.commPLC_M03.WriteCoin("DB20.4.2", true, out str); break; case "M04M05": ProgramHelp.Instance.commPLC_M04M05.WriteCoin("DB20.0.2", true, out str); break; } txtAlm.Text = ""; } private void btnClearException_MouseDown(object sender, MouseEventArgs e) { //switch (strPLC) //{ // case "M01M02": // ProgramHelp.Instance.commPLC_M01M02.WriteCoin("DB20.494.2", true, out string str); // break; // case "M03": // ProgramHelp.Instance.commPLC_M03.WriteCoin("DB20.4.2", true, out str); // break; // case "M04M05": // ProgramHelp.Instance.commPLC_M04M05.WriteCoin("DB20.0.2", true, out str); // break; //} //txtAlm.Text = ""; } private void btnClearException_MouseUp(object sender, MouseEventArgs e) { switch (strPLC) { case "M01M02": ProgramHelp.Instance.commPLC_M01M02.WriteCoin("DB20.494.2", false, out string str); break; case "M03": ProgramHelp.Instance.commPLC_M03.WriteCoin("DB20.4.2", false, out str); break; case "M04M05": ProgramHelp.Instance.commPLC_M04M05.WriteCoin("DB20.0.2", false, out str); break; } txtAlm.Text = ""; } private void btnStop_Click(object sender, EventArgs e) { //switch (strPLC) //{ // case "M01M02": // ProgramHelp.Instance.commPLC_M01M02.WriteCoin("DB20.494.4", true, out string str); // break; // case "M03": // ProgramHelp.Instance.commPLC_M03.WriteCoin("DB20.4.4", true, out str); // break; // case "M04M05": // ProgramHelp.Instance.commPLC_M04M05.WriteCoin("DB20.0.4", true, out str); // break; //} } private void btnStop_MouseDown(object sender, MouseEventArgs e) { switch (strPLC) { case "M01M02": ProgramHelp.Instance.commPLC_M01M02.WriteCoin("DB20.494.4", true, out string str); break; case "M03": ProgramHelp.Instance.commPLC_M03.WriteCoin("DB20.4.4", true, out str); break; case "M04M05": ProgramHelp.Instance.commPLC_M04M05.WriteCoin("DB20.0.4", true, out str); break; } } private void btnStop_MouseUp(object sender, MouseEventArgs e) { switch (strPLC) { case "M01M02": ProgramHelp.Instance.commPLC_M01M02.WriteCoin("DB20.494.4", false, out string str); break; case "M03": ProgramHelp.Instance.commPLC_M03.WriteCoin("DB20.4.4", false, out str); break; case "M04M05": ProgramHelp.Instance.commPLC_M04M05.WriteCoin("DB20.0.4", false, out str); break; } } private void btnStart_Click(object sender, EventArgs e) { //switch (strPLC) //{ // case "M01M02": // ProgramHelp.Instance.commPLC_M01M02.WriteCoin("DB20.494.3", true, out string str); // break; // case "M03": // ProgramHelp.Instance.commPLC_M03.WriteCoin("DB48.25.1", true, out str); // break; // case "M04M05": // ProgramHelp.Instance.commPLC_M04M05.WriteCoin("DB48.25.1", true, out str); // break; //} } private void btnStart_MouseDown(object sender, MouseEventArgs e) { switch (strPLC) { case "M01M02": ProgramHelp.Instance.commPLC_M01M02.WriteCoin("DB20.494.3", true, out string str); break; case "M03": ProgramHelp.Instance.commPLC_M03.WriteCoin("DB48.25.1", true, out str); break; case "M04M05": ProgramHelp.Instance.commPLC_M04M05.WriteCoin("DB48.25.1", true, out str); break; } } private void btnStart_MouseUp(object sender, MouseEventArgs e) { switch (strPLC) { case "M01M02": ProgramHelp.Instance.commPLC_M01M02.WriteCoin("DB20.494.3", false, out string str); break; case "M03": ProgramHelp.Instance.commPLC_M03.WriteCoin("DB48.25.1", false, out str); break; case "M04M05": ProgramHelp.Instance.commPLC_M04M05.WriteCoin("DB48.25.1", false, out str); break; } } private void btnHandAndAuto_Click(object sender, EventArgs e) { switch (strPLC) { case "M01M02": //ProgramHelp.Instance.commPLC_M01M02.ReadCoin("DB20.494.5", out bool isTrue, out string str); ProgramHelp.Instance.commPLC_M01M02.WriteCoin("DB20.494.5", !PLCCylinder.dictPLC_InitIO["M01M02"].isHandOrAuto , out string str);//isTrue break; case "M03": //ProgramHelp.Instance.commPLC_M03.ReadCoin("DB20.4.5", out isTrue, out str); ProgramHelp.Instance.commPLC_M03.WriteCoin("DB20.4.5", !PLCCylinder.dictPLC_InitIO["M03"].isHandOrAuto, out str); break; case "M04M05": //ProgramHelp.Instance.commPLC_M04M05.ReadCoin("DB20.0.5", out isTrue, out str); ProgramHelp.Instance.commPLC_M04M05.WriteCoin("DB20.0.5", !PLCCylinder.dictPLC_InitIO["M04M05"].isHandOrAuto, out str); break; } } #endregion private void timer1_Tick(object sender, EventArgs e) { try { //string strPLC = strPLC; //if (PLCCylinder.dictPLC_InitIO[strPLC].isHandOrAuto) btnHandAndAuto.BeginInvoke(new Action(() => { btnHandAndAuto.Text = "手动"; btnHandAndAuto.Symbol = 362038; })); //else btnHandAndAuto.BeginInvoke(new Action(() => { btnHandAndAuto.Text = "自动"; btnHandAndAuto.Symbol = 57531; })); if (PLCCylinder.dictPLC_InitIO[strPLC].isInitDone) lightInitDone.BeginInvoke(new Action(() => { lightInitDone.State = Sunny.UI.UILightState.On; })); else lightInitDone.BeginInvoke(new Action(() => { lightInitDone.State = Sunny.UI.UILightState.Off; })); if (PLCCylinder.dictPLC_InitIO[strPLC].isWait) lightWait.BeginInvoke(new Action(() => { lightWait.State = Sunny.UI.UILightState.On; })); else lightWait.BeginInvoke(new Action(() => { lightWait.State = Sunny.UI.UILightState.Off; })); if (PLCCylinder.dictPLC_InitIO[strPLC].isAlm) lightAlm.BeginInvoke(new Action(() => { lightAlm.State = Sunny.UI.UILightState.On; })); else lightAlm.BeginInvoke(new Action(() => { lightAlm.State = Sunny.UI.UILightState.Off; })); if (PLCCylinder.dictPLC_InitIO[strPLC].isAutoRunning) lightAutoRun.BeginInvoke(new Action(() => { lightAutoRun.State = Sunny.UI.UILightState.On; })); else lightAutoRun.BeginInvoke(new Action(() => { lightAutoRun.State = Sunny.UI.UILightState.Off; })); btnInitilize.BeginInvoke(new Action(() => { btnInitilize.FillColor = PLCCylinder.dictPLC_InitIO[strPLC].isBtnInit ? Color.Red : Color.LightSkyBlue; })); btnClearException.BeginInvoke(new Action(() => { btnClearException.FillColor = PLCCylinder.dictPLC_InitIO[strPLC].isBtnExceptionQuit ? Color.Red : Color.LightSkyBlue; })); btnStop.BeginInvoke(new Action(() => { btnStop.FillColor = PLCCylinder.dictPLC_InitIO[strPLC].isBtnStop ? Color.Red : Color.LightSkyBlue; })); btnStart.BeginInvoke(new Action(() => { btnStart.FillColor = PLCCylinder.dictPLC_InitIO[strPLC].isBtnStart ? Color.Red : Color.LightSkyBlue; })); btnHandAndAuto.BeginInvoke(new Action(() => { btnHandAndAuto.FillColor = PLCCylinder.dictPLC_InitIO[strPLC].isBtnHandOrAuto ? Color.Red : Color.LightSkyBlue; })); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void ThMonitor() { while (true) { try { if (PLCCylinder.dictPLC_InitIO[strPLC].isInitDone) lightInitDone.BeginInvoke(new Action(() => { lightInitDone.State = Sunny.UI.UILightState.On; })); else lightInitDone.BeginInvoke(new Action(() => { lightInitDone.State = Sunny.UI.UILightState.Off; })); if (PLCCylinder.dictPLC_InitIO[strPLC].isWait) lightWait.BeginInvoke(new Action(() => { lightWait.State = Sunny.UI.UILightState.On; })); else lightWait.BeginInvoke(new Action(() => { lightWait.State = Sunny.UI.UILightState.Off; })); if (PLCCylinder.dictPLC_InitIO[strPLC].isAlm) lightAlm.BeginInvoke(new Action(() => { lightAlm.State = Sunny.UI.UILightState.On; })); else { lightAlm.BeginInvoke(new Action(() => { lightAlm.State = Sunny.UI.UILightState.Off; })); txtAlm.BeginInvoke(new Action(() => { txtAlm.Text = ""; })); } if (PLCCylinder.dictPLC_InitIO[strPLC].isAutoRunning) lightAutoRun.BeginInvoke(new Action(() => { lightAutoRun.State = Sunny.UI.UILightState.On; })); else lightAutoRun.BeginInvoke(new Action(() => { lightAutoRun.State = Sunny.UI.UILightState.Off; })); btnInitilize.BeginInvoke(new Action(() => { btnInitilize.FillColor = PLCCylinder.dictPLC_InitIO[strPLC].isBtnInit ? Color.Red : Color.LightSkyBlue; })); btnClearException.BeginInvoke(new Action(() => { btnClearException.FillColor = PLCCylinder.dictPLC_InitIO[strPLC].isBtnExceptionQuit ? Color.Red : Color.LightSkyBlue; })); btnStop.BeginInvoke(new Action(() => { btnStop.FillColor = PLCCylinder.dictPLC_InitIO[strPLC].isBtnStop ? Color.Red : Color.LightSkyBlue; })); btnStart.BeginInvoke(new Action(() => { btnStart.FillColor = PLCCylinder.dictPLC_InitIO[strPLC].isBtnStart ? Color.Red : Color.LightSkyBlue; })); btnHandAndAuto.BeginInvoke(new Action(() => { btnHandAndAuto.FillColor = PLCCylinder.dictPLC_InitIO[strPLC].isBtnHandOrAuto ? Color.Red : Color.LightSkyBlue; btnHandAndAuto.Text = PLCCylinder.dictPLC_InitIO[strPLC].isBtnHandOrAuto ? "自动模式" : "手动模式"; })); switch(strPLC) { case "M01M02": foreach (var item in PLCCylinder.dictM01M02_Alm) { if (item.Value.isAlm) txtAlm.BeginInvoke(new Action(() => { if(!txtAlm.Text.Contains(item.Value.AlmName)) txtAlm.Text += item.Value.AlmName + ";"; })); } break; case "M03": foreach (var item in PLCCylinder.dictM03_Alm) { if (item.Value.isAlm) txtAlm.BeginInvoke(new Action(() => { if (!txtAlm.Text.Contains(item.Value.AlmName)) txtAlm.Text += item.Value.AlmName + ";"; })); } break; case "M04M05": foreach (var item in PLCCylinder.dictM04M05_Alm) { if (item.Value.isAlm) txtAlm.BeginInvoke(new Action(() => { if (!txtAlm.Text.Contains(item.Value.AlmName)) txtAlm.Text += item.Value.AlmName + ";"; })); } break; } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } Thread.Sleep(1); } } } }