From fe442ab8c9ba6648b140f253902b4ef24c5618bf Mon Sep 17 00:00:00 2001 From: lss <Lss@HanInfo> Date: 星期二, 20 五月 2025 18:26:31 +0800 Subject: [PATCH] 合肥佳通变更 --- HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/S7Helper.cs | 110 ++++++++++++++++++++++++++++++++----------------------- 1 files changed, 64 insertions(+), 46 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 fd14ebc..7842918 100644 --- a/HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/S7Helper.cs +++ b/HH.WCS.Mobox3/HH.WCS.Mobox3.JiaTong/device/S7Helper.cs @@ -8,6 +8,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; using System.Web.Services.Description; using System.Web.UI.WebControls.WebParts; @@ -86,6 +87,8 @@ //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(); + /// <summary> /// 鍚堣偉浣抽�璇诲彇鍙犵洏鏈� /// </summary> @@ -95,34 +98,38 @@ public static int ReadDpj(int startByte, S7.Net.VarType varType) { string ip = "10.68.9.15"; short port = 102; - try + lock (_lockdpj) { - 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) + try { - 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()); + 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; } - else//鍚堣偉浣抽�闄や簡鏁伴噺锛屽叾浠栭兘鏄痓yte绫诲瀷 + catch (Exception ex) { - - byte value = (byte)plc.Read(DataType.DataBlock, 1, startByte, VarType.Byte, 1); - LogHelper.Info($"閾炬帴鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port},璇诲彇绫诲瀷{varType},璇诲彇鍊納value}"); - result = value; + LogHelper.Error($"ReadDpj Error:{ex}", ex); + throw; } - - LogHelper.Info($"璇诲彇鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port},鍦板潃锛歿startByte},璇诲彇鍊艰浆鎹�{result}"); - return result; } - catch (Exception ex) - { - LogHelper.Error($"ReadDpj Error:{ex}",ex); - throw; - } + } @@ -132,37 +139,48 @@ /// <param name="startByte">鍋忕Щ閲忓湴鍧�/param> /// <param name="value">鍐欏叆鍊�byte绫诲瀷鍙兘0 1</param> /// <returns></returns> - public static bool WriteDpj(int startByte, int value) + public static bool WriteDpj(int startByte, byte value) { bool result = false; string ip = "10.68.9.15"; short port = 102; - try + lock (_lockdpj) { - var plc = new Plc(CpuType.S7200Smart, ip, port, 0, 0); - - LogHelper.Info($"閾炬帴鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port}"); - S7Helper.Link(plc); - plc.Write( - dataType: DataType.DataBlock, - db: 1, - startByteAdr: startByte, - value: value - ); - LogHelper.Info($"鍐欏叆鍙犵洏鏈猴紝ip锛歿ip}锛岀鍙o細{port},鍦板潃锛歿startByte},鍐欏叆鍊�{value}"); - byte[] readBack = plc.ReadBytes(DataType.DataBlock, 1, startByte, 1); - if (readBack[0] == value) + try { - result = true; - LogHelper.Info($"鍙犵洏鏈哄啓鍏ユ垚鍔燂紝鍥炶鍊硷細{readBack[0]}"); - } + var plc = new Plc(CpuType.S7200Smart, ip, port, 0, 0); - return result; + 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($"WriteDpj Error{ex}"); + throw; + } } - catch (Exception ex) - { - LogHelper.Info($"WriteDpj Error{ex}"); - throw; - } + // S7-200 Smart鐨刅鍖轰篃鏄犲皠涓篋B1 -- Gitblit v1.9.1