2017-05-11 update 1 move setting files to App.config

This commit is contained in:
parallelbgls
2017-05-11 10:49:31 +08:00
parent 3418162137
commit 139b093a4f
22 changed files with 110 additions and 332 deletions

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="COM" value="COM1"/>
<add key="FBoxOpcDaHost" value="opcda://localhost/FBoxOpcServer"/>
<add key="IP" value="192.168.1.1"/>
<add key="IPConnectionTimeout" value="5000"/>
<add key="ModbusPort" value="502"/>
<add key="OpcDaHost" value="opcda://localhost/..."/>
<add key="OpcUaHost" value="opc.tcp://localhost/..."/>
<add key="SiemensPort" value="102"/>
</appSettings>
</configuration>

View File

@@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.IO;
using Microsoft.Extensions.Configuration;
namespace Modbus.Net
{
public static class ConfigurationManager
{
private static IConfigurationBuilder builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddXmlFile("App.config");
private static IConfigurationRoot Configuration => builder.Build();
public static IConfigurationSection AppSettings => Configuration.GetSection("AppSettings");
}
}

View File

@@ -51,23 +51,11 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Xml" Version="1.1.2" />
<PackageReference Include="System.Collections.NonGeneric" Version="4.3.0" />
<PackageReference Include="System.Threading.ThreadPool" Version="4.3.0" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\src\Base.Common\ConfigurationManager.Designer.cs" Link="ConfigurationManager.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>ConfigurationManager.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="..\src\Base.Common\ConfigurationManager.resx" Link="ConfigurationManager.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>ConfigurationManager.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
</Project>

View File

@@ -27,4 +27,8 @@
<ProjectReference Include="..\ModBus.Net\Modbus.Net.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Configuration" />
</ItemGroup>
</Project>

View File

@@ -1,4 +1,6 @@
namespace Modbus.Net.Modbus
using System.Configuration;
namespace Modbus.Net.Modbus
{
/// <summary>
/// Modbus/Ascii码协议
@@ -6,7 +8,7 @@
public class ModbusAsciiProtocal : ModbusProtocal
{
public ModbusAsciiProtocal(byte slaveAddress, byte masterAddress, Endian endian)
: this(ConfigurationManager.COM, slaveAddress, masterAddress, endian)
: this(ConfigurationManager.AppSettings["COM"], slaveAddress, masterAddress, endian)
{
}

View File

@@ -1,4 +1,6 @@
namespace Modbus.Net.Modbus
using System.Configuration;
namespace Modbus.Net.Modbus
{
/// <summary>
/// Modbus/Rtu协议
@@ -6,7 +8,7 @@
public class ModbusRtuProtocal : ModbusProtocal
{
public ModbusRtuProtocal(byte slaveAddress, byte masterAddress, Endian endian)
: this(ConfigurationManager.COM, slaveAddress, masterAddress, endian)
: this(ConfigurationManager.AppSettings["COM"], slaveAddress, masterAddress, endian)
{
}

View File

@@ -1,4 +1,6 @@
namespace Modbus.Net.Modbus
using System.Configuration;
namespace Modbus.Net.Modbus
{
/// <summary>
/// Modbus/Tcp协议
@@ -6,7 +8,7 @@
public class ModbusTcpProtocal : ModbusProtocal
{
public ModbusTcpProtocal(byte slaveAddress, byte masterAddress, Endian endian)
: this(ConfigurationManager.IP, slaveAddress, masterAddress, endian)
: this(ConfigurationManager.AppSettings["IP"], slaveAddress, masterAddress, endian)
{
}

View File

@@ -1,11 +1,13 @@
namespace Modbus.Net.Modbus
using System.Configuration;
namespace Modbus.Net.Modbus
{
/// <summary>
/// Modbus/Tcp协议连接器
/// </summary>
public class ModbusTcpProtocalLinker : TcpProtocalLinker
{
public ModbusTcpProtocalLinker(string ip) : base(ip, int.Parse(ConfigurationManager.ModbusPort))
public ModbusTcpProtocalLinker(string ip) : base(ip, int.Parse(ConfigurationManager.AppSettings["ModbusPort"]))
{
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -13,7 +14,7 @@ namespace Modbus.Net.OPC.FBox
public string LinkerName { get; set; }
public FBoxOpcDaMachine(string localSequence, string linkerName,
IEnumerable<AddressUnit> getAddresses, bool keepConnect) : base(ConfigurationManager.FBoxOpcDaHost, getAddresses, keepConnect)
IEnumerable<AddressUnit> getAddresses, bool keepConnect) : base(ConfigurationManager.AppSettings["FBoxOpcDaHost"], getAddresses, keepConnect)
{
LocalSequence = localSequence;
LinkerName = linkerName;

View File

@@ -24,11 +24,17 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="H.Opc" Version="0.8.1" />
<PackageReference Include="H.Opc" Version="0.9.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ModBus.Net\Modbus.Net.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Configuration">
<HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Configuration.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

View File

@@ -1,4 +1,5 @@
using System.Text;
using System.Configuration;
using System.Text;
namespace Modbus.Net.OPC
{
@@ -7,7 +8,7 @@ namespace Modbus.Net.OPC
/// </summary>
public class OpcDaProtocalLinker : OpcProtocalLinker
{
public OpcDaProtocalLinker() : this(ConfigurationManager.OpcDaHost)
public OpcDaProtocalLinker() : this(ConfigurationManager.AppSettings["OpcDaHost"])
{
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -11,7 +12,7 @@ namespace Modbus.Net.OPC
/// </summary>
public class OpcUaProtocalLinker : OpcProtocalLinker
{
public OpcUaProtocalLinker() : this(ConfigurationManager.OpcUaHost)
public OpcUaProtocalLinker() : this(ConfigurationManager.AppSettings["OpcUaHost"])
{
}

View File

@@ -25,5 +25,11 @@
<ItemGroup>
<ProjectReference Include="..\ModBus.Net\Modbus.Net.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Configuration">
<HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Configuration.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

View File

@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Configuration;
using System.Threading.Tasks;
namespace Modbus.Net.Siemens
{
@@ -10,7 +11,7 @@ namespace Modbus.Net.Siemens
private readonly string _com;
public SiemensPpiProtocal(byte slaveAddress, byte masterAddress)
: this(ConfigurationManager.COM, slaveAddress, masterAddress)
: this(ConfigurationManager.AppSettings["COM"], slaveAddress, masterAddress)
{
}

View File

@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Configuration;
using System.Threading.Tasks;
namespace Modbus.Net.Siemens
{
@@ -18,7 +19,7 @@ namespace Modbus.Net.Siemens
private int _connectTryCount;
public SiemensTcpProtocal(byte tdpuSize, ushort tsapSrc, ushort tsapDst, ushort maxCalling, ushort maxCalled,
ushort maxPdu) : this(tdpuSize, tsapSrc, tsapDst, maxCalling, maxCalled, maxPdu, ConfigurationManager.IP)
ushort maxPdu) : this(tdpuSize, tsapSrc, tsapDst, maxCalling, maxCalled, maxPdu, ConfigurationManager.AppSettings["IP"])
{
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Configuration;
namespace Modbus.Net.Siemens
{
@@ -8,7 +9,7 @@ namespace Modbus.Net.Siemens
public class SiemensTcpProtocalLinker : TcpProtocalLinker
{
public SiemensTcpProtocalLinker(string ip)
: base(ip, int.Parse(ConfigurationManager.SiemensPort))
: base(ip, int.Parse(ConfigurationManager.AppSettings["SiemensPort"]))
{
}

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="COM" value="COM1"/>
<add key="FBoxOpcDaHost" value="opcda://localhost/FBoxOpcServer"/>
<add key="IP" value="192.168.1.1"/>
<add key="ComConnectionTimeout" value="3000"/>
<add key="IPConnectionTimeout" value="5000"/>
<add key="ModbusPort" value="502"/>
<add key="OpcDaHost" value="opcda://localhost/..."/>
<add key="OpcUaHost" value="opc.tcp://localhost/..."/>
<add key="SiemensPort" value="102"/>
</appSettings>
</configuration>

View File

@@ -1,4 +1,5 @@
using System.IO.Ports;
using System.Configuration;
using System.IO.Ports;
namespace Modbus.Net
{
@@ -15,7 +16,7 @@ namespace Modbus.Net
/// <param name="stopBits">停止位</param>
/// <param name="dataBits">数据位</param>
protected ComProtocalLinker(int baudRate, Parity parity, StopBits stopBits, int dataBits)
: this(ConfigurationManager.COM, baudRate, parity, stopBits, dataBits)
: this(ConfigurationManager.AppSettings["COM"], baudRate, parity, stopBits, dataBits)
{
}
@@ -30,7 +31,7 @@ namespace Modbus.Net
protected ComProtocalLinker(string com, int baudRate, Parity parity, StopBits stopBits, int dataBits)
{
//初始化连对象
BaseConnector = new ComConnector(com, baudRate, parity, stopBits, dataBits, 30000);
BaseConnector = new ComConnector(com, baudRate, parity, stopBits, dataBits, int.Parse(ConfigurationManager.AppSettings["ComConnectionTimeout"]));
}
}
}

View File

@@ -50,18 +50,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="..\src\Base.Common\ConfigurationManager.Designer.cs" Link="ConfigurationManager.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>ConfigurationManager.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="..\src\Base.Common\ConfigurationManager.resx" Link="ConfigurationManager.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>ConfigurationManager.Designer.cs</LastGenOutput>
</EmbeddedResource>
<Reference Include="System.Configuration">
<HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Configuration.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

View File

@@ -1,137 +0,0 @@
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码由工具生成。
// 运行时版本:4.0.30319.42000
//
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------
using System.Reflection;
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()]
public 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)]
public 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).GetTypeInfo().Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// 使用此强类型资源类,为所有资源查找
/// 重写当前线程的 CurrentUICulture 属性。
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
public static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
/// <summary>
/// 查找类似 COM1 的本地化字符串。
/// </summary>
public static string COM {
get {
return ResourceManager.GetString("COM", resourceCulture);
}
}
/// <summary>
/// 查找类似 opcda://localhost/FBoxOpcServer 的本地化字符串。
/// </summary>
public static string FBoxOpcDaHost {
get {
return ResourceManager.GetString("FBoxOpcDaHost", resourceCulture);
}
}
/// <summary>
/// 查找类似 192.168.1.1 的本地化字符串。
/// </summary>
public static string IP {
get {
return ResourceManager.GetString("IP", resourceCulture);
}
}
/// <summary>
/// 查找类似 5000 的本地化字符串。
/// </summary>
public static string IPConnectionTimeout {
get {
return ResourceManager.GetString("IPConnectionTimeout", resourceCulture);
}
}
/// <summary>
/// 查找类似 502 的本地化字符串。
/// </summary>
public static string ModbusPort {
get {
return ResourceManager.GetString("ModbusPort", resourceCulture);
}
}
/// <summary>
/// 查找类似 opcda://localhost/... 的本地化字符串。
/// </summary>
public static string OpcDaHost {
get {
return ResourceManager.GetString("OpcDaHost", resourceCulture);
}
}
/// <summary>
/// 查找类似 opc.tcp://localhost/... 的本地化字符串。
/// </summary>
public static string OpcUaHost {
get {
return ResourceManager.GetString("OpcUaHost", resourceCulture);
}
}
/// <summary>
/// 查找类似 102 的本地化字符串。
/// </summary>
public static string SiemensPort {
get {
return ResourceManager.GetString("SiemensPort", resourceCulture);
}
}
}
}

View File

@@ -1,144 +0,0 @@
<?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="COM" xml:space="preserve">
<value>COM1</value>
</data>
<data name="FBoxOpcDaHost" xml:space="preserve">
<value>opcda://localhost/FBoxOpcServer</value>
</data>
<data name="IP" xml:space="preserve">
<value>192.168.1.1</value>
</data>
<data name="IPConnectionTimeout" xml:space="preserve">
<value>5000</value>
</data>
<data name="ModbusPort" xml:space="preserve">
<value>502</value>
</data>
<data name="OpcDaHost" xml:space="preserve">
<value>opcda://localhost/...</value>
</data>
<data name="OpcUaHost" xml:space="preserve">
<value>opc.tcp://localhost/...</value>
</data>
<data name="SiemensPort" xml:space="preserve">
<value>102</value>
</data>
</root>

View File

@@ -1,4 +1,8 @@
namespace Modbus.Net
#if NET40||NET45||NET451||NET452||NET46||NET461||NET462||NET47
using System.Configuration;
#endif
namespace Modbus.Net
{
/// <summary>
/// Tcp连接对象
@@ -8,7 +12,7 @@
/// <summary>
/// 构造器
/// </summary>
protected TcpProtocalLinker() : this(ConfigurationManager.IP, int.Parse(ConfigurationManager.ModbusPort))
protected TcpProtocalLinker() : this(ConfigurationManager.AppSettings["IP"], int.Parse(ConfigurationManager.AppSettings["ModbusPort"]))
{
}
@@ -20,7 +24,7 @@
protected TcpProtocalLinker(string ip, int port)
{
//初始化连接对象
BaseConnector = new TcpConnector(ip, port, int.Parse(ConfigurationManager.IPConnectionTimeout));
BaseConnector = new TcpConnector(ip, port, int.Parse(ConfigurationManager.AppSettings["IPConnectionTimeout"]));
}
}
}