diff --git a/CleanMachineHMI/CleanMachineHMI.csproj b/CleanMachineHMI/CleanMachineHMI.csproj index 67978eb..22e5fb6 100644 --- a/CleanMachineHMI/CleanMachineHMI.csproj +++ b/CleanMachineHMI/CleanMachineHMI.csproj @@ -1,7 +1,7 @@  - WinExe + Exe net472 true @@ -45,10 +45,18 @@ ..\public\FastReport.dll + + + + + + + UserControl + UserControl diff --git a/CleanMachineHMI/FrmUserControl/IOBlock.Designer.cs b/CleanMachineHMI/FrmUserControl/IOBlock.Designer.cs new file mode 100644 index 0000000..bafbee6 --- /dev/null +++ b/CleanMachineHMI/FrmUserControl/IOBlock.Designer.cs @@ -0,0 +1,101 @@ +namespace CleanMachineHMI.FrmUserControl +{ + partial class IOBlock + { + /// + /// 必需的设计器变量。 + /// + private System.ComponentModel.IContainer components = null; + + /// + /// 清理所有正在使用的资源。 + /// + /// 如果应释放托管资源,为 true;否则为 false。 + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region 组件设计器生成的代码 + + /// + /// 设计器支持所需的方法 - 不要修改 + /// 使用代码编辑器修改此方法的内容。 + /// + private void InitializeComponent() + { + this.components = new System.ComponentModel.Container(); + this.panel62 = new System.Windows.Forms.Panel(); + this.labelName = new Krypton.Toolkit.KryptonLabel(); + this.cbt_value = new CleanMachineHMI.CircleButton(); + this.labelCode = new Krypton.Toolkit.KryptonLabel(); + this.toolTip1 = new System.Windows.Forms.ToolTip(this.components); + this.panel62.SuspendLayout(); + this.SuspendLayout(); + // + // panel62 + // + this.panel62.BackColor = System.Drawing.Color.White; + this.panel62.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.panel62.Controls.Add(this.labelName); + this.panel62.Controls.Add(this.cbt_value); + this.panel62.Controls.Add(this.labelCode); + this.panel62.Dock = System.Windows.Forms.DockStyle.Fill; + this.panel62.Location = new System.Drawing.Point(0, 0); + this.panel62.Name = "panel62"; + this.panel62.Size = new System.Drawing.Size(222, 35); + this.panel62.TabIndex = 2; + // + // labelName + // + this.labelName.Location = new System.Drawing.Point(50, 7); + this.labelName.Name = "labelName"; + this.labelName.Size = new System.Drawing.Size(84, 15); + this.labelName.StateCommon.ShortText.Font = new System.Drawing.Font("宋体", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); + this.labelName.TabIndex = 3; + this.labelName.Values.Text = "kryptonLabel2"; + // + // cbt_value + // + this.cbt_value.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(191)))), ((int)(((byte)(219)))), ((int)(((byte)(255))))); + this.cbt_value.Location = new System.Drawing.Point(31, 3); + this.cbt_value.Name = "cbt_value"; + this.cbt_value.Size = new System.Drawing.Size(23, 23); + this.cbt_value.TabIndex = 2; + this.cbt_value.UseVisualStyleBackColor = false; + // + // labelCode + // + this.labelCode.Location = new System.Drawing.Point(0, 6); + this.labelCode.Name = "labelCode"; + this.labelCode.Size = new System.Drawing.Size(32, 17); + this.labelCode.StateCommon.ShortText.Font = new System.Drawing.Font("宋体", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); + this.labelCode.TabIndex = 2; + this.labelCode.Values.Text = "Y17"; + // + // IOBlock + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.panel62); + this.Name = "IOBlock"; + this.Size = new System.Drawing.Size(222, 35); + this.panel62.ResumeLayout(false); + this.panel62.PerformLayout(); + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.Panel panel62; + private Krypton.Toolkit.KryptonLabel labelName; + private CircleButton cbt_value; + private Krypton.Toolkit.KryptonLabel labelCode; + private System.Windows.Forms.ToolTip toolTip1; + } +} diff --git a/CleanMachineHMI/FrmUserControl/IOBlock.cs b/CleanMachineHMI/FrmUserControl/IOBlock.cs new file mode 100644 index 0000000..de2c432 --- /dev/null +++ b/CleanMachineHMI/FrmUserControl/IOBlock.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace CleanMachineHMI.FrmUserControl +{ + public partial class IOBlock : UserControl + { + public IOBlock() + { + InitializeComponent(); + + } + + public string Code + { + set + { + this.labelCode.Text = value; + } + } + + public string IOName + { + set + { + this.labelName.Text = value; + this.toolTip1.SetToolTip(this.labelName, value); + // this.toolTip1.ToolTipTitle = value; + } + } + + public bool Value + { + + set + { + + this.cbt_value.BackColor = value ? Color.Lime : Color.Red; + + } + + } + + + + + + + } +} diff --git a/CleanMachineHMI/FrmUserControl/IOBlock.resx b/CleanMachineHMI/FrmUserControl/IOBlock.resx new file mode 100644 index 0000000..df8339b --- /dev/null +++ b/CleanMachineHMI/FrmUserControl/IOBlock.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file diff --git a/CleanMachineHMI/FrmUserControl/frmIOMonitor.cs b/CleanMachineHMI/FrmUserControl/frmIOMonitor.cs index 67ba6ca..594b72b 100644 --- a/CleanMachineHMI/FrmUserControl/frmIOMonitor.cs +++ b/CleanMachineHMI/FrmUserControl/frmIOMonitor.cs @@ -16,7 +16,7 @@ using CleanMachine.Model.BAS; namespace CleanMachineHMI.FrmUserControl { - [FormType(EnumFormType.IoMonitor)] + public partial class frmIOMonitor : UserControl { diff --git a/CleanMachineHMI/FrmUserControl/frmIOMonitorAuto.Designer.cs b/CleanMachineHMI/FrmUserControl/frmIOMonitorAuto.Designer.cs new file mode 100644 index 0000000..e07ee35 --- /dev/null +++ b/CleanMachineHMI/FrmUserControl/frmIOMonitorAuto.Designer.cs @@ -0,0 +1,61 @@ + +namespace CleanMachineHMI.FrmUserControl +{ + partial class frmIOMonitorAuto + { + /// + /// 必需的设计器变量。 + /// + private System.ComponentModel.IContainer components = null; + + /// + /// 清理所有正在使用的资源。 + /// + /// 如果应释放托管资源,为 true;否则为 false。 + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region 组件设计器生成的代码 + + /// + /// 设计器支持所需的方法 - 不要修改 + /// 使用代码编辑器修改此方法的内容。 + /// + private void InitializeComponent() + { + this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel(); + this.SuspendLayout(); + // + // flowLayoutPanel1 + // + this.flowLayoutPanel1.AutoScroll = true; + this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill; + this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 0); + this.flowLayoutPanel1.Name = "flowLayoutPanel1"; + this.flowLayoutPanel1.Size = new System.Drawing.Size(1024, 672); + this.flowLayoutPanel1.TabIndex = 0; + // + // frmIOMonitorAuto + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.flowLayoutPanel1); + this.Name = "frmIOMonitorAuto"; + this.RightToLeft = System.Windows.Forms.RightToLeft.No; + this.Size = new System.Drawing.Size(1024, 672); + this.Load += new System.EventHandler(this.frmIOMonitorNew_Load); + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1; + } +} diff --git a/CleanMachineHMI/FrmUserControl/frmIOMonitorAuto.cs b/CleanMachineHMI/FrmUserControl/frmIOMonitorAuto.cs new file mode 100644 index 0000000..6948724 --- /dev/null +++ b/CleanMachineHMI/FrmUserControl/frmIOMonitorAuto.cs @@ -0,0 +1,180 @@ +using CleanMachineHMI.Domain.Enums; +using CleanMachineHMI.Utils; +using Krypton.Toolkit; +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; +using CleanMachine.Model; +using CleanMachine.Model.BAS; + +namespace CleanMachineHMI.FrmUserControl +{ + [FormType(EnumFormType.IoMonitor)] + public partial class frmIOMonitorAuto : UserControl + { + + + + + + public frmIOMonitorAuto() + { + InitializeComponent(); + } + + private void frmIOMonitorNew_Load(object sender, EventArgs e) + { + initControl(); + PlcOperationHelper.Instance.OnShowX_Y_Status += Show_X_Y_Status; + } + + public Dictionary buttons = new Dictionary(); + + + private CircleButton findeControl(PlcItem item) + { + var controlname = item.PlcItemDataType == PlcItemTypeDesc.X ? "cbt_X" + Convert.ToString(Convert.ToInt32(item.PlcItemPriAddress), 8).PadLeft(2, '0') : + "cbt_Y" + Convert.ToString(Convert.ToInt32(item.PlcItemPriAddress), 8).PadLeft(2, '0'); + var controls = this.Controls.Find(controlname, true); + return controls.Count() > 0 ? controls[0] as CircleButton : null; + + } + + private void Show_X_Y_Status(IEnumerable items) + { + + + foreach (var item in items) + { + if (buttons.TryGetValue(item, out var btn)) + { + if (!string.IsNullOrEmpty(item.PlcItemValue)) + { + btn.Value = bool.Parse(item.PlcItemValue.ToString()); + } + + + //btn = findeControl(item); + //buttons.Add(item, btn); + } + + } + + } + + + + //private void Show_X_Y_Status(IEnumerable items) + //{ + + // BeginInvoke(new Action(() => + // { + // foreach (var item in items) + // { + // if (item.PlcItemDataType == 2) + // { + // if (this.Controls.Find("cbt_X" + Convert.ToString(Convert.ToInt32(item.PlcItemPriAddress), 8).PadLeft(2, '0'), true).Length > 0) + // { + // CircleButton ctn = this.Controls.Find("cbt_X" + Convert.ToString(Convert.ToInt32(item.PlcItemPriAddress), 8).PadLeft(2, '0'), true)[0] as CircleButton; + // if (string.IsNullOrEmpty(item.PlcItemValue)) + // { + // ctn.BackColor = Color.FromArgb(191, 219, 255); + // } + // else if (bool.Parse(item.PlcItemValue.ToString())) + // { + // ctn.BackColor = Color.Lime; + // } + // else + // { + // ctn.BackColor = Color.Red; + // } + // } + // } + // else + // { + // if (this.Controls.Find("cbt_Y" + Convert.ToString(Convert.ToInt32(item.PlcItemPriAddress), 8).PadLeft(2, '0'), true).Length > 0) + // { + // CircleButton ctn = this.Controls.Find("cbt_Y" + Convert.ToString(Convert.ToInt32(item.PlcItemPriAddress), 8).PadLeft(2, '0'), true)[0] as CircleButton; + // if (string.IsNullOrEmpty(item.PlcItemValue)) + // { + // ctn.BackColor = Color.FromArgb(191, 219, 255); + // } + // else if (bool.Parse(item.PlcItemValue.ToString())) + // { + // ctn.BackColor = Color.Lime; + // } + // else + // { + // ctn.BackColor = Color.Red; + // } + // } + // } + // } + // })); + //} + private void initControl() + { + + try + { + if (PlcOperationHelper.Instance.PlcItemMd_X_List != null && PlcOperationHelper.Instance.PlcItemMd_X_List.Count > 0) + { + List tempLst = PlcOperationHelper.Instance.PlcItemMd_X_List.OrderBy(x => Convert.ToInt32(x.PlcItemPriAddress)).ToList(); + foreach (PlcItem item in tempLst) + { + var IO = new IOBlock(); + IO.Code = "X" + (int.Parse( item.PlcItemPriAddress)+1).ToString().PadLeft(2, '0'); + IO.IOName = this.T(item.PlcItemName.Replace("(I/O监控)", ""), item.PlcItemCode); + this.flowLayoutPanel1.Controls.Add(IO); + buttons.Add(item, IO); + //if (this.Controls.Find("klb_X" + Convert.ToString(Convert.ToInt32(item.PlcItemPriAddress), 8).PadLeft(2, '0'), true).Length > 0) + //{ + // KryptonLabel label1 = this.Controls.Find("klb_X" + Convert.ToString(Convert.ToInt32(item.PlcItemPriAddress), 8).PadLeft(2, '0'), true)[0] as KryptonLabel; + + // label1.Text = this.T(item.PlcItemName.Replace("(I/O监控)", ""), item.PlcItemCode); + //} + + } + } + + if (PlcOperationHelper.Instance.PlcItemMd_Y_List != null && PlcOperationHelper.Instance.PlcItemMd_Y_List.Count > 0) + { + List tempLst = PlcOperationHelper.Instance.PlcItemMd_Y_List.OrderBy(x => Convert.ToInt32(x.PlcItemPriAddress)).ToList(); + foreach (PlcItem item in tempLst) + { + var IO = new IOBlock(); + IO.Code = "Y" + (int.Parse(item.PlcItemPriAddress) + 1).ToString().PadLeft(2,'0'); + IO.IOName = this.T(item.PlcItemName.Replace("(I/O监控)", ""), item.PlcItemCode); + this.flowLayoutPanel1.Controls.Add(IO); + + buttons.Add(item, IO); + + //if (this.Controls.Find("klb_Y" + Convert.ToString(Convert.ToInt32(item.PlcItemPriAddress), 8).PadLeft(2, '0'), true).Length > 0) + //{ + // KryptonLabel label1 = this.Controls.Find("klb_Y" + Convert.ToString(Convert.ToInt32(item.PlcItemPriAddress), 8).PadLeft(2, '0'), true)[0] as KryptonLabel; + + // label1.Text = this.T(item.PlcItemName.Replace("(I/O监控)", ""), item.PlcItemCode); + //} + + } + } + + + Show_X_Y_Status(PlcOperationHelper.Instance.PlcItemMd_X_List); + + Show_X_Y_Status(PlcOperationHelper.Instance.PlcItemMd_Y_List); + } + catch (Exception ex) + { + KryptonMessageBox.Show(ex.Message); + } + } + } +} diff --git a/CleanMachineHMI/FrmUserControl/frmIOMonitorAuto.resx b/CleanMachineHMI/FrmUserControl/frmIOMonitorAuto.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/CleanMachineHMI/FrmUserControl/frmIOMonitorAuto.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/CleanMachineHMI/Utils/PlcOperationHelper.cs b/CleanMachineHMI/Utils/PlcOperationHelper.cs index 1ab371e..91dfff9 100644 --- a/CleanMachineHMI/Utils/PlcOperationHelper.cs +++ b/CleanMachineHMI/Utils/PlcOperationHelper.cs @@ -99,7 +99,7 @@ namespace CleanMachineHMI if (_synchronization != null) { _synchronization.Post(this.PostShowX_Y_Status, plcItem); - // OnShowX_Y_Status?.Invoke(plcItem); + // OnShowX_Y_Status?.Invoke(plcItem); } } @@ -161,13 +161,22 @@ namespace CleanMachineHMI private void RefreshPlcItem() { + Stopwatch stopWatch = new Stopwatch(); while (Global.SysThIsRunFlag) { + + + stopWatch.Restart(); var haschange = false; RefreshPlcItem_M(ref haschange); RefreshPlcItem_D(ref haschange); RefreshPlcItem_X(ref haschange); RefreshPlcItem_Y(ref haschange); + + stopWatch.Stop(); + + TimeSpan ts = stopWatch.Elapsed; + LogHelper.WriteLog($"读取PLC时间:{ts.Milliseconds}", LogTypeEum.Run); if (haschange) { if (_synchronization != null) @@ -175,9 +184,16 @@ namespace CleanMachineHMI _synchronization.Post(this.DoOnReadFinish, null); } } + + // thEvent.Set(); // ShowWarningMsg(); - Thread.Sleep(100); + int sleeptime = 100 - ts.Milliseconds; + if (sleeptime<0) + { + sleeptime = 0; + } + Thread.Sleep(sleeptime); } } @@ -1184,5 +1200,5 @@ namespace CleanMachineHMI - + } diff --git a/CleanMachineHMI/frmMainNew.cs b/CleanMachineHMI/frmMainNew.cs index 4579819..cb585e1 100644 --- a/CleanMachineHMI/frmMainNew.cs +++ b/CleanMachineHMI/frmMainNew.cs @@ -225,11 +225,11 @@ namespace CleanMachineHMI PlcOperationHelper.Instance.StartRead(); - Thread th = new Thread(RefreshPlcItem) - { - IsBackground = true - }; - th.Start(); + //Thread th = new Thread(RefreshPlcItem) + //{ + // IsBackground = true + //}; + //th.Start(); //Thread th = new Thread(RefreshPlcItem_M) @@ -271,7 +271,7 @@ namespace CleanMachineHMI label3Professional.BackColor = Color.Red; isPlcConnStatus = false; - LogHelper.WriteLog($"串口打开失败,PLC连接成功!", LogTypeEum.Run); + LogHelper.WriteLog($"串口打开失败,PLC连接失败!", LogTypeEum.Run); } MothedShowMessage = new dlgShowMessage(Test); diff --git a/NX_LogUtility/LogHelper.cs b/NX_LogUtility/LogHelper.cs index 75a1391..0276e28 100644 --- a/NX_LogUtility/LogHelper.cs +++ b/NX_LogUtility/LogHelper.cs @@ -59,6 +59,7 @@ namespace NX_LogUtility string filePath = strDir2 + fileDate + "_" + logType + ".log"; string DateString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); string Str = "发生时间:" + DateString + "|" + Txt + "\r\n"; + Console.WriteLine(Str); WriteFile(strDir2, filePath, Str); }