-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add support to symbol type "REAL", "UINT"
- Loading branch information
Showing
5 changed files
with
160 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.CompilerServices; | ||
using System.Text; | ||
|
||
public static class SpanConverter | ||
{ | ||
public static T ConvertTo<T>(ReadOnlySpan<byte> span) where T : struct { | ||
if (span.Length < Marshal.SizeOf<T>()) | ||
{ | ||
throw new ArgumentException("The span is too small to contain the specified type."); | ||
} | ||
|
||
return MemoryMarshal.Read<T>(span); | ||
} | ||
|
||
public static object ConvertTo(ReadOnlySpan<byte> span, Type targetType) { | ||
if (span == null || span.Length == 0) | ||
{ | ||
throw new ArgumentException("Span is null or empty.", nameof(span)); | ||
} | ||
|
||
if (targetType == null) | ||
{ | ||
throw new ArgumentNullException(nameof(targetType)); | ||
} | ||
|
||
switch (Type.GetTypeCode(targetType)) | ||
{ | ||
case TypeCode.Boolean: | ||
if (span.Length >= sizeof(Boolean)) | ||
{ | ||
return BitConverter.ToBoolean(span); | ||
} | ||
break; | ||
case TypeCode.UInt16: | ||
if (span.Length >= sizeof(UInt16)) | ||
{ | ||
return BitConverter.ToUInt16(span); | ||
} | ||
break; | ||
case TypeCode.UInt32: | ||
if (span.Length >= sizeof(UInt32)) | ||
{ | ||
return BitConverter.ToUInt32(span); | ||
} | ||
break; | ||
case TypeCode.UInt64: | ||
if (span.Length >= sizeof(UInt64)) | ||
{ | ||
return BitConverter.ToUInt64(span); | ||
} | ||
break; | ||
case TypeCode.Int16: | ||
if (span.Length >= sizeof(Int16)) | ||
{ | ||
return BitConverter.ToInt16(span); | ||
} | ||
break; | ||
case TypeCode.Int32: | ||
if (span.Length >= sizeof(Int32)) | ||
{ | ||
return BitConverter.ToInt32(span); | ||
} | ||
break; | ||
case TypeCode.Int64: | ||
if (span.Length >= sizeof(Int64)) | ||
{ | ||
return BitConverter.ToInt64(span); | ||
} | ||
break; | ||
case TypeCode.Single: | ||
if (span.Length >= sizeof(Single)) | ||
{ | ||
return BitConverter.ToSingle(span); | ||
} | ||
break; | ||
case TypeCode.Double: | ||
if (span.Length >= sizeof(Double)) | ||
{ | ||
return BitConverter.ToDouble(span); | ||
} | ||
break; | ||
// todo: add more cases | ||
default: | ||
throw new NotSupportedException($"Conversion to type '{targetType}' is not supported."); | ||
} | ||
|
||
throw new ArgumentException("Span does not contain enough data to convert to the specified type.", nameof(span)); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.5.2.0 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TwincatToolbox", "TwincatToolbox.csproj", "{A0FF4E68-20C6-69CE-B6DD-17373783E8D5}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{A0FF4E68-20C6-69CE-B6DD-17373783E8D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{A0FF4E68-20C6-69CE-B6DD-17373783E8D5}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{A0FF4E68-20C6-69CE-B6DD-17373783E8D5}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{A0FF4E68-20C6-69CE-B6DD-17373783E8D5}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {C31C6327-37C2-4E98-8910-E3603D16A755} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters