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 | 198 ++++++++++++++++++++++++++----------------------- 1 files changed, 106 insertions(+), 92 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 4ce8f34..15d72f4 100644 --- a/HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/S7Helper.cs +++ b/HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/S7Helper.cs @@ -23,27 +23,27 @@ { private static bool debug = true; private static S7.Net.Plc plc = null; - - 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) - { - Console.WriteLine("S7Helper Init err=" + ex.Message); - } - } + //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) + // { + + // Console.WriteLine("S7Helper Init err=" + ex.Message); + // } + //} private static Plc GetPlc(string plc) { if (plcDic.ContainsKey(plc)) @@ -79,7 +79,7 @@ catch (Exception ex) { // Console.WriteLine($"plc{plc.IP}杩炴帴澶辫触锛宔rr={ex.Message}"); - LogHelper.Error($"Link Error plc{plc.IP}杩炴帴澶辫触锛宔rr={ex.Message}",ex); + LogHelper.Error($"Link Error plc{plc.IP}杩炴帴澶辫触锛宔rr={ex.Message}", ex); //Init(); } @@ -87,7 +87,7 @@ //https://www.ad.siemens.com.cn/productportal/Prods/S7-1200_PLC_EASY_PLUS/SmartSMS/060.html //https://www.ad.siemens.com.cn/productportal/Prods/S7-1200_PLC_EASY_PLUS/07-Program/02-basic/01-Data_Type/09-String.html - private static object _lockdpj=new object(); + private static object _lockdpj = new object(); /// <summary> /// 鍚堣偉浣抽�璇诲彇鍙犵洏鏈�@@ -95,43 +95,55 @@ /// <param name="startByte">鍋忕Щ閲忓湴鍧�vb2001</param> /// /// <param name="varType">鍊肩被鍨�/param> /// <returns></returns> - public static int ReadDpj(string ip,int startByte, S7.Net.VarType varType) + public static int ReadDpj(string ip, int startByte, S7.Net.VarType varType) { - // string ip = "10.68.9.15"; + // 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.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> @@ -140,55 +152,57 @@ /// <param name="startByte">鍋忕Щ閲忓湴鍧�/param> /// <param name="value">鍐欏叆鍊�byte绫诲瀷鍙兘0 1</param> /// <returns></returns> - public static bool WriteDpj(string ip,int startByte, byte value) + public static bool WriteDpj(string ip, int startByte, byte value) { bool result = false; - // string ip = "10.68.9.15"; + // 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 - { - LogHelper.Info($"鍙犵洏鏈哄啓鍏ュけ璐ワ紝鍥炶鍊硷細{fdvalue}"); - } - - return result; - } - catch (Exception ex) + LogHelper.Info($"閾炬帴鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port}"); + S7Helper.Link(plc); + if (plc.IsConnected != true) { - LogHelper.Info($"WriteDpj Error{ex}"); - throw; + 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