using Newtonsoft.Json.Linq; 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 UCCylinder : UserControl { public UCCylinder() { InitializeComponent(); } public UCCylinder(string plcName, string strInfo,Dictionary dictIO) { InitializeComponent(); strPLCName = plcName; DictPLCIO = dictIO; string[] arrStr = strInfo.Split(','); CylinderName = arrStr[0]; if (arrStr.Length == 2) { lightReset.Visible = false; lightSet.Visible = false; btnReset.Visible = false; string[] arrStrAdd = arrStr[1].Split('-'); btnSetting.Text = arrStrAdd[0]; strSetAdd = arrStrAdd[1]; strLedSetAdd = arrStrAdd[2]; } else if (arrStr.Length == 3) { string strreset = arrStr[1]; string[] arrreset = strreset.Split('-'); if(arrreset.Length == 5) { btnReset.Text = arrreset[0]; strResetAdd = arrreset[1]; strLedResetAdd = arrreset[2]; strResetEnableAdd = arrreset[3]; strSensorResetAdd = arrreset[4]; } else if(arrreset.Length == 4) { btnReset.Text = arrreset[0]; strResetAdd = arrreset[1]; strLedResetAdd = arrreset[2]; strResetEnableAdd = arrreset[3]; lightReset.Visible = false; } string strset= arrStr[2]; string[] arrset = strset.Split('-'); if(arrset.Length == 5) { btnSetting.Text = arrset[0]; strSetAdd = arrset[1]; strLedSetAdd = arrset[2]; strSetEnableAdd = arrset[3]; strSensorSetAdd = arrset[4]; } else if(arrset.Length == 4) { btnSetting.Text = arrset[0]; strSetAdd = arrset[1]; strLedSetAdd = arrset[2]; strSetEnableAdd = arrset[3]; lightSet.Visible = false; } } commPLC = GetPLC(); thMonitor = new Thread(new ThreadStart(ThMonitor)); thMonitor.IsBackground = true; } public Thread thMonitor ; CommPLC commPLC; private Dictionary DictPLCIO = null; public string CylinderName { set { TitleName.Text = value; } get { return TitleName.Text; } } private string strResetAdd = ""; private string strSetAdd = ""; private string strLedResetAdd = ""; private string strLedSetAdd = ""; private string strSensorResetAdd = ""; private string strSensorSetAdd = ""; private string strResetEnableAdd = ""; private string strSetEnableAdd = ""; private string strPLCName = ""; #region MyRegion public string ResetAdd { get { return strResetAdd; } set { strResetAdd = value; } } public string SetAdd { get { return strSetAdd; } set { strSetAdd = value; } } public string LedResetAdd { get { return strLedResetAdd; } set { strLedResetAdd = value; } } public string LedSetAdd { get { return strLedSetAdd; } set { strLedSetAdd = value; } } public string SensorResetAdd { get { return strSensorResetAdd; } set { strSensorResetAdd = value; } } public string SensorSetAdd { get { return strSensorSetAdd; } set { strSensorSetAdd = value; } } public string ResetEnableAdd { get { return strResetEnableAdd; } set { strResetEnableAdd = value; } } public string SetEnableAdd { get { return strSetEnableAdd; } set { strSetEnableAdd = value; } } public string PLCName { get { return strPLCName; } set { strPLCName = value; } } #endregion private void UCCylinder_Load(object sender, EventArgs e) { this.Dock = DockStyle.Fill; } private CommPLC GetPLC() { if(PLCName == "M01M02") return ProgramHelp.Instance.commPLC_M01M02; if (PLCName == "M03") return ProgramHelp.Instance.commPLC_M03; if (PLCName == "M04M05") return ProgramHelp.Instance.commPLC_M04M05; return null; } private void btnReset_MouseDown(object sender, MouseEventArgs e) { try { ReadPLCHelp.SetBoolValue(ResetAdd, commPLC, true); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void btnReset_MouseUp(object sender, MouseEventArgs e) { try { ReadPLCHelp.SetBoolValue(ResetAdd, commPLC, false); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void btnSetting_MouseDown(object sender, MouseEventArgs e) { try { ReadPLCHelp.SetBoolValue(SetAdd, commPLC, true); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void btnSetting_MouseUp(object sender, MouseEventArgs e) { try { ReadPLCHelp.SetBoolValue(SetAdd, commPLC, false); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void timer1_Tick(object sender, EventArgs e) { try { if (strSensorResetAdd != "") lightReset?.BeginInvoke(new Action(() => { lightReset.State = DictPLCIO[CylinderName].isSensorResetOn ? Sunny.UI.UILightState.On : Sunny.UI.UILightState.Off; })); if (strSensorSetAdd != "") lightSet?.BeginInvoke(new Action(() => { lightSet.State = DictPLCIO[CylinderName].isSensorSetOn ? Sunny.UI.UILightState.On : Sunny.UI.UILightState.Off; })); if (strLedResetAdd != "") btnReset?.BeginInvoke(new Action(() => { btnReset.FillColor = DictPLCIO[CylinderName].isLedResetOn ? Color.Green : Color.Gray; })); if (strLedSetAdd != "") btnSetting?.BeginInvoke(new Action(() => { btnSetting.FillColor = DictPLCIO[CylinderName].isLedSetOn ? Color.Green : Color.Gray; })); } catch (Exception ) { //MessageBox.Show(ex.ToString()); } } private void UCCylinder_Leave(object sender, EventArgs e) { timer1.Enabled = false; //try //{ // thMonitor.Abort(); //} //catch (Exception) //{ //} } private void ThMonitor() { while (true) { try { if ((PLCName == "M01M02" && ReadPLCHelp.EnumWindowDisplay != WindowDisplay.上料称重房) || (PLCName == "M03" && ReadPLCHelp.EnumWindowDisplay != WindowDisplay.压机房) || (PLCName == "M04M05" && ReadPLCHelp.EnumWindowDisplay != WindowDisplay.检测装箱房)) { Thread.Sleep(10); continue; } if (strSensorResetAdd != "") lightReset?.BeginInvoke(new Action(() => { lightReset.State = DictPLCIO[CylinderName].isSensorResetOn ? Sunny.UI.UILightState.On : Sunny.UI.UILightState.Off; })); if (strSensorSetAdd != "") lightSet?.BeginInvoke(new Action(() => { lightSet.State = DictPLCIO[CylinderName].isSensorSetOn ? Sunny.UI.UILightState.On : Sunny.UI.UILightState.Off; })); if (strLedResetAdd != "") btnReset?.BeginInvoke(new Action(() => { btnReset.FillColor = DictPLCIO[CylinderName].isLedResetOn ? Color.Green : Color.Gray; })); if (strLedSetAdd != "") btnSetting?.BeginInvoke(new Action(() => { btnSetting.FillColor = DictPLCIO[CylinderName].isLedSetOn ? Color.Green : Color.Gray; })); } catch (Exception ) { //MessageBox.Show(ex.ToString()); } Thread.Sleep(20); } } } }