Selaa lähdekoodia

1. 底层统计新增字节数从末尾从前移除0x00
2. 版本更新

Shun 2 vuotta sitten
vanhempi
commit
3f03101b43

+ 2 - 2
src/YSAI.DAQ/YSAI.Core/YSAI.Core.csproj

@@ -5,7 +5,7 @@
     <ImplicitUsings>enable</ImplicitUsings>
     <Nullable>enable</Nullable>
     <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
-    <Version>1.0.0.65</Version>
+    <Version>1.0.0.66</Version>
     <Authors>Shun</Authors>
     <Company>YSAI</Company>
     <Product>SCADA</Product>
@@ -17,7 +17,7 @@
 		<PackageReference Include="Microsoft.ClearScript" Version="7.4.4" />
 		<PackageReference Include="System.IO.Ports" Version="7.0.0" />
 		<PackageReference Include="YSAI.Log" Version="1.0.0.9" />
-		<PackageReference Include="YSAI.Unility" Version="1.0.0.22" />
+		<PackageReference Include="YSAI.Unility" Version="1.0.0.24" />
 	</ItemGroup>
 
 </Project>

+ 3 - 4
src/YSAI.DAQ/YSAI.Core/communication/net/tcp/client/TcpClientOperate.cs

@@ -121,7 +121,7 @@ namespace YSAI.Core.communication.net.tcp.client
                                 int bytes = Communication.GetStream().ReadAsync(CacheBuffer, 0, alen).WaitAsync(token.Token).Result;  //读取数据
                                 if (bytes > 0)
                                 {
-                                    OnEventHandler(this, new EventResult(true, $"接收到[{TAG}]监控数据", ByteTool.ByteTrimEnd(CacheBuffer), @enum.ResultType.Byte));  //数据传递出去
+                                    OnEventHandler(this, new EventResult(true, $"接收到[{TAG}]监控数据", CacheBuffer.TrimEnd(), @enum.ResultType.Byte));  //数据传递出去
                                 }
                                 else
                                 {
@@ -333,8 +333,7 @@ namespace YSAI.Core.communication.net.tcp.client
                     if (reverseBack.State)
                     {
                         CancellationTokenSource token = new CancellationTokenSource();
-
-                        byte[] Byte = new byte[1024 * 1024];
+                        byte[] Byte = new byte[65536];
                         bool Status = Task.Run(() =>
                         {
                             while (!token.IsCancellationRequested)
@@ -362,7 +361,7 @@ namespace YSAI.Core.communication.net.tcp.client
                         {
                             if (Byte.Length > 0)
                             {
-                                return Break("SendWait", true, RData: Byte, RType: @enum.ResultType.Byte);
+                                return Break("SendWait", true, RData: Byte.TrimEnd(), RType: @enum.ResultType.Byte);
                             }
                             else
                             {

+ 2 - 2
src/YSAI.DAQ/YSAI.Core/communication/net/tcp/service/TcpServiceOperate.cs

@@ -162,7 +162,7 @@ namespace YSAI.Core.communication.net.tcp.service
             {
                 Socket ClientCon = socket as Socket;
                 string IpPort = ClientCon.RemoteEndPoint.ToString();
-                ArraySegment<byte> DataByte = new ArraySegment<byte>(new byte[1024 * 1024]); //数据缓冲区
+                ArraySegment<byte> DataByte = new ArraySegment<byte>(new byte[65536]); //数据缓冲区
                 while (!token.IsCancellationRequested)
                 {
                     try
@@ -170,7 +170,7 @@ namespace YSAI.Core.communication.net.tcp.service
                         int length = ClientCon.ReceiveAsync(DataByte, SocketFlags.Partial).WaitAsync(token.Token).Result; // 接收数据,并返回数据的长度;
                         if (length > 0)
                         {
-                            OnEventHandler(this, new EventResult(true, $"[{IpPort}]数据接收成功", new TcpServiceData.ClientMessage { Step = TcpServiceData.Steps.消息接收, IpPort = IpPort, Data = ByteTool.ByteTrimEnd(DataByte.Array) }, @enum.ResultType.Byte));
+                            OnEventHandler(this, new EventResult(true, $"[{IpPort}]数据接收成功", new TcpServiceData.ClientMessage { Step = TcpServiceData.Steps.消息接收, IpPort = IpPort, Data = DataByte.Array.TrimEnd() }, @enum.ResultType.Byte));
                         }
                         else
                         {

+ 5 - 5
src/YSAI.DAQ/YSAI.Core/communication/net/udp/UdpOperate.cs

@@ -105,7 +105,7 @@ namespace YSAI.Core.communication.net.udp
                             UdpReceiveResult urr = Communication.ReceiveAsync(token.Token).Result;  //读取数据
                             if (urr.Buffer.Length > 0)
                             {
-                                OnEventHandler(this, new EventResult(true, $"接收到[{TAG}]监控数据,来自[{urr.RemoteEndPoint}]", new UdpData.TerminalMessage { IpPort = urr.RemoteEndPoint.ToString(), Data = ByteTool.ByteTrimEnd(urr.Buffer) }, @enum.ResultType.Byte));  //数据传递出去
+                                OnEventHandler(this, new EventResult(true, $"接收到[{TAG}]监控数据,来自[{urr.RemoteEndPoint}]", new UdpData.TerminalMessage { IpPort = urr.RemoteEndPoint.ToString(), Data = urr.Buffer.TrimEnd() }, @enum.ResultType.Byte));  //数据传递出去
                             }
                             else
                             {
@@ -307,7 +307,7 @@ namespace YSAI.Core.communication.net.udp
                     if (reverseBack.State)
                     {
                         CancellationTokenSource token = new CancellationTokenSource();
-                        byte[] Byte = new byte[1024 * 1024];
+                        byte[] Byte = new byte[65536];
                         bool Status = Task.Run(() =>
                         {
                             while (!token.IsCancellationRequested)
@@ -336,7 +336,7 @@ namespace YSAI.Core.communication.net.udp
                         {
                             if (Byte.Length > 0)
                             {
-                                return Break("SendWait", true, RData: Byte, RType: @enum.ResultType.Byte);
+                                return Break("SendWait", true, RData: Byte.TrimEnd(), RType: @enum.ResultType.Byte);
                             }
                             else
                             {
@@ -374,7 +374,7 @@ namespace YSAI.Core.communication.net.udp
                     if (reverseBack.State)
                     {
                         CancellationTokenSource token = new CancellationTokenSource();
-                        byte[] Byte = new byte[1024 * 1024];
+                        byte[] Byte = new byte[65536];
                         bool Status = Task.Run(() =>
                         {
                             while (!token.IsCancellationRequested)
@@ -385,7 +385,7 @@ namespace YSAI.Core.communication.net.udp
                                     UdpReceiveResult urr = Communication.ReceiveAsync().Result;  //读取数据
                                     if (urr.Buffer.Length > 0)
                                     {
-                                        OnEventHandler(this, new EventResult(true, $"接收到[{TAG}]监控数据,来自[{urr.RemoteEndPoint}]", new UdpData.TerminalMessage { IpPort = urr.RemoteEndPoint.ToString(), Data = ByteTool.ByteTrimEnd(urr.Buffer) }, @enum.ResultType.Byte));  //数据传递出去
+                                        OnEventHandler(this, new EventResult(true, $"接收到[{TAG}]监控数据,来自[{urr.RemoteEndPoint}]", new UdpData.TerminalMessage { IpPort = urr.RemoteEndPoint.ToString(), Data = urr.Buffer.TrimEnd() }, @enum.ResultType.Byte));  //数据传递出去
                                     }
                                     else
                                     {

+ 5 - 5
src/YSAI.DAQ/YSAI.Core/communication/net/ws/client/WsClientOperate.cs

@@ -110,11 +110,11 @@ namespace YSAI.Core.communication.net.ws.client
                     {
                         if (Communication != null && Communication.State.Equals(WebSocketState.Open))
                         {
-                            ArraySegment<byte> CacheBuffer = new ArraySegment<byte>(new byte[1024 * 1024]);
+                            ArraySegment<byte> CacheBuffer = new ArraySegment<byte>(new byte[65536]);
                             WebSocketReceiveResult result = Communication.ReceiveAsync(CacheBuffer, token.Token).WaitAsync(token.Token).Result;   //读取数据
                             if (result.Count > 0)
                             {
-                                OnEventHandler(this, new EventResult(true, $"接收到[{TAG}]监控数据", ByteTool.ByteTrimEnd(CacheBuffer.Array), @enum.ResultType.Byte));  //数据传递出去
+                                OnEventHandler(this, new EventResult(true, $"接收到[{TAG}]监控数据", CacheBuffer.Array.TrimEnd(), @enum.ResultType.Byte));  //数据传递出去
                             }
                             else
                             {
@@ -341,12 +341,12 @@ namespace YSAI.Core.communication.net.ws.client
                     if (reverseBack.State)
                     {
                         CancellationTokenSource token = new CancellationTokenSource();
-                        byte[] Byte = new byte[1024 * 1024];
+                        byte[] Byte = new byte[65536];
                         bool Status = Task.Run(() =>
                         {
                             while (!token.IsCancellationRequested)
                             {
-                                ArraySegment<byte> CacheBuffer = new ArraySegment<byte>(new byte[1024 * 1024]);
+                                ArraySegment<byte> CacheBuffer = new ArraySegment<byte>(new byte[65536]);
                                 WebSocketReceiveResult result = Communication.ReceiveAsync(CacheBuffer, token.Token).Result;  //读取数据
                                 if (result.Count > 0)
                                 {
@@ -363,7 +363,7 @@ namespace YSAI.Core.communication.net.ws.client
                         {
                             if (Byte.Length > 0)
                             {
-                                return Break("SendWait", true, RData: Byte, RType: @enum.ResultType.Byte);
+                                return Break("SendWait", true, RData: Byte.TrimEnd(), RType: @enum.ResultType.Byte);
                             }
                             else
                             {

+ 2 - 2
src/YSAI.DAQ/YSAI.Core/communication/net/ws/service/WsServiceOperate.cs

@@ -179,7 +179,7 @@ namespace YSAI.Core.communication.net.ws.service
             //起一个新线程来监控
             return Task.Factory.StartNew(() =>
             {
-                ArraySegment<byte> DataByte = new ArraySegment<byte>(new byte[1024 * 1024]); //数据缓冲区
+                ArraySegment<byte> DataByte = new ArraySegment<byte>(new byte[65536]); //数据缓冲区
                 while (!token.IsCancellationRequested)
                 {
                     try
@@ -187,7 +187,7 @@ namespace YSAI.Core.communication.net.ws.service
                         WebSocketReceiveResult result = webSocket.ReceiveAsync(DataByte, token.Token).Result; // 接收数据,并返回数据的长度;
                         if (result.Count > 0)
                         {
-                            OnEventHandler(this, new EventResult(true, $"[{IpPort}]数据接收成功", new WsServiceData.ClientMessage { Step = WsServiceData.Steps.消息接收, IpPort = IpPort, Data = DataByte.Array }, @enum.ResultType.Byte));
+                            OnEventHandler(this, new EventResult(true, $"[{IpPort}]数据接收成功", new WsServiceData.ClientMessage { Step = WsServiceData.Steps.消息接收, IpPort = IpPort, Data = DataByte.Array.TrimEnd() }, @enum.ResultType.Byte));
                         }
                         else
                         {

+ 3 - 3
src/YSAI.DAQ/YSAI.Core/communication/serial/SerialOperate.cs

@@ -110,7 +110,7 @@ namespace YSAI.Core.communication.serial
                                 byte[] Byte = new byte[BufSize];
                                 if (Communication.Read(Byte, 0, BufSize) > 0)
                                 {
-                                    OnEventHandler(this, new EventResult(true, $"接收到[{TAG}]监控数据", ByteTool.ByteTrimEnd(Byte), ResultType.Byte));
+                                    OnEventHandler(this, new EventResult(true, $"接收到[{TAG}]监控数据", ByteTool.TrimEnd(Byte), ResultType.Byte));
                                 }
                                 else
                                 {
@@ -267,7 +267,7 @@ namespace YSAI.Core.communication.serial
                     if (reverseBack.State)
                     {
                         CancellationTokenSource token = new CancellationTokenSource();
-                        byte[] Byte = new byte[1024 * 1024];
+                        byte[] Byte = new byte[65536];
                         bool Status = Task.Run(() =>
                         {
                             while (!token.IsCancellationRequested)
@@ -291,7 +291,7 @@ namespace YSAI.Core.communication.serial
                         {
                             if (Byte.Length > 0)
                             {
-                                return Break("SendWait", true, RData: Byte, RType: @enum.ResultType.Byte);
+                                return Break("SendWait", true, RData: Byte.TrimEnd(), RType: @enum.ResultType.Byte);
                             }
                             else
                             {

+ 1 - 1
src/YSAI.DAQ/YSAI.Mvvm/YSAI.Mvvm.csproj

@@ -6,6 +6,6 @@
     <UseWPF>true</UseWPF>
   </PropertyGroup>
 	<ItemGroup>
-		<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.75" />
+		<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.77" />
 	</ItemGroup>
 </Project>

+ 1 - 1
src/YSAI.DAQ/YSAI.Opc/YSAI.Opc.csproj

@@ -12,7 +12,7 @@
 	  <GenerateDocumentationFile>True</GenerateDocumentationFile>
   </PropertyGroup>
   <ItemGroup>
-    <PackageReference Include="OPCFoundation.NetStandard.Opc.Ua" Version="1.4.372.56" />
+    <PackageReference Include="OPCFoundation.NetStandard.Opc.Ua" Version="1.4.372.76" />
 	<PackageReference Include="YSAI.Core" Version="1.0.0.65" />
   </ItemGroup>
 

+ 70 - 116
src/YSAI.DAQ/YSAI.Unility/ByteTool.cs

@@ -6,7 +6,7 @@ namespace YSAI.Unility
     /// <summary>
     /// 字节处理工具
     /// </summary>
-    public class ByteTool
+    public static class ByteTool
     {
         private static readonly ushort[] CrcTable =
        {
@@ -358,7 +358,7 @@ namespace YSAI.Unility
         /// </summary>
         /// <param name="bytes"></param>
         /// <returns></returns>
-        public static byte[] ByteTrimEnd(byte[] bytes)
+        public static byte[] TrimEnd(this byte[] bytes)
         {
             List<byte> list = bytes.ToList();
             for (int i = bytes.Length - 1; i >= 0; i--)
@@ -372,10 +372,6 @@ namespace YSAI.Unility
                     break;
                 }
             }
-            if (list.Count.Equals(0))
-            {
-                list.Add(0x00);
-            }
             return list.ToArray();
         }
 
@@ -384,27 +380,44 @@ namespace YSAI.Unility
         /// </summary>
         /// <param name="bytes"></param>
         /// <returns></returns>
-        public static string HexToStr(byte[] bytes, bool ClearEmpty = false)
+        public static string HexToStr(this byte[] bytes, bool ClearEmpty = false)
         {
             if (ClearEmpty)
             {
-                bytes = ByteTrimEnd(bytes);
+                bytes = TrimEnd(bytes);
             }
-            string InfoStr = string.Empty;
-            int index = 1;
-            foreach (var item in bytes)
+            return ByteArrayToHexString(bytes);
+        }
+
+        /// <summary>
+        /// byte[] 分割
+        /// </summary>
+        /// <param name="Data">数据</param>
+        /// <param name="Segmentation">几位几位分割</param>
+        public static List<byte[]> ByteListSegmentation(this byte[] Data, int Segmentation)
+        {
+            List<byte[]> bytels = new List<byte[]>();
+            for (int i = 0; i < Data.Length; i += Segmentation)
             {
-                if (index.Equals(bytes.Length))
-                {
-                    InfoStr += item.ToString("x2");
-                }
-                else
+                byte[] bytes = new byte[Segmentation];
+                for (int ic = 0; ic < Segmentation; ic++)
                 {
-                    InfoStr += item.ToString("x2") + " ";
+                    bytes[ic] = Data[i + ic];
                 }
-                ++index;
+                bytels.Add(bytes);
+                if (bytels.Count.Equals(Data.Length / Segmentation)) break;
             }
-            return InfoStr;
+            return bytels;
+        }
+
+        /// <summary>
+        /// 将十六进制数字字符串(例如:E4 CA B2)转换为字节数组。
+        /// </summary>
+        /// <param name="s"></param>
+        /// <returns></returns>
+        public static byte[]? HexStringToByteArray(string s)
+        {
+            return StrToHex(s);
         }
 
         /// <summary>
@@ -412,7 +425,7 @@ namespace YSAI.Unility
         /// </summary>
         /// <param name="bytes"></param>
         /// <returns></returns>
-        public static string HexToStr(object bytes, bool ClearEmpty = false)
+        public static string HexToStr(this object bytes, bool ClearEmpty = false)
         {
             if (bytes as byte[] != null)
             {
@@ -429,11 +442,43 @@ namespace YSAI.Unility
         /// </summary>
         /// <param name="bytes"></param>
         /// <returns></returns>
-        public static string HexToStr(byte bytes)
+        public static string HexToStr(this byte bytes)
         {
             return bytes.ToString("x2");
         }
 
+        /// <summary>
+        /// 16进制字符串转换
+        /// </summary>
+        /// <param name="hexstring">用空格切割字符串</param>
+        /// <returns>返回一个二进制字符串</returns>
+        public static byte[] StrToHex(this string hexstring)
+        {
+            string[] tmpary = hexstring.Trim().Split(' ');
+            byte[] buff = new byte[tmpary.Length];
+            for (int i = 0; i < buff.Length; i++)
+            {
+                buff[i] = Convert.ToByte(tmpary[i], 16);
+            }
+            return buff;
+        }
+
+        /// <summary>
+        /// 将字节数组转换为 EA AX 字符串
+        /// </summary>
+        /// <param name="data"></param>
+        /// <returns></returns>
+        public static string ByteArrayToHexString(byte[] data)
+        {
+            StringBuilder sb = new StringBuilder(data.Length * 3);
+            foreach (byte b in data)
+            {
+                sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
+            }
+
+            return sb.ToString().ToUpper();
+        }
+
         /// <summary>
         /// byte[] 字节分割
         /// 保存预留位
@@ -442,7 +487,7 @@ namespace YSAI.Unility
         /// <param name="ReserveLocation">头部预留位</param>
         /// <param name="SplitCount">分割数 几位几位分割</param>
         /// <returns></returns>
-        public static List<byte[]> ByteDataSplit(byte[] FileStreamData, int ReserveLocation, int SplitCount)
+        public static List<byte[]> ByteDataSplit(this byte[] FileStreamData, int ReserveLocation, int SplitCount)
         {
             List<byte[]> ByteList = new List<byte[]>();  //存储
             double Num = Convert.ToDouble(FileStreamData.Length) / Convert.ToDouble(SplitCount - ReserveLocation); //计算循环数
@@ -660,7 +705,7 @@ namespace YSAI.Unility
             src[2] = (byte)((value >> 16) & 0xFF);
             src[1] = (byte)((value >> 8) & 0xFF);
             src[0] = (byte)(value & 0xFF);
-            return ByteTrimEnd(src);
+            return TrimEnd(src);
         }
 
         /**
@@ -694,64 +739,6 @@ namespace YSAI.Unility
             return src;
         }
 
-        /// <summary>
-        /// byte[] 分割
-        /// </summary>
-        /// <param name="Data">数据</param>
-        /// <param name="Segmentation">几位几位分割</param>
-        public static List<byte[]> ByteListSegmentation(byte[] Data, int Segmentation)
-        {
-            List<byte[]> bytels = new List<byte[]>();
-            for (int i = 0; i < Data.Length; i += Segmentation)
-            {
-                byte[] bytes = new byte[Segmentation];
-                for (int ic = 0; ic < Segmentation; ic++)
-                {
-                    bytes[ic] = Data[i + ic];
-                }
-                bytels.Add(bytes);
-                if (bytels.Count.Equals(Data.Length / Segmentation)) break;
-            }
-            return bytels;
-        }
-
-        /// <summary>
-        /// 将十六进制数字字符串(例如:E4 CA B2)转换为字节数组。
-        /// </summary>
-        /// <param name="s"></param>
-        /// <returns></returns>
-        public static object HexStringToByteArray(string s)
-        {
-            s = s.Replace(" ", "").Replace("\r", "").Replace("\n", "");
-            byte[] buffer = new byte[s.Length / 2];
-            for (int i = 0; i < s.Length; i += 2)
-            {
-                buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
-            }
-            if (buffer.Length > 1)
-            {
-                return buffer;
-            }
-            else
-            {
-                return buffer[0];
-            }
-        }
-
-        /// <summary> Converts an array of bytes into a formatted string of hex digits (ex: E4 CA B2)</summary>
-        /// <param name="data"> The array of bytes to be translated into a string of hex digits. </param>
-        /// <returns> Returns a well formatted string of hex digits with spacing. </returns>
-        public static string ByteArrayToHexString(byte[] data)
-        {
-            StringBuilder sb = new StringBuilder(data.Length * 3);
-            foreach (byte b in data)
-            {
-                sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
-            }
-
-            return sb.ToString().ToUpper();
-        }
-
         /// <summary>
         /// 将一条十六进制字符串转换为ASCII
         /// </summary>
@@ -759,7 +746,7 @@ namespace YSAI.Unility
         /// <returns>返回一条ASCII码</returns>
         public static string HexStringToASCII(string hexstring)
         {
-            byte[] bt = HexStringToBinary(hexstring);
+            byte[] bt = StrToHex(hexstring);
             string lin = "";
             for (int i = 0; i < bt.Length; i++)
             {
@@ -781,22 +768,6 @@ namespace YSAI.Unility
 
         /**/
 
-        /// <summary>
-        /// 16进制字符串转换
-        /// </summary>
-        /// <param name="hexstring">用空格切割字符串</param>
-        /// <returns>返回一个二进制字符串</returns>
-        public static byte[] HexStringToBinary(string hexstring)
-        {
-            string[] tmpary = hexstring.Trim().Split(' ');
-            byte[] buff = new byte[tmpary.Length];
-            for (int i = 0; i < buff.Length; i++)
-            {
-                buff[i] = Convert.ToByte(tmpary[i], 16);
-            }
-            return buff;
-        }
-
         public static int StringToHexOrDec(string strData)
         {
             int dData = -1;
@@ -826,29 +797,12 @@ namespace YSAI.Unility
             return dData;
         }
 
-        /// <summary>
-        /// 将byte型转换为字符串
-        /// </summary>
-        /// <param name="arrInput">byte型数组</param>
-        /// <returns>目标字符串</returns>
-        private string ByteArrayToString(byte[] arrInput)
-        {
-            int i;
-            StringBuilder sOutput = new StringBuilder(arrInput.Length);
-            for (i = 0; i < arrInput.Length; i++)
-            {
-                sOutput.Append(arrInput[i].ToString("X2"));
-            }
-            //将此实例的值转换为System.String
-            return sOutput.ToString();
-        }
-
         /// <summary>
         /// 对接收到的数据进行解包(将接收到的byte型数组解包为Unicode字符串)
         /// </summary>
         /// <param name="recbytes">byte型数组</param>
         /// <returns>Unicode编码的字符串</returns>
-        public string disPackage(byte[] recbytes)
+        public static string disPackage(byte[] recbytes)
         {
             string temp = "";
             foreach (byte b in recbytes)

+ 1 - 1
src/YSAI.DAQ/YSAI.Unility/YSAI.Unility.csproj

@@ -5,7 +5,7 @@
 		<ImplicitUsings>enable</ImplicitUsings>
 		<Nullable>enable</Nullable>
 		<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
-		<Version>1.0.0.22</Version>
+		<Version>1.0.0.24</Version>
 		<Authors>Shun</Authors>
 		<Company>YSAI</Company>
 		<Product>SCADA</Product>