diff --git a/NA200H/ModBus.Net/ConfigurationManager.Designer.cs b/NA200H/ModBus.Net/ConfigurationManager.Designer.cs new file mode 100644 index 0000000..4d6c075 --- /dev/null +++ b/NA200H/ModBus.Net/ConfigurationManager.Designer.cs @@ -0,0 +1,90 @@ +//------------------------------------------------------------------------------ +// +// 此代码由工具生成。 +// 运行时版本:4.0.30319.34014 +// +// 对此文件的更改可能会导致不正确的行为,并且如果 +// 重新生成代码,这些更改将会丢失。 +// +//------------------------------------------------------------------------------ + +namespace ModBus.Net { + using System; + + + /// + /// 一个强类型的资源类,用于查找本地化的字符串等。 + /// + // 此类是由 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() { + } + + /// + /// 返回此类使用的缓存的 ResourceManager 实例。 + /// + [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; + } + } + + /// + /// 使用此强类型资源类,为所有资源查找 + /// 重写当前线程的 CurrentUICulture 属性。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// 查找类似 192.168.2.100 的本地化字符串。 + /// + internal static string IP { + get { + return ResourceManager.GetString("IP", resourceCulture); + } + } + + /// + /// 查找类似 502 的本地化字符串。 + /// + internal static string Port { + get { + return ResourceManager.GetString("Port", resourceCulture); + } + } + + /// + /// 查找类似 1000 的本地化字符串。 + /// + internal static string SocketTimeOut { + get { + return ResourceManager.GetString("SocketTimeOut", resourceCulture); + } + } + } +} diff --git a/NA200H/ModBus.Net/ConfigurationManager.resx b/NA200H/ModBus.Net/ConfigurationManager.resx new file mode 100644 index 0000000..e2ed588 --- /dev/null +++ b/NA200H/ModBus.Net/ConfigurationManager.resx @@ -0,0 +1,129 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 192.168.2.100 + + + 502 + + + 1000 + + \ No newline at end of file diff --git a/NA200H/ModBus.Net/ModBus.Net.csproj b/NA200H/ModBus.Net/ModBus.Net.csproj new file mode 100644 index 0000000..9585270 --- /dev/null +++ b/NA200H/ModBus.Net/ModBus.Net.csproj @@ -0,0 +1,77 @@ + + + + + Debug + AnyCPU + {124EBEF2-8960-4447-84CF-1D683B1EF7CC} + Library + Properties + ModBus.Net + ModBus.Net + v4.5 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + false + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + True + True + ConfigurationManager.resx + + + + + + + + + Code + + + + + + + ResXFileCodeGenerator + ConfigurationManager.Designer.cs + + + + + \ No newline at end of file diff --git a/NA200H/ModBus.Net/Properties/AssemblyInfo.cs b/NA200H/ModBus.Net/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..a1b573e --- /dev/null +++ b/NA200H/ModBus.Net/Properties/AssemblyInfo.cs @@ -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")] \ No newline at end of file diff --git a/NA200H/ModBus.Net/ProtocalLinker.cs b/NA200H/ModBus.Net/ProtocalLinker.cs new file mode 100644 index 0000000..e67dd8f --- /dev/null +++ b/NA200H/ModBus.Net/ProtocalLinker.cs @@ -0,0 +1,9 @@ +namespace ModBus.Net +{ + public abstract class ProtocalLinker + { + public abstract byte[] SendReceive(byte[] content); + + public abstract bool SendOnly(byte[] content); + } +} \ No newline at end of file diff --git a/NA200H/ModBus.Net/TCPProtocalLinker.cs b/NA200H/ModBus.Net/TCPProtocalLinker.cs new file mode 100644 index 0000000..b942187 --- /dev/null +++ b/NA200H/ModBus.Net/TCPProtocalLinker.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/NA200H/ModBus.Net/ValueHelper.cs b/NA200H/ModBus.Net/ValueHelper.cs new file mode 100644 index 0000000..b855d3c --- /dev/null +++ b/NA200H/ModBus.Net/ValueHelper.cs @@ -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; + } + } +} \ No newline at end of file