update 2014-07-31
This commit is contained in:
90
NA200H/ModBus.Net/ConfigurationManager.Designer.cs
generated
Normal file
90
NA200H/ModBus.Net/ConfigurationManager.Designer.cs
generated
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
//------------------------------------------------------------------------------
|
||||||
|
// <auto-generated>
|
||||||
|
// 此代码由工具生成。
|
||||||
|
// 运行时版本:4.0.30319.34014
|
||||||
|
//
|
||||||
|
// 对此文件的更改可能会导致不正确的行为,并且如果
|
||||||
|
// 重新生成代码,这些更改将会丢失。
|
||||||
|
// </auto-generated>
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
namespace ModBus.Net {
|
||||||
|
using System;
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 一个强类型的资源类,用于查找本地化的字符串等。
|
||||||
|
/// </summary>
|
||||||
|
// 此类是由 StronglyTypedResourceBuilder
|
||||||
|
// 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
|
||||||
|
// 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen
|
||||||
|
// (以 /str 作为命令选项),或重新生成 VS 项目。
|
||||||
|
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
|
||||||
|
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||||
|
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
||||||
|
internal class ConfigurationManager {
|
||||||
|
|
||||||
|
private static global::System.Resources.ResourceManager resourceMan;
|
||||||
|
|
||||||
|
private static global::System.Globalization.CultureInfo resourceCulture;
|
||||||
|
|
||||||
|
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
||||||
|
internal ConfigurationManager() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 返回此类使用的缓存的 ResourceManager 实例。
|
||||||
|
/// </summary>
|
||||||
|
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
||||||
|
internal static global::System.Resources.ResourceManager ResourceManager {
|
||||||
|
get {
|
||||||
|
if (object.ReferenceEquals(resourceMan, null)) {
|
||||||
|
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ModBus.Net.ConfigurationManager", typeof(ConfigurationManager).Assembly);
|
||||||
|
resourceMan = temp;
|
||||||
|
}
|
||||||
|
return resourceMan;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 使用此强类型资源类,为所有资源查找
|
||||||
|
/// 重写当前线程的 CurrentUICulture 属性。
|
||||||
|
/// </summary>
|
||||||
|
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
||||||
|
internal static global::System.Globalization.CultureInfo Culture {
|
||||||
|
get {
|
||||||
|
return resourceCulture;
|
||||||
|
}
|
||||||
|
set {
|
||||||
|
resourceCulture = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 查找类似 192.168.2.100 的本地化字符串。
|
||||||
|
/// </summary>
|
||||||
|
internal static string IP {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("IP", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 查找类似 502 的本地化字符串。
|
||||||
|
/// </summary>
|
||||||
|
internal static string Port {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("Port", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 查找类似 1000 的本地化字符串。
|
||||||
|
/// </summary>
|
||||||
|
internal static string SocketTimeOut {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("SocketTimeOut", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
129
NA200H/ModBus.Net/ConfigurationManager.resx
Normal file
129
NA200H/ModBus.Net/ConfigurationManager.resx
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<root>
|
||||||
|
<!--
|
||||||
|
Microsoft ResX Schema
|
||||||
|
|
||||||
|
Version 2.0
|
||||||
|
|
||||||
|
The primary goals of this format is to allow a simple XML format
|
||||||
|
that is mostly human readable. The generation and parsing of the
|
||||||
|
various data types are done through the TypeConverter classes
|
||||||
|
associated with the data types.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
... ado.net/XML headers & schema ...
|
||||||
|
<resheader name="resmimetype">text/microsoft-resx</resheader>
|
||||||
|
<resheader name="version">2.0</resheader>
|
||||||
|
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
|
||||||
|
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
|
||||||
|
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
|
||||||
|
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
|
||||||
|
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
|
||||||
|
<value>[base64 mime encoded serialized .NET Framework object]</value>
|
||||||
|
</data>
|
||||||
|
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||||
|
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
|
||||||
|
<comment>This is a comment</comment>
|
||||||
|
</data>
|
||||||
|
|
||||||
|
There are any number of "resheader" rows that contain simple
|
||||||
|
name/value pairs.
|
||||||
|
|
||||||
|
Each data row contains a name, and value. The row also contains a
|
||||||
|
type or mimetype. Type corresponds to a .NET class that support
|
||||||
|
text/value conversion through the TypeConverter architecture.
|
||||||
|
Classes that don't support this are serialized and stored with the
|
||||||
|
mimetype set.
|
||||||
|
|
||||||
|
The mimetype is used for serialized objects, and tells the
|
||||||
|
ResXResourceReader how to depersist the object. This is currently not
|
||||||
|
extensible. For a given mimetype the value must be set accordingly:
|
||||||
|
|
||||||
|
Note - application/x-microsoft.net.object.binary.base64 is the format
|
||||||
|
that the ResXResourceWriter will generate, however the reader can
|
||||||
|
read any of the formats listed below.
|
||||||
|
|
||||||
|
mimetype: application/x-microsoft.net.object.binary.base64
|
||||||
|
value : The object must be serialized with
|
||||||
|
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
|
||||||
|
: and then encoded with base64 encoding.
|
||||||
|
|
||||||
|
mimetype: application/x-microsoft.net.object.soap.base64
|
||||||
|
value : The object must be serialized with
|
||||||
|
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
|
||||||
|
: and then encoded with base64 encoding.
|
||||||
|
|
||||||
|
mimetype: application/x-microsoft.net.object.bytearray.base64
|
||||||
|
value : The object must be serialized into a byte array
|
||||||
|
: using a System.ComponentModel.TypeConverter
|
||||||
|
: and then encoded with base64 encoding.
|
||||||
|
-->
|
||||||
|
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||||
|
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||||
|
<xsd:element name="root" msdata:IsDataSet="true">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:choice maxOccurs="unbounded">
|
||||||
|
<xsd:element name="metadata">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:sequence>
|
||||||
|
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||||
|
</xsd:sequence>
|
||||||
|
<xsd:attribute name="name" use="required" type="xsd:string" />
|
||||||
|
<xsd:attribute name="type" type="xsd:string" />
|
||||||
|
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||||
|
<xsd:attribute ref="xml:space" />
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
<xsd:element name="assembly">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:attribute name="alias" type="xsd:string" />
|
||||||
|
<xsd:attribute name="name" type="xsd:string" />
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
<xsd:element name="data">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:sequence>
|
||||||
|
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||||
|
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||||
|
</xsd:sequence>
|
||||||
|
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
|
||||||
|
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||||
|
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||||
|
<xsd:attribute ref="xml:space" />
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
<xsd:element name="resheader">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:sequence>
|
||||||
|
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||||
|
</xsd:sequence>
|
||||||
|
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
</xsd:choice>
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
</xsd:schema>
|
||||||
|
<resheader name="resmimetype">
|
||||||
|
<value>text/microsoft-resx</value>
|
||||||
|
</resheader>
|
||||||
|
<resheader name="version">
|
||||||
|
<value>2.0</value>
|
||||||
|
</resheader>
|
||||||
|
<resheader name="reader">
|
||||||
|
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||||
|
</resheader>
|
||||||
|
<resheader name="writer">
|
||||||
|
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||||
|
</resheader>
|
||||||
|
<data name="IP" xml:space="preserve">
|
||||||
|
<value>192.168.2.100</value>
|
||||||
|
</data>
|
||||||
|
<data name="Port" xml:space="preserve">
|
||||||
|
<value>502</value>
|
||||||
|
</data>
|
||||||
|
<data name="SocketTimeOut" xml:space="preserve">
|
||||||
|
<value>1000</value>
|
||||||
|
</data>
|
||||||
|
</root>
|
||||||
77
NA200H/ModBus.Net/ModBus.Net.csproj
Normal file
77
NA200H/ModBus.Net/ModBus.Net.csproj
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="12.0" DefaultTargets="Build" 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>{124EBEF2-8960-4447-84CF-1D683B1EF7CC}</ProjectGuid>
|
||||||
|
<OutputType>Library</OutputType>
|
||||||
|
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||||
|
<RootNamespace>ModBus.Net</RootNamespace>
|
||||||
|
<AssemblyName>ModBus.Net</AssemblyName>
|
||||||
|
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||||
|
<FileAlignment>512</FileAlignment>
|
||||||
|
</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>
|
||||||
|
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
|
||||||
|
</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.Windows.Forms" />
|
||||||
|
<Reference Include="System.Xml.Linq" />
|
||||||
|
<Reference Include="System.Data.DataSetExtensions" />
|
||||||
|
<Reference Include="Microsoft.CSharp" />
|
||||||
|
<Reference Include="System.Data" />
|
||||||
|
<Reference Include="System.Xml" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="AddressTranslator.cs" />
|
||||||
|
<Compile Include="BaseProtocal.cs" />
|
||||||
|
<Compile Include="ConfigurationManager.Designer.cs">
|
||||||
|
<AutoGen>True</AutoGen>
|
||||||
|
<DesignTime>True</DesignTime>
|
||||||
|
<DependentUpon>ConfigurationManager.resx</DependentUpon>
|
||||||
|
</Compile>
|
||||||
|
<Compile Include="IProtocalFormatting.cs" />
|
||||||
|
<Compile Include="ProtocalLinker.cs" />
|
||||||
|
<Compile Include="ProtocalUnit.cs" />
|
||||||
|
<Compile Include="ModbusProtocal.cs" />
|
||||||
|
<Compile Include="ModbusTCPProtocal.cs" />
|
||||||
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
<Compile Include="TCPSocket.cs">
|
||||||
|
<SubType>Code</SubType>
|
||||||
|
</Compile>
|
||||||
|
<Compile Include="TCPProtocalLinker.cs" />
|
||||||
|
<Compile Include="ValueHelper.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<EmbeddedResource Include="ConfigurationManager.resx">
|
||||||
|
<Generator>ResXFileCodeGenerator</Generator>
|
||||||
|
<LastGenOutput>ConfigurationManager.Designer.cs</LastGenOutput>
|
||||||
|
</EmbeddedResource>
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
|
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||||
|
Other similar extension points exist, see Microsoft.Common.targets.
|
||||||
|
<Target Name="BeforeBuild">
|
||||||
|
</Target>
|
||||||
|
<Target Name="AfterBuild">
|
||||||
|
</Target>
|
||||||
|
-->
|
||||||
|
</Project>
|
||||||
39
NA200H/ModBus.Net/Properties/AssemblyInfo.cs
Normal file
39
NA200H/ModBus.Net/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
// 有关程序集的常规信息通过以下
|
||||||
|
// 特性集控制。更改这些特性值可修改
|
||||||
|
// 与程序集关联的信息。
|
||||||
|
|
||||||
|
[assembly: AssemblyTitle("ModBus.Net")]
|
||||||
|
[assembly: AssemblyDescription("")]
|
||||||
|
[assembly: AssemblyConfiguration("")]
|
||||||
|
[assembly: AssemblyCompany("")]
|
||||||
|
[assembly: AssemblyProduct("ModBus.Net")]
|
||||||
|
[assembly: AssemblyCopyright("Copyright © 2014")]
|
||||||
|
[assembly: AssemblyTrademark("")]
|
||||||
|
[assembly: AssemblyCulture("")]
|
||||||
|
|
||||||
|
// 将 ComVisible 设置为 false 使此程序集中的类型
|
||||||
|
// 对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型,
|
||||||
|
// 则将该类型上的 ComVisible 特性设置为 true。
|
||||||
|
|
||||||
|
[assembly: ComVisible(false)]
|
||||||
|
|
||||||
|
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
|
||||||
|
|
||||||
|
[assembly: Guid("5e21fad9-c4ec-442e-bc0c-050e7a52b418")]
|
||||||
|
|
||||||
|
// 程序集的版本信息由下面四个值组成:
|
||||||
|
//
|
||||||
|
// 主版本
|
||||||
|
// 次版本
|
||||||
|
// 生成号
|
||||||
|
// 修订号
|
||||||
|
//
|
||||||
|
// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
|
||||||
|
// 方法是按如下所示使用“*”:
|
||||||
|
// [assembly: AssemblyVersion("1.0.*")]
|
||||||
|
|
||||||
|
[assembly: AssemblyVersion("1.0.0.0")]
|
||||||
|
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||||
9
NA200H/ModBus.Net/ProtocalLinker.cs
Normal file
9
NA200H/ModBus.Net/ProtocalLinker.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace ModBus.Net
|
||||||
|
{
|
||||||
|
public abstract class ProtocalLinker
|
||||||
|
{
|
||||||
|
public abstract byte[] SendReceive(byte[] content);
|
||||||
|
|
||||||
|
public abstract bool SendOnly(byte[] content);
|
||||||
|
}
|
||||||
|
}
|
||||||
25
NA200H/ModBus.Net/TCPProtocalLinker.cs
Normal file
25
NA200H/ModBus.Net/TCPProtocalLinker.cs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
namespace ModBus.Net
|
||||||
|
{
|
||||||
|
public class TCPProtocalLinker : ProtocalLinker
|
||||||
|
{
|
||||||
|
private static TCPSocket _socket;
|
||||||
|
|
||||||
|
public TCPProtocalLinker()
|
||||||
|
{
|
||||||
|
if (_socket == null)
|
||||||
|
{
|
||||||
|
_socket = new TCPSocket(ConfigurationManager.IP, int.Parse(ConfigurationManager.Port), false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override byte[] SendReceive(byte[] content)
|
||||||
|
{
|
||||||
|
return _socket.SendMsg(content);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool SendOnly(byte[] content)
|
||||||
|
{
|
||||||
|
return _socket.SendMsgWithoutReturn(content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
262
NA200H/ModBus.Net/ValueHelper.cs
Normal file
262
NA200H/ModBus.Net/ValueHelper.cs
Normal file
@@ -0,0 +1,262 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace ModBus.Net
|
||||||
|
{
|
||||||
|
public class ValueHelper
|
||||||
|
{
|
||||||
|
protected static bool _littleEndian = false;
|
||||||
|
|
||||||
|
protected ValueHelper()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool LittleEndian
|
||||||
|
{
|
||||||
|
get { return _littleEndian; }
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_littleEndian = value;
|
||||||
|
_Instance = LittleEndian ? new ValueHelper() : new LittleEndianValueHelper();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Factory
|
||||||
|
|
||||||
|
protected static ValueHelper _Instance = null;
|
||||||
|
|
||||||
|
internal static ValueHelper Instance
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_Instance == null)
|
||||||
|
{
|
||||||
|
_Instance = LittleEndian ? new ValueHelper() : new LittleEndianValueHelper();
|
||||||
|
}
|
||||||
|
return _Instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
public Byte[] GetBytes(byte value)
|
||||||
|
{
|
||||||
|
return new[] {value};
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual Byte[] GetBytes(short value)
|
||||||
|
{
|
||||||
|
return BitConverter.GetBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual Byte[] GetBytes(int value)
|
||||||
|
{
|
||||||
|
return BitConverter.GetBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual Byte[] GetBytes(long value)
|
||||||
|
{
|
||||||
|
return BitConverter.GetBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual Byte[] GetBytes(ushort value)
|
||||||
|
{
|
||||||
|
return BitConverter.GetBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual Byte[] GetBytes(uint value)
|
||||||
|
{
|
||||||
|
return BitConverter.GetBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual Byte[] GetBytes(ulong value)
|
||||||
|
{
|
||||||
|
return BitConverter.GetBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual Byte[] GetBytes(float value)
|
||||||
|
{
|
||||||
|
return BitConverter.GetBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual Byte[] GetBytes(double value)
|
||||||
|
{
|
||||||
|
return BitConverter.GetBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte GetByte(byte[] data, ref int pos)
|
||||||
|
{
|
||||||
|
byte t = data[pos];
|
||||||
|
pos += 1;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual short GetShort(byte[] data, ref int pos)
|
||||||
|
{
|
||||||
|
short t = BitConverter.ToInt16(data, pos);
|
||||||
|
pos += 2;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual int GetInt(byte[] data, ref int pos)
|
||||||
|
{
|
||||||
|
int t = BitConverter.ToInt32(data, pos);
|
||||||
|
pos += 4;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual long GetLong(byte[] data, ref int pos)
|
||||||
|
{
|
||||||
|
long t = BitConverter.ToInt64(data, pos);
|
||||||
|
pos += 8;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual ushort GetUShort(byte[] data, ref int pos)
|
||||||
|
{
|
||||||
|
ushort t = BitConverter.ToUInt16(data, pos);
|
||||||
|
pos += 2;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual uint GetUInt(byte[] data, ref int pos)
|
||||||
|
{
|
||||||
|
uint t = BitConverter.ToUInt32(data, pos);
|
||||||
|
pos += 4;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual ulong GetULong(byte[] data, ref int pos)
|
||||||
|
{
|
||||||
|
ulong t = BitConverter.ToUInt64(data, 0);
|
||||||
|
pos += 8;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual float GetFloat(byte[] data, ref int pos)
|
||||||
|
{
|
||||||
|
float t = BitConverter.ToSingle(data, 0);
|
||||||
|
pos += 4;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual double GetDouble(byte[] data, ref int pos)
|
||||||
|
{
|
||||||
|
double t = BitConverter.ToDouble(data, 0);
|
||||||
|
pos += 8;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class LittleEndianValueHelper : ValueHelper
|
||||||
|
{
|
||||||
|
public override Byte[] GetBytes(short value)
|
||||||
|
{
|
||||||
|
return Reverse(BitConverter.GetBytes(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Byte[] GetBytes(int value)
|
||||||
|
{
|
||||||
|
return Reverse(BitConverter.GetBytes(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Byte[] GetBytes(long value)
|
||||||
|
{
|
||||||
|
return Reverse(BitConverter.GetBytes(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Byte[] GetBytes(ushort value)
|
||||||
|
{
|
||||||
|
return Reverse(BitConverter.GetBytes(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Byte[] GetBytes(uint value)
|
||||||
|
{
|
||||||
|
return Reverse(BitConverter.GetBytes(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Byte[] GetBytes(ulong value)
|
||||||
|
{
|
||||||
|
return Reverse(BitConverter.GetBytes(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Byte[] GetBytes(float value)
|
||||||
|
{
|
||||||
|
return Reverse(BitConverter.GetBytes(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Byte[] GetBytes(double value)
|
||||||
|
{
|
||||||
|
return Reverse(BitConverter.GetBytes(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public override short GetShort(byte[] data, ref int pos)
|
||||||
|
{
|
||||||
|
Array.Reverse(data, pos, 2);
|
||||||
|
short t = BitConverter.ToInt16(data, pos);
|
||||||
|
pos += 2;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int GetInt(byte[] data, ref int pos)
|
||||||
|
{
|
||||||
|
Array.Reverse(data, pos, 4);
|
||||||
|
int t = BitConverter.ToInt32(data, pos);
|
||||||
|
pos += 4;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override long GetLong(byte[] data, ref int pos)
|
||||||
|
{
|
||||||
|
Array.Reverse(data, pos, 8);
|
||||||
|
long t = BitConverter.ToInt64(data, pos);
|
||||||
|
pos += 8;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override ushort GetUShort(byte[] data, ref int pos)
|
||||||
|
{
|
||||||
|
Array.Reverse(data, pos, 2);
|
||||||
|
ushort t = BitConverter.ToUInt16(data, pos);
|
||||||
|
pos += 2;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override uint GetUInt(byte[] data, ref int pos)
|
||||||
|
{
|
||||||
|
Array.Reverse(data, pos, 4);
|
||||||
|
uint t = BitConverter.ToUInt32(data, pos);
|
||||||
|
pos += 4;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override ulong GetULong(byte[] data, ref int pos)
|
||||||
|
{
|
||||||
|
Array.Reverse(data, pos, 8);
|
||||||
|
ulong t = BitConverter.ToUInt64(data, 0);
|
||||||
|
pos += 8;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override float GetFloat(byte[] data, ref int pos)
|
||||||
|
{
|
||||||
|
Array.Reverse(data, pos, 4);
|
||||||
|
float t = BitConverter.ToSingle(data, 0);
|
||||||
|
pos += 4;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override double GetDouble(byte[] data, ref int pos)
|
||||||
|
{
|
||||||
|
Array.Reverse(data, pos, 8);
|
||||||
|
double t = BitConverter.ToDouble(data, 0);
|
||||||
|
pos += 8;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Byte[] Reverse(Byte[] data)
|
||||||
|
{
|
||||||
|
Array.Reverse(data);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user