From f319fd5d5e5e0332c4c7e209df64c351dfbe6887 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期六, 25 四月 2026 22:13:19 +0800
Subject: [PATCH] fix: 修复PLC通信及任务处理中的多个问题
---
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs | 106 +++++++++++++++++++++++++++++++++++++----------------
1 files changed, 74 insertions(+), 32 deletions(-)
diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs
index cdfdee9..c2dfc11 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs
@@ -1,4 +1,5 @@
锘�#region << 鐗� 鏈� 娉� 閲� >>
+
/*----------------------------------------------------------------
* 鍛藉悕绌洪棿锛歐IDESEAWCS_Communicator
* 鍒涘缓鑰咃細鑳$搴�
@@ -11,8 +12,9 @@
* 淇敼鏃堕棿锛�
* 鐗堟湰锛歏1.0.1
* 淇敼璇存槑锛�
- *
+ *
*----------------------------------------------------------------*/
+
#endregion << 鐗� 鏈� 娉� 閲� >>
using HslCommunication;
@@ -20,19 +22,12 @@
using HslCommunication.LogNet;
using HslCommunication.Profinet.Siemens;
using Newtonsoft.Json;
-using Newtonsoft.Json.Linq;
-using System;
-using System.Collections;
-using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
-using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Text;
-using System.Threading.Tasks;
-using System.Xml.Linq;
namespace WIDESEAWCS_Communicator
{
@@ -43,6 +38,7 @@
public class SiemensS7 : BaseCommunicator
{
#region Private Member
+
/// <summary>
/// HSLCommunication鐨勮タ闂ㄥ瓙鐨凷7鍗忚鐨勯�氳绫�
/// </summary>
@@ -59,7 +55,7 @@
private int _port;
/// <summary>
- /// 褰撳墠閫氳鍣ㄦ槸鍚﹀凡杩炴帴鍒癙LC銆�
+ /// 褰撳墠閫氳鍣ㄦ槸鍚﹀凡杩炴帴鍒癙LC銆�
/// </summary>
private bool _connected;
@@ -71,11 +67,13 @@
private ILogNet _logNet;
private bool _isPing = true;
+
#endregion Private Member
#region Public Member
- /// <summary>
- /// 鑾峰彇褰撳墠閫氳鍣ㄦ槸鍚﹀凡杩炴帴鍒癙LC銆�
+
+ /// <summary>
+ /// 鑾峰彇褰撳墠閫氳鍣ㄦ槸鍚﹀凡杩炴帴鍒癙LC銆�
/// </summary>
public override bool IsConnected => _connected;
@@ -92,10 +90,12 @@
/// <summary>
/// 鏄惁鍦ㄥ啓鍏ユ暟鎹悗璇诲彇鏁版嵁纭銆�
/// </summary>
- public override bool IsReadAfterWrite { get; set; } = false;
+ public override bool IsReadAfterWrite { get; set; } = true;
+
#endregion Public Member
#region Constructor Function
+
/// <summary>
/// 鏋勯�犲嚱鏁�
/// </summary>
@@ -118,9 +118,11 @@
_port = port;//閫氳繃鏋勯�犲嚱鏁拌祴鍊艰繛鎺ヤ娇鐢ㄧ殑绔彛鍙�
_name = name;
}
+
#endregion
#region Private Method
+
/// <summary>
/// 浠嶰perateResult瀵硅薄涓幏鍙栬鍙栫殑鏁版嵁銆�
/// </summary>
@@ -150,7 +152,7 @@
}
/// <summary>
- ///
+ ///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="operateResult"></param>
@@ -221,25 +223,32 @@
try
{
Type type = value.GetType();
-
switch (Type.GetTypeCode(type))
{
case TypeCode.Int32:
return plc.Write(address, Convert.ToInt32(value));
+
case TypeCode.UInt32:
return plc.Write(address, Convert.ToUInt32(value));
+
case TypeCode.Int16:
return plc.Write(address, Convert.ToInt16(value));
+
case TypeCode.UInt16:
return plc.Write(address, Convert.ToUInt16(value));
+
case TypeCode.Single:
return plc.Write(address, Convert.ToSingle(value));
+
case TypeCode.Boolean:
return plc.Write(address, Convert.ToBoolean(value));
+
case TypeCode.Byte:
return plc.Write(address, Convert.ToByte(value));
+
case TypeCode.String:
return plc.Write(address, Convert.ToString(value));
+
case TypeCode.Char:
return plc.Write(address, Convert.ToChar(value));
//case TypeCode.arr:
@@ -302,44 +311,53 @@
return (int[])GetContent(plc.ReadInt32(address, length), address);
else
return (int)GetContent(plc.ReadInt32(address), address);
+
case TypeCode.UInt32:
if (length > 1)
return (uint[])GetContent(plc.ReadUInt32(address, length), address);
else
return (uint)GetContent(plc.ReadUInt32(address), address);
+
case TypeCode.Int16:
if (length > 1)
return (short[])GetContent(plc.ReadInt16(address, length), address);
else
return (short)GetContent(plc.ReadInt16(address), address);
+
case TypeCode.UInt16:
if (length > 1)
return (ushort[])GetContent(plc.ReadUInt16(address, length), address);
else
return (ushort)GetContent(plc.ReadUInt16(address), address);
+
case TypeCode.Single:
if (length > 1)
return (float[])GetContent(plc.ReadFloat(address, length), address);
else
return (float)GetContent(plc.ReadFloat(address), address);
+
case TypeCode.Boolean:
if (length > 1)
return (bool[])GetContent(plc.ReadBool(address, length), address);
else
return (bool)GetContent(plc.ReadBool(address), address);
+
case TypeCode.Byte:
if (length > 1)
return (byte)GetContent(plc.Read(address, length), address);
return (byte)GetContent(plc.ReadByte(address), address);
+
case TypeCode.String:
if (length > 1)
return (string)GetContent(plc.ReadString(address, length), address);
else
return (string)GetContent(plc.ReadString(address), address);
+
case TypeCode.Char:
if (length > 1)
return Encoding.Default.GetString((byte[])GetContent(plc.Read(address, length), address)).ToArray();
return (char)GetContent(plc.ReadByte(address), address);
+
default:
throw new CommunicationException(string.Format(CommunicationExceptionMessage.DataTypeErrorException, typeCode.ToString(), address), CommunicationErrorType.TypeError);
}
@@ -376,14 +394,15 @@
}
}
});
-
}
+
#endregion
#region Public Method
- /// <summary>
+
+ /// <summary>
/// 杩炴帴鍒癙LC銆�
- /// </summary>
+ /// </summary>
/// <returns>濡傛灉杩炴帴鎴愬姛鍒欒繑鍥瀟rue锛屽惁鍒欒繑鍥瀎alse銆�</returns>
/// <exception cref="CommunicationException">鑷畾涔夐�氳寮傚父绫�</exception>
public override bool Connect()
@@ -416,10 +435,10 @@
}
}
- /// <summary>
- /// 鏂紑涓庡伐涓氳澶囩殑杩炴帴銆�
- /// </summary>
- /// <returns>濡傛灉鎴愬姛鏂紑杩炴帴鍒欒繑鍥瀟rue锛屽鏋滃凡缁忔槸鏂紑鐘舵�佸垯杩斿洖false銆�</returns>
+ /// <summary>
+ /// 鏂紑涓庡伐涓氳澶囩殑杩炴帴銆�
+ /// </summary>
+ /// <returns>濡傛灉鎴愬姛鏂紑杩炴帴鍒欒繑鍥瀟rue锛屽鏋滃凡缁忔槸鏂紑鐘舵�佸垯杩斿洖false銆�</returns>
public override bool Disconnect()
{
try
@@ -442,12 +461,13 @@
}
#region Read
- /// <summary>
- /// 浠嶱LC璇诲彇鏁版嵁銆�
- /// </summary>
- /// <param name="address">婧愬湴鍧�锛屽叿浣撴牸寮忓彇鍐充簬浣跨敤鐨勫伐涓氬崗璁��</param>
- /// <param name="length">瑕佽鍙栫殑鏁版嵁闀垮害銆�</param>
- /// <returns>璇诲彇鍒扮殑鏁版嵁锛屽鏋滆鍙栧け璐ュ垯鍙兘杩斿洖null銆佺┖鏁扮粍鎴栨姏鍑鸿嚜瀹氫箟閫氳寮傚父銆�</returns>
+
+ /// <summary>
+ /// 浠嶱LC璇诲彇鏁版嵁銆�
+ /// </summary>
+ /// <param name="address">婧愬湴鍧�锛屽叿浣撴牸寮忓彇鍐充簬浣跨敤鐨勫伐涓氬崗璁��</param>
+ /// <param name="length">瑕佽鍙栫殑鏁版嵁闀垮害銆�</param>
+ /// <returns>璇诲彇鍒扮殑鏁版嵁锛屽鏋滆鍙栧け璐ュ垯鍙兘杩斿洖null銆佺┖鏁扮粍鎴栨姏鍑鸿嚜瀹氫箟閫氳寮傚父銆�</returns>
/// <exception cref="CommunicationException">鑷畾涔夐�氳寮傚父绫�</exception>
public override byte[] Read(string address, int length)
{
@@ -469,7 +489,7 @@
}
/// <summary>
- ///
+ ///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="address"></param>
@@ -495,9 +515,11 @@
else
return Read(address, SiemensDBDataType.GetTypeCode(dataType));
}
+
#endregion
#region Write
+
/// <summary>
/// 鍚慞LC鍐欏叆鏁版嵁銆�
/// </summary>
@@ -528,7 +550,7 @@
}
/// <summary>
- ///
+ ///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="address"></param>
@@ -539,7 +561,6 @@
{
return GetResult(Write(address, value), address, value);
}
-
public override bool Write<T>(string address, T[] values)
{
@@ -573,6 +594,7 @@
obj = GetResult(Write(address, writeVal), address, writeVal);
}
break;
+
case SiemensDBDataType.DataType_DW:
{
uint writeVal;
@@ -587,6 +609,7 @@
obj = GetResult(Write(address, writeVal), address, writeVal);
}
break;
+
case SiemensDBDataType.DataType_Int:
{
short writeVal;
@@ -601,6 +624,7 @@
obj = GetResult(Write(address, writeVal), address, writeVal);
}
break;
+
case SiemensDBDataType.DataType_W:
{
ushort writeVal;
@@ -615,6 +639,7 @@
obj = GetResult(Write(address, writeVal), address, writeVal);
}
break;
+
case SiemensDBDataType.DataType_Float:
{
float writeVal;
@@ -629,6 +654,7 @@
obj = GetResult(Write(address, writeVal), address, writeVal);
}
break;
+
case SiemensDBDataType.DataType_Bool:
{
bool writeVal;
@@ -643,6 +669,7 @@
obj = GetResult(Write(address, writeVal), address, writeVal);
}
break;
+
case SiemensDBDataType.DataType_Byte:
{
byte writeVal;
@@ -657,6 +684,7 @@
obj = GetResult(Write(address, writeVal), address, writeVal);
}
break;
+
case SiemensDBDataType.DataType_String:
{
string writeVal;
@@ -672,6 +700,7 @@
}
break;
+
case SiemensDBDataType.DataType_Char:
break;
@@ -690,14 +719,17 @@
obj = GetResult(Write(address, writeVal), address, writeVal);
}
break;
+
default:
throw new CommunicationException(string.Format(CommunicationExceptionMessage.DataTypeErrorException, dataType, address), CommunicationErrorType.TypeError);
}
return obj;
}
+
#endregion
#region ReadCustomer
+
/// <summary>
/// 璇诲彇鑷畾涔夌殑鏁版嵁绫诲瀷锛岄渶瑕佺户鎵胯嚜IDataTransfer鎺ュ彛锛岃繑鍥炰竴涓柊鐨勭被鍨嬬殑瀹炰緥瀵硅薄銆�
/// </summary>
@@ -716,9 +748,11 @@
throw new CommunicationException(ex.Message, CommunicationErrorType.ReadException, innerException: ex);
}
}
+
#endregion
#region WriteCustomer
+
/// <summary>
/// 鍐欏叆鑷畾涔夌被鍨嬬殑鏁版嵁锛岃绫诲瀷蹇呴』缁ф壙鑷狪DataTransfer鎺ュ彛銆�
/// </summary>
@@ -785,6 +819,7 @@
LogNet.WriteInfo(Name, stringBuilder.ToString());
}
}
+
#endregion
/// <summary>
@@ -829,29 +864,36 @@
case TypeCode.Int16:
OperateResult<TimeSpan> operateResultShort = plc.Wait(address, Convert.ToInt16(value), readInterval, waitTimeout);
return operateResultShort;
+
case TypeCode.Int32:
OperateResult<TimeSpan> operateResultInt = plc.Wait(address, Convert.ToInt16(value), readInterval, waitTimeout);
return operateResultInt;
+
case TypeCode.UInt16:
OperateResult<TimeSpan> operateResultUShort = plc.Wait(address, Convert.ToInt16(value), readInterval, waitTimeout);
return operateResultUShort;
+
case TypeCode.UInt32:
OperateResult<TimeSpan> operateResultUInt = plc.Wait(address, Convert.ToInt16(value), readInterval, waitTimeout);
return operateResultUInt;
+
default:
throw new NotSupportedException();
}
}
+
#endregion
#region Destruction Function
+
/// <summary>
- /// 鏋愭瀯鍑芥暟锛岀‘淇濆湪涓嶅啀闇�瑕佹椂鍏抽棴杩炴帴
+ /// 鏋愭瀯鍑芥暟锛岀‘淇濆湪涓嶅啀闇�瑕佹椂鍏抽棴杩炴帴
/// </summary>
~SiemensS7()
{
Dispose();
}
+
#endregion
}
-}
+}
\ No newline at end of file
--
Gitblit v1.9.3