修改发送指令时的编码规则 改为适配中文的

This commit is contained in:
hehaibing-1996
2024-12-27 16:27:00 +08:00
parent ce4a2fbe3a
commit 0bec70f175
7 changed files with 154 additions and 13 deletions

16
Encode/Encode.cs Normal file
View File

@ -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);
}
}
}

48
Encode/Encode.csproj Normal file
View File

@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{F344121D-856D-4E5C-8519-6EADDE98616E}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Encode</RootNamespace>
<AssemblyName>Encode</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Encode.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -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")]

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.ComponentModel.Design; using System.ComponentModel.Design;
using System.Reflection;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using TouchSocket.Core; using TouchSocket.Core;
@ -371,31 +372,63 @@ namespace WCS.BLL
public List<byte[]> GenerateMessageList(int boardId, byte[] data, string message) public List<byte[]> GenerateMessageList(int boardId, byte[] data, string message)
{ {
var list = new List<byte[]>(); var list = new List<byte[]>();
//去除不满足ASCII的数据 ////去除不满足ASCII的数据
message = Regex.Replace(message, @"[^\u0000-\u007F]+", string.Empty); //message = Regex.Replace(message, @"[^\u0000-\u007F]+", string.Empty);
//总共发的数据位数 ////总共发的数据位数
data[1] = Convert.ToByte(message.Length); //data[1] = Convert.ToByte(message.Length);
//数据发送总帧数 ////数据发送总帧数
var messageCount = Math.Ceiling((decimal)message.Length / 6); //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++) for (int i = 0; i < messageCount; i++)
{ {
var tempmessage = string.Empty; byte[] charArray = dataBtyes.Skip(i * 6).Take(6).ToArray();
//不是最后一条指令 //不是最后一条指令
if (i != messageCount - 1) if (i != messageCount - 1)
{ {
tempmessage = message.Substring(i * 6, 6);
var charArray = tempmessage.ToCharArray();
for (int index = 0; index < 6; index++) for (int index = 0; index < 6; index++)
{ {
data[2 + index] = Convert.ToByte(charArray[index]); data[2 + index] = charArray[index];
} }
} }
//最后一条指令 //最后一条指令
else else
{ {
tempmessage = message.Substring(i * 6, message.Length % 6 == 0 ? 6 : message.Length % 6);
var charArray = tempmessage.ToCharArray();
//先把所有数据位置为0 //先把所有数据位置为0
for (int index = 0; index < 6; index++) for (int index = 0; index < 6; index++)
{ {
@ -404,11 +437,12 @@ namespace WCS.BLL
//有数据的置为对应的数据 //有数据的置为对应的数据
for (int index = 0; index < charArray.Length; index++) 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)); list.Add(GenerateMessage(boardId, data));
} }
return list; return list;
} }

View File

@ -7,12 +7,14 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="System.Text.Encoding.CodePages" Version="9.0.0" />
<PackageReference Include="TouchSocket" Version="2.0.11" /> <PackageReference Include="TouchSocket" Version="2.0.11" />
<PackageReference Include="TouchSocket.Http" Version="2.0.3" /> <PackageReference Include="TouchSocket.Http" Version="2.0.3" />
<PackageReference Include="XLParser" Version="1.7.2" /> <PackageReference Include="XLParser" Version="1.7.2" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Encode\Encode.csproj" />
<ProjectReference Include="..\WCS.DAL\WCS.DAL.csproj" /> <ProjectReference Include="..\WCS.DAL\WCS.DAL.csproj" />
<ProjectReference Include="..\WCS.Model\WCS.Model.csproj" /> <ProjectReference Include="..\WCS.Model\WCS.Model.csproj" />
</ItemGroup> </ItemGroup>

View File

@ -21,6 +21,8 @@ namespace WebApi
{ {
try try
{ {
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
//<2F><>ʼ<EFBFBD><CABC>websocket //<2F><>ʼ<EFBFBD><CABC>websocket
WebSoceketManager.InitWebSocket(); WebSoceketManager.InitWebSocket();

View File

@ -16,6 +16,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "货架标准上位机", "..
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WCS.Model", "..\WCS.Model\WCS.Model.csproj", "{7CE9AF07-3538-46C3-BBF0-A039BDE15AAF}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WCS.Model", "..\WCS.Model\WCS.Model.csproj", "{7CE9AF07-3538-46C3-BBF0-A039BDE15AAF}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Encode", "..\Encode\Encode.csproj", "{F344121D-856D-4E5C-8519-6EADDE98616E}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{7CE9AF07-3538-46C3-BBF0-A039BDE15AAF}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE