增加西门子gb2312字符串读写
This commit is contained in:
parent
1393c3106b
commit
50562e1fc4
@ -3,6 +3,7 @@ using System.Text;
|
|||||||
using S7.Net.Types;
|
using S7.Net.Types;
|
||||||
using PluginInterface;
|
using PluginInterface;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
namespace PLC.SiemensS7
|
namespace PLC.SiemensS7
|
||||||
{
|
{
|
||||||
@ -47,6 +48,7 @@ namespace PLC.SiemensS7
|
|||||||
_device = device;
|
_device = device;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
|
||||||
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||||
_logger.LogInformation($"Device:[{_device}],Create()");
|
_logger.LogInformation($"Device:[{_device}],Create()");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,27 +134,21 @@ namespace PLC.SiemensS7
|
|||||||
[Method("读西门子PLC标准地址", description: "读西门子PLC标准地址")]
|
[Method("读西门子PLC标准地址", description: "读西门子PLC标准地址")]
|
||||||
public DriverReturnValueModel Read(DriverAddressIoArgModel ioArg)
|
public DriverReturnValueModel Read(DriverAddressIoArgModel ioArg)
|
||||||
{
|
{
|
||||||
|
var encodeArr = System.Text.Encoding.GetEncodings();
|
||||||
var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good };
|
var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good };
|
||||||
|
|
||||||
if (_plc is { IsConnected: true })
|
if (_plc is { IsConnected: true })
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (ioArg.ValueType == DataTypeEnum.AsciiString)
|
if (ioArg.ValueType == DataTypeEnum.AsciiString || ioArg.ValueType == DataTypeEnum.Utf8String || ioArg.ValueType == DataTypeEnum.Gb2312String)
|
||||||
{
|
{
|
||||||
var str = string.Empty;
|
var str = string.Empty;
|
||||||
var dataItem = S7.Net.Types.DataItem.FromAddress(ioArg.Address);
|
var dataItem = S7.Net.Types.DataItem.FromAddress(ioArg.Address);
|
||||||
var head = _plc.ReadBytes(dataItem.DataType, dataItem.DB, dataItem.StartByteAdr, 2);
|
var head = _plc.ReadBytes(dataItem.DataType, dataItem.DB, dataItem.StartByteAdr, 2);
|
||||||
var strBytes = _plc.ReadBytes(dataItem.DataType, dataItem.DB, dataItem.StartByteAdr + 2, head[1]);
|
var strBytes = _plc.ReadBytes(dataItem.DataType, dataItem.DB, dataItem.StartByteAdr + 2, head[1]);
|
||||||
var strRaw = Encoding.ASCII.GetString(strBytes).TrimEnd(new char[] { '\0' });
|
var strRaw = GetString(ioArg.ValueType, strBytes);
|
||||||
if (strRaw.Any())
|
|
||||||
{
|
|
||||||
foreach (var chart in strRaw)
|
|
||||||
{
|
|
||||||
if (chart >= 0x20 && chart <= 0x7E)
|
|
||||||
str += chart;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ret.Value = strRaw;
|
ret.Value = strRaw;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -373,15 +369,48 @@ namespace PLC.SiemensS7
|
|||||||
toWriteString = toWriteString.Take(length).ToString();
|
toWriteString = toWriteString.Take(length).ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
switch (ioArg.ValueType)
|
||||||
|
{
|
||||||
|
case DataTypeEnum.Utf8String:
|
||||||
|
return Encoding.UTF8.GetBytes(toWriteString);
|
||||||
|
case DataTypeEnum.Gb2312String:
|
||||||
|
Encoding toEcoding = Encoding.GetEncoding("gb2312");
|
||||||
|
byte[] fromBytes = Encoding.UTF8.GetBytes(toWriteString);
|
||||||
|
return Encoding.Convert(Encoding.UTF8, toEcoding, fromBytes);
|
||||||
|
case DataTypeEnum.AsciiString:
|
||||||
|
default:
|
||||||
|
return Encoding.ASCII.GetBytes(toWriteString);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
throw new Exception("字符串解析异常");
|
throw new Exception("字符串解析异常");
|
||||||
}
|
}
|
||||||
|
|
||||||
return Encoding.ASCII.GetBytes(toWriteString);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private string GetString(DataTypeEnum dataType, byte[] strBytes)
|
||||||
|
{
|
||||||
|
string? str = string.Empty;
|
||||||
|
switch (dataType)
|
||||||
|
{
|
||||||
|
case DataTypeEnum.Utf8String:
|
||||||
|
str = Encoding.UTF8.GetString(strBytes);
|
||||||
|
break;
|
||||||
|
case DataTypeEnum.Gb2312String:
|
||||||
|
Encoding fromEncoding = Encoding.GetEncoding("gb2312");
|
||||||
|
byte[] toBytes = Encoding.Convert(fromEncoding, Encoding.UTF8, strBytes);
|
||||||
|
str = Encoding.UTF8.GetString(toBytes);
|
||||||
|
break;
|
||||||
|
case DataTypeEnum.AsciiString:
|
||||||
|
default:
|
||||||
|
var strRaw = Encoding.ASCII.GetString(strBytes.Where(x => x is >= 0x20 and <= 0x7E).ToArray());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return str.TrimEnd(new char[] { '\0' });
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,10 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="System.Text.Encoding.CodePages" Version="6.0.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\PluginInterface\PluginInterface.csproj" />
|
<ProjectReference Include="..\..\PluginInterface\PluginInterface.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@ -59,5 +59,7 @@ namespace PluginInterface
|
|||||||
Custome4,
|
Custome4,
|
||||||
[Display(Name = "自定义5")]
|
[Display(Name = "自定义5")]
|
||||||
Custome5,
|
Custome5,
|
||||||
|
[Display(Name = "Gb2312")]
|
||||||
|
Gb2312String,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
Binary file not shown.
@ -8,7 +8,8 @@
|
|||||||
".NETCoreApp,Version=v6.0": {
|
".NETCoreApp,Version=v6.0": {
|
||||||
"PLC.SiemensS7/1.0.0": {
|
"PLC.SiemensS7/1.0.0": {
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"PluginInterface": "1.0.0"
|
"PluginInterface": "1.0.0",
|
||||||
|
"System.Text.Encoding.CodePages": "6.0.0"
|
||||||
},
|
},
|
||||||
"runtime": {
|
"runtime": {
|
||||||
"PLC.SiemensS7.dll": {}
|
"PLC.SiemensS7.dll": {}
|
||||||
@ -30,6 +31,12 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"System.Runtime.CompilerServices.Unsafe/6.0.0": {},
|
||||||
|
"System.Text.Encoding.CodePages/6.0.0": {
|
||||||
|
"dependencies": {
|
||||||
|
"System.Runtime.CompilerServices.Unsafe": "6.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"PluginInterface/1.0.0": {
|
"PluginInterface/1.0.0": {
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"Microsoft.Extensions.Logging.Abstractions": "6.0.2",
|
"Microsoft.Extensions.Logging.Abstractions": "6.0.2",
|
||||||
@ -61,6 +68,20 @@
|
|||||||
"path": "newtonsoft.json/13.0.1",
|
"path": "newtonsoft.json/13.0.1",
|
||||||
"hashPath": "newtonsoft.json.13.0.1.nupkg.sha512"
|
"hashPath": "newtonsoft.json.13.0.1.nupkg.sha512"
|
||||||
},
|
},
|
||||||
|
"System.Runtime.CompilerServices.Unsafe/6.0.0": {
|
||||||
|
"type": "package",
|
||||||
|
"serviceable": true,
|
||||||
|
"sha512": "sha512-/iUeP3tq1S0XdNNoMz5C9twLSrM/TH+qElHkXWaPvuNOt+99G75NrV0OS2EqHx5wMN7popYjpc8oTjC1y16DLg==",
|
||||||
|
"path": "system.runtime.compilerservices.unsafe/6.0.0",
|
||||||
|
"hashPath": "system.runtime.compilerservices.unsafe.6.0.0.nupkg.sha512"
|
||||||
|
},
|
||||||
|
"System.Text.Encoding.CodePages/6.0.0": {
|
||||||
|
"type": "package",
|
||||||
|
"serviceable": true,
|
||||||
|
"sha512": "sha512-ZFCILZuOvtKPauZ/j/swhvw68ZRi9ATCfvGbk1QfydmcXBkIWecWKn/250UH7rahZ5OoDBaiAudJtPvLwzw85A==",
|
||||||
|
"path": "system.text.encoding.codepages/6.0.0",
|
||||||
|
"hashPath": "system.text.encoding.codepages.6.0.0.nupkg.sha512"
|
||||||
|
},
|
||||||
"PluginInterface/1.0.0": {
|
"PluginInterface/1.0.0": {
|
||||||
"type": "project",
|
"type": "project",
|
||||||
"serviceable": false,
|
"serviceable": false,
|
||||||
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user