From 1319cb7072623dff12369a889af766db2d166def Mon Sep 17 00:00:00 2001 From: lss <2538410689@qq.com> Date: 星期一, 28 七月 2025 17:58:47 +0800 Subject: [PATCH] 叠盘机优化 --- HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/S7Helper.cs | 186 ++++++++++++++++++++++++---------------------- 1 files changed, 98 insertions(+), 88 deletions(-) diff --git a/HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/S7Helper.cs b/HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/S7Helper.cs index a7854c1..15d72f4 100644 --- a/HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/S7Helper.cs +++ b/HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/S7Helper.cs @@ -24,26 +24,26 @@ private static bool debug = true; private static S7.Net.Plc plc = null; - static S7Helper() - { - Init(); - } + //static S7Helper() + //{ + // Init(); + //} private static Dictionary<string, Plc> plcDic = new Dictionary<string, Plc>(); - private static void Init() - { - //閰嶇疆鏂囦欢璇诲彇鎵�湁鐨刾lc杩涜鍒濆鍖�- try - { - var plc1 = new Plc(CpuType.S71500, "", 0, 1); - plcDic.Add("plc1", plc1); - Link(plc1); - } - catch (Exception ex) - { + //private static void Init() + //{ + // //閰嶇疆鏂囦欢璇诲彇鎵�湁鐨刾lc杩涜鍒濆鍖�+ // try + // { + // var plc1 = new Plc(CpuType.S71500, "", 0, 1); + // plcDic.Add("plc1", plc1); + // Link(plc1); + // } + // catch (Exception ex) + // { - Console.WriteLine("S7Helper Init err=" + ex.Message); - } - } + // Console.WriteLine("S7Helper Init err=" + ex.Message); + // } + //} private static Plc GetPlc(string plc) { if (plcDic.ContainsKey(plc)) @@ -99,40 +99,51 @@ { // string ip = "10.68.9.15"; short port = 102; - lock (_lockdpj) + + try { - try - { - var plc = new Plc(CpuType.S7200Smart, ip, port, 0, 0); - LogHelper.Info($"閾炬帴鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port}"); - S7Helper.Link(plc); - int result = 0; - if (varType == VarType.Int) - { - var value = (short)plc.Read(DataType.DataBlock, 1, startByte, VarType.Int, 1); - LogHelper.Info($"閾炬帴鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port},璇诲彇绫诲瀷{varType},璇诲彇鍊納value}"); - result = int.Parse(value.ToString()); - } - else//鍚堣偉浣抽�闄や簡鏁伴噺锛屽叾浠栭兘鏄痓yte绫诲瀷 - { - byte value = (byte)plc.Read(DataType.DataBlock, 1, startByte, VarType.Byte, 1); - LogHelper.Info($"閾炬帴鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port},璇诲彇绫诲瀷{varType},璇诲彇鍊納value}"); - result = value; - } - - LogHelper.Info($"璇诲彇鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port},鍦板潃锛歿startByte},璇诲彇鍊艰浆鎹�{result}"); - return result; - } - catch (Exception ex) + var plc = new Plc(CpuType.S7200Smart, ip, port, 0, 1); + if (plc.IsConnected == true) { - LogHelper.Info($"鍙犵洏鏈�{ip}閾炬帴澶辫触"); - LogHelper.Error($"ReadDpj ip:{ip},startByte:{startByte} Error:{ex}", ex); - throw; + LogHelper.Info($"璇ヨ澶囨鍦ㄥ鐞嗗叾浠栦氦浜抂{ip}]"); + throw new Exception("PLC鍗忚灞傝繛鎺ュけ璐�); } + LogHelper.Info($"閾炬帴鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port}"); + S7Helper.Link(plc); + int result = 0; + // 楠岃瘉杩炴帴鐘舵� + if (plc.IsConnected != true) + { + LogHelper.Info($"鐗╃悊杩炴帴鎴愬姛浣嗗崗璁眰鏈氨缁猍{ip}]"); + throw new Exception("PLC鍗忚灞傝繛鎺ュけ璐�); + } + + if (varType == VarType.Int) + { + var value = (short)plc.Read(DataType.DataBlock, 1, startByte, VarType.Int, 1); + LogHelper.Info($"閾炬帴鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port},璇诲彇绫诲瀷{varType},璇诲彇鍊納value}"); + result = int.Parse(value.ToString()); + } + else//鍚堣偉浣抽�闄や簡鏁伴噺锛屽叾浠栭兘鏄痓yte绫诲瀷 + { + + byte value = (byte)plc.Read(DataType.DataBlock, 1, startByte, VarType.Byte, 1); + LogHelper.Info($"閾炬帴鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port},璇诲彇绫诲瀷{varType},璇诲彇鍊納value}"); + result = value; + } + plc.Close(); + LogHelper.Info($"璇诲彇鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port},鍦板潃锛歿startByte},璇诲彇鍊艰浆鎹�{result}"); + + return result; } - - + catch (Exception ex) + { + plc.Close(); + LogHelper.Info($"鍙犵洏鏈�{ip}閾炬帴澶辫触"); + LogHelper.Error($"ReadDpj ip:{ip},startByte:{startByte} Error:{ex}", ex); + throw; + } } /// <summary> @@ -146,53 +157,52 @@ bool result = false; // string ip = "10.68.9.15"; short port = 102; - lock (_lockdpj) + + try { - try - { - var plc = new Plc(CpuType.S7200Smart, ip, port, 0, 0); + var plc = new Plc(CpuType.S7200Smart, ip, port, 0, 1); - LogHelper.Info($"閾炬帴鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port}"); - S7Helper.Link(plc); - // plc.Write($"VB{startByte}", value); - Thread.Sleep(300); - plc.Write( - dataType: DataType.DataBlock, - db: 1, - startByteAdr: startByte, - value: value - ); - LogHelper.Info($"鍐欏叆鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port},鍦板潃锛歿startByte},鍐欏叆鍊�{value}"); - Thread.Sleep(300); - byte fdvalue = (byte)plc.Read(DataType.DataBlock, 1, startByte, VarType.Byte, 1); - if (fdvalue == value) - { - result = true; - LogHelper.Info($"鍙犵洏鏈哄啓鍏ユ垚鍔燂紝鍥炶鍊硷細{fdvalue}"); - } - else - { - // Task.Run(() => WriteDpj(ip, startByte, value)); - LogHelper.Info($"鍙犵洏鏈哄啓鍏ュけ璐ワ紝鍥炶鍊硷細{fdvalue}"); - } - - return result; - } - catch (Exception ex) + LogHelper.Info($"閾炬帴鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port}"); + S7Helper.Link(plc); + if (plc.IsConnected != true) { - // Task.Run(() => WriteDpj(ip, startByte, value)); - LogHelper.Info($"鍙犵洏鏈�{ip}閾炬帴澶辫触"); - LogHelper.Error($"WriteDpj ip:{ip},startByte:{startByte} Error:{ex}", ex); - return false; + LogHelper.Info($"鐗╃悊杩炴帴鎴愬姛浣嗗崗璁眰鏈氨缁猍{ip}]"); + throw new Exception("PLC鍗忚灞傝繛鎺ュけ璐�); } + // plc.Write($"VB{startByte}", value); + Thread.Sleep(300); + plc.Write( + dataType: DataType.DataBlock, + db: 1, + startByteAdr: startByte, + value: value + ); + + LogHelper.Info($"鍐欏叆鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port},鍦板潃锛歿startByte},鍐欏叆鍊�{value}"); + Thread.Sleep(300); + byte fdvalue = (byte)plc.Read(DataType.DataBlock, 1, startByte, VarType.Byte, 1); + if (fdvalue == value) + { + result = true; + LogHelper.Info($"鍙犵洏鏈哄啓鍏ユ垚鍔燂紝鍥炶鍊硷細{fdvalue}"); + } + else + { + // Task.Run(() => WriteDpj(ip, startByte, value)); + LogHelper.Info($"鍙犵洏鏈哄啓鍏ュけ璐ワ紝鍥炶鍊硷細{fdvalue}"); + } + plc.Close(); + return result; } - - - + catch (Exception ex) + { + plc.Close(); + // Task.Run(() => WriteDpj(ip, startByte, value)); + LogHelper.Info($"鍙犵洏鏈�{ip}閾炬帴澶辫触"); + LogHelper.Error($"WriteDpj ip:{ip},startByte:{startByte} Error:{ex}", ex); + return false; + } // S7-200 Smart鐨刅鍖轰篃鏄犲皠涓篋B1 - - - } internal static short[] ReadInt(string device, int db, int byteAddr, int count) { -- Gitblit v1.9.1