From 0bec70f1757a59f8b1a23e1f8e9a936a482cb81b Mon Sep 17 00:00:00 2001 From: hehaibing-1996 Date: Fri, 27 Dec 2024 16:27:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=91=E9=80=81=E6=8C=87?= =?UTF-8?q?=E4=BB=A4=E6=97=B6=E7=9A=84=E7=BC=96=E7=A0=81=E8=A7=84=E5=88=99?= =?UTF-8?q?=20=E6=94=B9=E4=B8=BA=E9=80=82=E9=85=8D=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Encode/Encode.cs | 16 +++++++++ Encode/Encode.csproj | 48 +++++++++++++++++++++++++ Encode/Properties/AssemblyInfo.cs | 33 +++++++++++++++++ WCS.BLL/Tool/TCPClient.cs | 60 ++++++++++++++++++++++++------- WCS.BLL/WCS.BLL.csproj | 2 ++ WCS.WebApi/Program.cs | 2 ++ WCS.WebApi/WCS后端.sln | 6 ++++ 7 files changed, 154 insertions(+), 13 deletions(-) create mode 100644 Encode/Encode.cs create mode 100644 Encode/Encode.csproj create mode 100644 Encode/Properties/AssemblyInfo.cs diff --git a/Encode/Encode.cs b/Encode/Encode.cs new file mode 100644 index 0000000..6b963b3 --- /dev/null +++ b/Encode/Encode.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Encode +{ + public static class Encode + { + public static byte[] GetGb2312(string message) + { + return Encoding.GetEncoding("gb2312").GetBytes(message); + } + } +} diff --git a/Encode/Encode.csproj b/Encode/Encode.csproj new file mode 100644 index 0000000..8129ed7 --- /dev/null +++ b/Encode/Encode.csproj @@ -0,0 +1,48 @@ + + + + + Debug + AnyCPU + {F344121D-856D-4E5C-8519-6EADDE98616E} + Library + Properties + Encode + Encode + v4.6.2 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Encode/Properties/AssemblyInfo.cs b/Encode/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..7faae2c --- /dev/null +++ b/Encode/Properties/AssemblyInfo.cs @@ -0,0 +1,33 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("Encode")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Encode")] +[assembly: AssemblyCopyright("Copyright © 2024")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("f344121d-856d-4e5c-8519-6eadde98616e")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/WCS.BLL/Tool/TCPClient.cs b/WCS.BLL/Tool/TCPClient.cs index e902643..1b03e11 100644 --- a/WCS.BLL/Tool/TCPClient.cs +++ b/WCS.BLL/Tool/TCPClient.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Concurrent; using System.ComponentModel.Design; +using System.Reflection; using System.Text; using System.Text.RegularExpressions; using TouchSocket.Core; @@ -371,31 +372,63 @@ namespace WCS.BLL public List GenerateMessageList(int boardId, byte[] data, string message) { var list = new List(); - //去除不满足ASCII的数据 - message = Regex.Replace(message, @"[^\u0000-\u007F]+", string.Empty); - //总共发的数据位数 - data[1] = Convert.ToByte(message.Length); - //数据发送总帧数 - var messageCount = Math.Ceiling((decimal)message.Length / 6); + ////去除不满足ASCII的数据 + //message = Regex.Replace(message, @"[^\u0000-\u007F]+", string.Empty); + ////总共发的数据位数 + //data[1] = Convert.ToByte(message.Length); + ////数据发送总帧数 + //var messageCount = Math.Ceiling((decimal)message.Length / 6); + //for (int i = 0; i < messageCount; i++) + //{ + // var tempmessage = string.Empty; + // //不是最后一条指令 + // if (i != messageCount - 1) + // { + // tempmessage = message.Substring(i * 6, 6); + // var charArray = tempmessage.ToCharArray(); + // for (int index = 0; index < 6; index++) + // { + // data[2 + index] = Convert.ToByte(charArray[index]); + // } + // } + // //最后一条指令 + // else + // { + // tempmessage = message.Substring(i * 6, message.Length % 6 == 0 ? 6 : message.Length % 6); + // var charArray = tempmessage.ToCharArray(); + // //先把所有数据位置为0 + // for (int index = 0; index < 6; index++) + // { + // data[2 + index] = 0x00; + // } + // //有数据的置为对应的数据 + // for (int index = 0; index < charArray.Length; index++) + // { + // data[2 + index] = Convert.ToByte(charArray[index]); + // } + // } + // list.Add(GenerateMessage(boardId, data)); + //} + //var dataBtyes = Encoding.GetEncoding(936).GetBytes(message); + + var dataBtyes = Encode.Encode.GetGb2312(message); + data[1] = Convert.ToByte(dataBtyes.Length); + var messageCount = Math.Ceiling((decimal)dataBtyes.Length / 6); for (int i = 0; i < messageCount; i++) { - var tempmessage = string.Empty; + byte[] charArray = dataBtyes.Skip(i * 6).Take(6).ToArray(); //不是最后一条指令 if (i != messageCount - 1) { - tempmessage = message.Substring(i * 6, 6); - var charArray = tempmessage.ToCharArray(); for (int index = 0; index < 6; index++) { - data[2 + index] = Convert.ToByte(charArray[index]); + data[2 + index] = charArray[index]; } } //最后一条指令 else { - tempmessage = message.Substring(i * 6, message.Length % 6 == 0 ? 6 : message.Length % 6); - var charArray = tempmessage.ToCharArray(); //先把所有数据位置为0 for (int index = 0; index < 6; index++) { @@ -404,11 +437,12 @@ namespace WCS.BLL //有数据的置为对应的数据 for (int index = 0; index < charArray.Length; index++) { - data[2 + index] = Convert.ToByte(charArray[index]); + data[2 + index] = charArray[index]; } } list.Add(GenerateMessage(boardId, data)); } + return list; } diff --git a/WCS.BLL/WCS.BLL.csproj b/WCS.BLL/WCS.BLL.csproj index 33674c1..b19675e 100644 --- a/WCS.BLL/WCS.BLL.csproj +++ b/WCS.BLL/WCS.BLL.csproj @@ -7,12 +7,14 @@ + + diff --git a/WCS.WebApi/Program.cs b/WCS.WebApi/Program.cs index 6264b23..de2ff51 100644 --- a/WCS.WebApi/Program.cs +++ b/WCS.WebApi/Program.cs @@ -21,6 +21,8 @@ namespace WebApi { try { + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + //ʼwebsocket WebSoceketManager.InitWebSocket(); diff --git a/WCS.WebApi/WCS后端.sln b/WCS.WebApi/WCS后端.sln index 3faa1d1..3cd9bd4 100644 --- a/WCS.WebApi/WCS后端.sln +++ b/WCS.WebApi/WCS后端.sln @@ -16,6 +16,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "货架标准上位机", ".. EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WCS.Model", "..\WCS.Model\WCS.Model.csproj", "{7CE9AF07-3538-46C3-BBF0-A039BDE15AAF}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Encode", "..\Encode\Encode.csproj", "{F344121D-856D-4E5C-8519-6EADDE98616E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -42,6 +44,10 @@ Global {7CE9AF07-3538-46C3-BBF0-A039BDE15AAF}.Debug|Any CPU.Build.0 = Debug|Any CPU {7CE9AF07-3538-46C3-BBF0-A039BDE15AAF}.Release|Any CPU.ActiveCfg = Release|Any CPU {7CE9AF07-3538-46C3-BBF0-A039BDE15AAF}.Release|Any CPU.Build.0 = Release|Any CPU + {F344121D-856D-4E5C-8519-6EADDE98616E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F344121D-856D-4E5C-8519-6EADDE98616E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F344121D-856D-4E5C-8519-6EADDE98616E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F344121D-856D-4E5C-8519-6EADDE98616E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE