From 4bb2a4be804f64e866d93f0a8e0029e3db3edf6a Mon Sep 17 00:00:00 2001 From: wenjy Date: Tue, 5 Mar 2024 13:04:43 +0800 Subject: [PATCH] =?UTF-8?q?change=20-=20Plc=E4=BF=AE=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E8=A5=BF=E9=97=A8=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.config | 2 +- CheckCode.cs | 2 +- Common/PlcHelper.cs | 20 ++++++++-------- DoSomething.cs | 42 ++++++++++++++++---------------- LoginFrom.cs | 55 ++++++++++++++++++++++-------------------- Main.cs | 57 ++++++++++++++++++++++---------------------- Program.cs | 6 ++++- Untils/ConnectPLC.cs | 30 ++++++++++++++++++++--- 8 files changed, 123 insertions(+), 91 deletions(-) diff --git a/App.config b/App.config index 5d22d53..da3163d 100644 --- a/App.config +++ b/App.config @@ -6,7 +6,7 @@ - + diff --git a/CheckCode.cs b/CheckCode.cs index 9ab4002..39fd480 100644 --- a/CheckCode.cs +++ b/CheckCode.cs @@ -353,7 +353,7 @@ namespace ZJ_BYD for (int i = 0; i < responseData.Count; i++) { var val = dt.Rows[0][i] == null ? "0" : (string.IsNullOrWhiteSpace(dt.Rows[0][i].ToString()) ? "0" : dt.Rows[0][i].ToString()); - Program.omronCipNet.Write(responseData[i].Address, float.Parse(val)); + Program.siemensS7Net.Write(responseData[i].Address, float.Parse(val)); resultData += val + ","; if ((i + 1) == int.Parse(len)) { diff --git a/Common/PlcHelper.cs b/Common/PlcHelper.cs index 627f985..62f0ce3 100644 --- a/Common/PlcHelper.cs +++ b/Common/PlcHelper.cs @@ -23,14 +23,14 @@ namespace ZJ_BYD.Common switch (dataType) { case "bool": - if (Program.omronCipNet == null) + if (Program.siemensS7Net == null) { Program.communicationstatus = "异常"; return (false, strVal); } else { - var operateBoolResult = Program.omronCipNet.ReadBool(address); + var operateBoolResult = Program.siemensS7Net.ReadBool(address); if (!operateBoolResult.IsSuccess) { return (false, strVal); @@ -43,14 +43,14 @@ namespace ZJ_BYD.Common } } case "short": - if (Program.omronCipNet == null) + if (Program.siemensS7Net == null) { Program.communicationstatus = "异常"; return (false, strVal); } else { - var operateShortResult = Program.omronCipNet.ReadInt16(address); + var operateShortResult = Program.siemensS7Net.ReadInt16(address); if (!operateShortResult.IsSuccess) { return (false, strVal); @@ -63,14 +63,14 @@ namespace ZJ_BYD.Common } } case "ushort": - if (Program.omronCipNet == null) + if (Program.siemensS7Net == null) { Program.communicationstatus = "异常"; return (false, strVal); } else { - var operateShortResult = Program.omronCipNet.ReadUInt16(address); + var operateShortResult = Program.siemensS7Net.ReadUInt16(address); if (!operateShortResult.IsSuccess) { return (false, strVal); @@ -83,14 +83,14 @@ namespace ZJ_BYD.Common } } case "float": - if (Program.omronCipNet == null) + if (Program.siemensS7Net == null) { Program.communicationstatus = "异常"; return (false, strVal); } else { - var operateFloatResult = Program.omronCipNet.ReadFloat(address); + var operateFloatResult = Program.siemensS7Net.ReadFloat(address); if (!operateFloatResult.IsSuccess) { return (false, strVal); @@ -103,14 +103,14 @@ namespace ZJ_BYD.Common } } case "string": - if (Program.omronCipNet == null) + if (Program.siemensS7Net == null) { Program.communicationstatus = "异常"; return (false, strVal); } else { - var operateFloatResult = Program.omronCipNet.ReadString(address, ushort.Parse(length), Encoding.UTF8); + var operateFloatResult = Program.siemensS7Net.ReadString(address, ushort.Parse(length), Encoding.UTF8); if (!operateFloatResult.IsSuccess) { return (false, strVal); diff --git a/DoSomething.cs b/DoSomething.cs index 98a556c..7488942 100644 --- a/DoSomething.cs +++ b/DoSomething.cs @@ -330,12 +330,12 @@ namespace ZJ_BYD { BarCode = resultBarcode; //打印反馈 - Program.omronCipNet.Write(M17.Address, ushort.Parse("1")); + Program.siemensS7Net.Write(M17.Address, ushort.Parse("1")); } else { //打印反馈 - Program.omronCipNet.Write(M17.Address, ushort.Parse("2")); + Program.siemensS7Net.Write(M17.Address, ushort.Parse("2")); } _logs.Add(new LogVm { @@ -351,7 +351,7 @@ namespace ZJ_BYD var msg = ex == null ? "操作异常" : ex.Message; LogHelper.WriteErrorLog($"标签打印时异常:{msg}"); //打印反馈 - Program.omronCipNet.Write(M17.Address, ushort.Parse("2")); + Program.siemensS7Net.Write(M17.Address, ushort.Parse("2")); } } else @@ -397,17 +397,17 @@ namespace ZJ_BYD Log = $"{DateTime.Now:HH:mm:ss}>>>>>主条码匹配反馈:{(ok ? "OK" : "NG")},详情:{msg}{Environment.NewLine}"; if (ok) { - Program.omronCipNet.Write(M20.Address, ushort.Parse("1")); + Program.siemensS7Net.Write(M20.Address, ushort.Parse("1")); } else { - Program.omronCipNet.Write(M20.Address, ushort.Parse("2")); + Program.siemensS7Net.Write(M20.Address, ushort.Parse("2")); } } catch (Exception ex) { - Program.omronCipNet.Write(M20.Address, ushort.Parse("2")); + Program.siemensS7Net.Write(M20.Address, ushort.Parse("2")); var errMsg = ex == null ? "未知异常" : ex.Message; LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>{errMsg}"); runMainMatch = true; @@ -983,16 +983,16 @@ namespace ZJ_BYD { CheckCode.SearchEmptyTotalWeight(_plcPoints, out var emptyTotalWeight); var M31 = _plcPoints.FirstOrDefault(m => m.Key == "M31"); - var writeOk = Program.omronCipNet.Write(M31.Address, emptyTotalWeight); + var writeOk = Program.siemensS7Net.Write(M31.Address, emptyTotalWeight); LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>一体机启动反馈:获取压缩机总空重成功,数据值:{emptyTotalWeight},写入M31"); var M26 = _plcPoints.FirstOrDefault(m => m.Key == "M26"); if (writeOk.IsSuccess) { - Program.omronCipNet.Write(M26.Address, ushort.Parse("1")); + Program.siemensS7Net.Write(M26.Address, ushort.Parse("1")); } else { - Program.omronCipNet.Write(M26.Address, ushort.Parse("2")); + Program.siemensS7Net.Write(M26.Address, ushort.Parse("2")); } } else @@ -1015,11 +1015,11 @@ namespace ZJ_BYD { if (ok.Value) { - Program.omronCipNet.Write(M26.Address, ushort.Parse("1")); + Program.siemensS7Net.Write(M26.Address, ushort.Parse("1")); } else { - Program.omronCipNet.Write(M26.Address, ushort.Parse("2")); + Program.siemensS7Net.Write(M26.Address, ushort.Parse("2")); } } _logs.Add(new LogVm @@ -1341,7 +1341,7 @@ namespace ZJ_BYD } else { - Program.omronCipNet.Write(M31.Address, weight); + Program.siemensS7Net.Write(M31.Address, weight); LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>一体机启动反馈:获取[{value.Key}]成功,数据值:{value.Value},写入M31"); } } @@ -1350,7 +1350,7 @@ namespace ZJ_BYD { var value = values.keyValues.ElementAt(1); var M32 = _plcPoints.FirstOrDefault(m => m.Key == "M32"); - Program.omronCipNet.Write(M32.Address, float.Parse(value.Value)); + Program.siemensS7Net.Write(M32.Address, float.Parse(value.Value)); LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>一体机启动反馈:获取[{value.Key}]成功,数据值:{value.Value},写入M32"); } } @@ -1369,11 +1369,11 @@ namespace ZJ_BYD var M26 = _plcPoints.FirstOrDefault(m => m.Key == "M26"); if (isCheck) { - Program.omronCipNet.Write(M26.Address, ushort.Parse("1")); + Program.siemensS7Net.Write(M26.Address, ushort.Parse("1")); } else { - Program.omronCipNet.Write(M26.Address, ushort.Parse("2")); + Program.siemensS7Net.Write(M26.Address, ushort.Parse("2")); } } catch (Exception ex) @@ -1543,20 +1543,20 @@ namespace ZJ_BYD { if (isOk) { - var result = Program.omronCipNet.Write(M24.Address, ushort.Parse("1")); + var result = Program.siemensS7Net.Write(M24.Address, ushort.Parse("1")); if (!result.IsSuccess) { - Program.omronCipNet.Write(M24.Address, ushort.Parse("1")); + Program.siemensS7Net.Write(M24.Address, ushort.Parse("1")); } savedb = true; LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>写入PLC地址M24,数据值:1"); } else { - var result = Program.omronCipNet.Write(M24.Address, ushort.Parse("2")); + var result = Program.siemensS7Net.Write(M24.Address, ushort.Parse("2")); if (!result.IsSuccess) { - Program.omronCipNet.Write(M24.Address, ushort.Parse("2")); + Program.siemensS7Net.Write(M24.Address, ushort.Parse("2")); } savedb = true; LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>写入PLC地址M24,数据值:2"); @@ -1579,13 +1579,13 @@ namespace ZJ_BYD { if (isOk) { - Program.omronCipNet.Write(M22.Address, ushort.Parse("1")); + Program.siemensS7Net.Write(M22.Address, ushort.Parse("1")); savedb = true; LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>写入PLC地址M22,数据值:1"); } else { - Program.omronCipNet.Write(M22.Address, ushort.Parse("2")); + Program.siemensS7Net.Write(M22.Address, ushort.Parse("2")); savedb = true; LogHandler($"{DateTime.Now:HH:mm:ss}>>>>>写入PLC地址M22,数据值:2"); } diff --git a/LoginFrom.cs b/LoginFrom.cs index f30261f..17306fa 100644 --- a/LoginFrom.cs +++ b/LoginFrom.cs @@ -210,30 +210,33 @@ namespace ZJ_BYD //调用MES接口 try { - var mesCheck = RequestMes.CheckUser(site_1, txtUserName.Text.Trim(), txtPwd.Text); - if (!mesCheck.ok) - { - LogHelper.WriteLog(mesCheck.msg); - XtraMessageBox.Show(mesCheck.msg); - return; - } - if (Program.omronCipNet != null) + #region Delete By wenjy 2024-03-05 登录时取消调用MES接口 + //var mesCheck = RequestMes.CheckUser(site_1, txtUserName.Text.Trim(), txtPwd.Text); + //if (!mesCheck.ok) + //{ + // LogHelper.WriteLog(mesCheck.msg); + // XtraMessageBox.Show(mesCheck.msg); + // return; + //} + #endregion + + if (Program.siemensS7Net != null) { - foreach (var item in plcPoints) - { - var result = Program.omronCipNet.Write(item.Address, ushort.Parse("1")); - if (!result.IsSuccess) - { - result = Program.omronCipNet.Write(item.Address, ushort.Parse("1")); - if (!result.IsSuccess) - { - XtraMessageBox.Show($"在线模式登录时,[{item.StationCode}]工位用户验证结果写入失败,请重新登录!", "系统提示"); - FaceDeviceHelper.Instance.faceLoginUserInfo.IsLogin = false; - Environment.Exit(0); - } - } - Thread.Sleep(100); - } + //foreach (var item in plcPoints) + //{ + // var result = Program.siemensS7Net.Write(item.Address, ushort.Parse("1")); + // if (!result.IsSuccess) + // { + // result = Program.siemensS7Net.Write(item.Address, ushort.Parse("1")); + // if (!result.IsSuccess) + // { + // XtraMessageBox.Show($"在线模式登录时,[{item.StationCode}]工位用户验证结果写入失败,请重新登录!", "系统提示"); + // FaceDeviceHelper.Instance.faceLoginUserInfo.IsLogin = false; + // Environment.Exit(0); + // } + // } + // Thread.Sleep(100); + //} FaceDeviceHelper.Instance.faceLoginUserInfo.IsLogin = true; } else @@ -255,14 +258,14 @@ namespace ZJ_BYD else { - if (Program.omronCipNet != null) + if (Program.siemensS7Net != null) { foreach (var item in plcPoints) { - var result = Program.omronCipNet.Write(item.Address, ushort.Parse("2")); + var result = Program.siemensS7Net.Write(item.Address, ushort.Parse("2")); if (!result.IsSuccess) { - result = Program.omronCipNet.Write(item.Address, ushort.Parse("2")); + result = Program.siemensS7Net.Write(item.Address, ushort.Parse("2")); if (!result.IsSuccess) { XtraMessageBox.Show($"离线模式登录时,[{item.StationCode}]工位用户验证结果写入失败,请重新登录!", "系统提示"); diff --git a/Main.cs b/Main.cs index ed0669a..3fa2017 100644 --- a/Main.cs +++ b/Main.cs @@ -242,9 +242,9 @@ namespace ZJ_BYD // overWrite = true; // continue; //} - //if (Program.omronCipNet == null) + //if (Program.siemensS7Net == null) //{ - // LogHelper.WriteLog($"心跳监测时,Program.omronCipNet为空,进行重连!"); + // LogHelper.WriteLog($"心跳监测时,Program.siemensS7Net为空,进行重连!"); // SetPlcStatusLbl(lblplcstatus, "PLC通讯状态异常", Color.Red); // overWrite = true; // ConnectPLC(); @@ -256,7 +256,7 @@ namespace ZJ_BYD overWrite = true; continue; } - if (Program.omronCipNet != null) + if (Program.siemensS7Net != null) { for (int i = 0; i < heartPoints.Count; i++) { @@ -281,10 +281,10 @@ namespace ZJ_BYD var val = Program.SysMode ? "1" : "2"; foreach (var loginPlcItem in loginPlcPoints) { - var loginResult = Program.omronCipNet.Write(loginPlcItem.Address, ushort.Parse(val)); + var loginResult = Program.siemensS7Net.Write(loginPlcItem.Address, ushort.Parse(val)); if (!loginResult.IsSuccess) { - result = Program.omronCipNet.Write(loginPlcItem.Address, ushort.Parse(val)); + result = Program.siemensS7Net.Write(loginPlcItem.Address, ushort.Parse(val)); if (!loginResult.IsSuccess) { XtraMessageBox.Show($"离线模式登录时,[{loginPlcItem.StationCode}]工位用户验证结果写入失败,请重新登录!", "系统提示"); @@ -316,7 +316,7 @@ namespace ZJ_BYD OperateResult operateResult = null; Task task = Task.Run(() => { - return Program.omronCipNet.Write(address, ushort.Parse("1")); + return Program.siemensS7Net.Write(address, ushort.Parse("1")); }); task.Wait(); operateResult = task.Result; @@ -330,7 +330,8 @@ namespace ZJ_BYD try { var connectPLC = new ConnectPLC(); - Program.omronCipNet = connectPLC.GetOmronCipNet(); + //Program.siemensS7Net = connectPLC.GetOmronCipNet(); + Program.siemensS7Net = connectPLC.GetSiemensS7Net(); } catch (Exception) { @@ -776,24 +777,24 @@ namespace ZJ_BYD var result = MessageBox.Show("确定要退出系统吗?", "提示", MessageBoxButtons.OKCancel); if (result == DialogResult.OK) { - if (Program.omronCipNet != null) - { - var point = Program.PointKeyValues.FirstOrDefault(m => m.Key == "M1"); - if (point != null) - { - var plcResult = Program.omronCipNet.Write(point.Address, ushort.Parse("0")); - if (!plcResult.IsSuccess) - { - XtraMessageBox.Show(string.Format("退出登录时,[{0}]工位用户验证结果写入失败!", point.StationCode), "系统提示"); - return; - } - } - else - { - XtraMessageBox.Show(string.Format("退出登录时,[{0}]工位M1点位未配置,用户验证结果写入失败!", point.StationCode), "系统提示"); - return; - } - } + //if (Program.siemensS7Net != null) + //{ + // var point = Program.PointKeyValues.FirstOrDefault(m => m.Key == "M1"); + // if (point != null) + // { + // var plcResult = Program.siemensS7Net.Write(point.Address, ushort.Parse("0")); + // if (!plcResult.IsSuccess) + // { + // XtraMessageBox.Show(string.Format("退出登录时,[{0}]工位用户验证结果写入失败!", point.StationCode), "系统提示"); + // return; + // } + // } + // else + // { + // XtraMessageBox.Show(string.Format("退出登录时,[{0}]工位M1点位未配置,用户验证结果写入失败!", point.StationCode), "系统提示"); + // return; + // } + //} //设置用户登录状态 FaceDeviceHelper.Instance.faceLoginUserInfo.IsLogin = false; FaceDeviceHelper.Instance.faceLoginUserInfo.IsSuccess = false; @@ -957,7 +958,7 @@ namespace ZJ_BYD } var M4 = Program.PointKeyValues.FirstOrDefault(m => m.StationCode == station && m.Key == "M4"); var M6 = Program.PointKeyValues.FirstOrDefault(m => m.StationCode == station && m.Key == "M6"); - var writeResult = Program.omronCipNet.Write(M4.Address, ushort.Parse(machineSortIndex)); + var writeResult = Program.siemensS7Net.Write(M4.Address, ushort.Parse(machineSortIndex)); if (!writeResult.IsSuccess) { LogHelper.WriteLog($"{Program.ActiveStatinCode}切换机型失败"); @@ -967,7 +968,7 @@ namespace ZJ_BYD isLoad = true; return; } - writeResult = Program.omronCipNet.Write(M6.Address, true); + writeResult = Program.siemensS7Net.Write(M6.Address, true); if (!writeResult.IsSuccess) { LogHelper.WriteLog($"{Program.ActiveStatinCode}切换机型失败"); @@ -978,7 +979,7 @@ namespace ZJ_BYD return; } Thread.Sleep(500); - writeResult = Program.omronCipNet.Write(M6.Address, false); + writeResult = Program.siemensS7Net.Write(M6.Address, false); if (!writeResult.IsSuccess) { LogHelper.WriteLog($"{Program.ActiveStatinCode}切换机型失败"); diff --git a/Program.cs b/Program.cs index bb86766..40ef48d 100644 --- a/Program.cs +++ b/Program.cs @@ -1,5 +1,6 @@ using DevExpress.XtraEditors; using HslCommunication.Profinet.Omron; +using HslCommunication.Profinet.Siemens; using MQTTnet.Client; using SqlSugar; using System; @@ -111,7 +112,8 @@ namespace ZJ_BYD #region 获取连接PLC对象 var connectPLC = new ConnectPLC(); - Program.omronCipNet = connectPLC.GetOmronCipNet(); + //Program.siemensS7Net = connectPLC.GetOmronCipNet(); + Program.siemensS7Net = connectPLC.GetSiemensS7Net(); #endregion #region 定时任务 @@ -277,6 +279,8 @@ namespace ZJ_BYD /// public static OmronCipNet omronCipNet = null; + public static SiemensS7Net siemensS7Net = null; + /// /// 测试文件名称 /// diff --git a/Untils/ConnectPLC.cs b/Untils/ConnectPLC.cs index bbe2d64..3c25123 100644 --- a/Untils/ConnectPLC.cs +++ b/Untils/ConnectPLC.cs @@ -1,4 +1,6 @@ -using HslCommunication.Profinet.Omron; +using HslCommunication; +using HslCommunication.Profinet.Omron; +using HslCommunication.Profinet.Siemens; namespace ZJ_BYD.Untils { @@ -6,6 +8,9 @@ namespace ZJ_BYD.Untils { private OmronCipNet omronCipNet = null; + private SiemensS7Net siemensS7Net = null; + + public ConnectPLC(string plcIp = "", int plcPort = 0) { if (string.IsNullOrWhiteSpace(plcIp)) @@ -16,10 +21,14 @@ namespace ZJ_BYD.Untils { plcPort = Program.PlcPort; } - omronCipNet = new OmronCipNet(plcIp, plcPort) + /*omronCipNet = new OmronCipNet(plcIp, plcPort) { ConnectTimeOut = 3000 - }; + };*/ + + siemensS7Net = new SiemensS7Net(SiemensPLCS.S1500); + siemensS7Net.IpAddress = plcIp; + siemensS7Net.Port = plcPort; } public OmronCipNet GetOmronCipNet() @@ -36,5 +45,20 @@ namespace ZJ_BYD.Untils } } + + public SiemensS7Net GetSiemensS7Net() + { + OperateResult connect = siemensS7Net.ConnectServer(); + if (connect.IsSuccess) + { + return siemensS7Net; + } + else + { + LogHelper.WriteLog("西门子PLC连接失败!"); + return null; + } + + } } }