From 9b49f5af504636d137da67a53471ebd2ddd36f1b Mon Sep 17 00:00:00 2001 From: parallelbgls Date: Wed, 27 Dec 2017 16:59:39 +0800 Subject: [PATCH] 2017-12-27 Controller Maintainance and less exception throw --- Modbus.Net/Modbus.Net-vs2015.sln | 43 ++++++ .../Modbus.Net.Modbus-vs2015.csproj | 80 +++++++++++ .../Modbus.Net.Modbus.csproj | 6 +- .../ModbusAsciiInTcpProtocolLinker.cs | 2 +- .../ModbusAsciiProtocolLinker.cs | 4 +- .../ModbusRtuInTcpProtocolLinker.cs | 2 +- .../ModbusRtuProtocolLinker.cs | 4 +- .../ModbusTcpProtocolLinker.cs | 2 +- .../Properties/AssemblyInfo.cs | 36 +++++ Modbus.Net/Modbus.Net.Modbus/packages.config | 5 + Modbus.Net/Modbus.Net.Modbus/project.json | 5 + .../Modbus.Net.OPC-vs2015.csproj | 101 ++++++++++++++ .../Modbus.Net.OPC/Modbus.Net.OPC.csproj | 8 ++ .../Modbus.Net.OPC/Properties/AssemblyInfo.cs | 36 +++++ Modbus.Net/Modbus.Net.OPC/packages.config | 5 + Modbus.Net/Modbus.Net.OPC/project.json | 5 + .../Modbus.Net.Siemens-vs2015.csproj | 75 +++++++++++ .../Modbus.Net.Siemens.csproj | 8 ++ .../Properties/AssemblyInfo.cs | 36 +++++ .../SiemensPpiProtocolLinker.cs | 4 +- .../SiemensTcpProtocolLinker.cs | 4 +- .../Modbus.Net.Siemens/SiemensUtility.cs | 2 +- Modbus.Net/Modbus.Net.Siemens/packages.config | 5 + Modbus.Net/Modbus.Net.Siemens/project.json | 5 + Modbus.Net/Modbus.Net/BaseConnector.cs | 3 +- Modbus.Net/Modbus.Net/BaseController.cs | 4 +- Modbus.Net/Modbus.Net/BaseProtocol.cs | 6 +- Modbus.Net/Modbus.Net/FifoController.cs | 2 +- Modbus.Net/Modbus.Net/MatchController.cs | 93 ++----------- .../Modbus.Net/MatchDirectlySendController.cs | 37 ++++++ .../Modbus.Net/Modbus.Net-vs2015.csproj | 124 ++++++++++++++++++ Modbus.Net/Modbus.Net/Modbus.Net.csproj | 9 ++ .../Modbus.Net/Properties/AssemblyInfo.cs | 36 +++++ Modbus.Net/Modbus.Net/ProtocalLinker.cs | 13 +- Modbus.Net/Modbus.Net/ProtocalUnit.cs | 6 +- Modbus.Net/Modbus.Net/package-lock.json | 3 + Modbus.Net/Modbus.Net/packages.config | 9 ++ Modbus.Net/Modbus.Net/project.json | 5 + Tests/Modbus.Net.PersistedTests/Program.cs | 62 +-------- 39 files changed, 733 insertions(+), 162 deletions(-) create mode 100644 Modbus.Net/Modbus.Net-vs2015.sln create mode 100644 Modbus.Net/Modbus.Net.Modbus/Modbus.Net.Modbus-vs2015.csproj create mode 100644 Modbus.Net/Modbus.Net.Modbus/Properties/AssemblyInfo.cs create mode 100644 Modbus.Net/Modbus.Net.Modbus/packages.config create mode 100644 Modbus.Net/Modbus.Net.Modbus/project.json create mode 100644 Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC-vs2015.csproj create mode 100644 Modbus.Net/Modbus.Net.OPC/Properties/AssemblyInfo.cs create mode 100644 Modbus.Net/Modbus.Net.OPC/packages.config create mode 100644 Modbus.Net/Modbus.Net.OPC/project.json create mode 100644 Modbus.Net/Modbus.Net.Siemens/Modbus.Net.Siemens-vs2015.csproj create mode 100644 Modbus.Net/Modbus.Net.Siemens/Properties/AssemblyInfo.cs create mode 100644 Modbus.Net/Modbus.Net.Siemens/packages.config create mode 100644 Modbus.Net/Modbus.Net.Siemens/project.json create mode 100644 Modbus.Net/Modbus.Net/MatchDirectlySendController.cs create mode 100644 Modbus.Net/Modbus.Net/Modbus.Net-vs2015.csproj create mode 100644 Modbus.Net/Modbus.Net/Properties/AssemblyInfo.cs create mode 100644 Modbus.Net/Modbus.Net/package-lock.json create mode 100644 Modbus.Net/Modbus.Net/packages.config create mode 100644 Modbus.Net/Modbus.Net/project.json diff --git a/Modbus.Net/Modbus.Net-vs2015.sln b/Modbus.Net/Modbus.Net-vs2015.sln new file mode 100644 index 0000000..1931373 --- /dev/null +++ b/Modbus.Net/Modbus.Net-vs2015.sln @@ -0,0 +1,43 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27130.2010 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Modbus.Net-vs2015", "Modbus.Net\Modbus.Net-vs2015.csproj", "{0C662F94-7362-4D4F-AFD7-76562764893C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Modbus.Net.Modbus-vs2015", "Modbus.Net.Modbus\Modbus.Net.Modbus-vs2015.csproj", "{AF2BFCBB-F396-4340-A612-5D0C2477C1B3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Modbus.Net.Siemens-vs2015", "Modbus.Net.Siemens\Modbus.Net.Siemens-vs2015.csproj", "{53EFB3C6-771E-44D2-9E77-5E70C86D00A5}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Modbus.Net.OPC-vs2015", "Modbus.Net.OPC\Modbus.Net.OPC-vs2015.csproj", "{AD3FF8DE-B9C1-4242-ACC5-4E9400A883B5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0C662F94-7362-4D4F-AFD7-76562764893C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0C662F94-7362-4D4F-AFD7-76562764893C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0C662F94-7362-4D4F-AFD7-76562764893C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0C662F94-7362-4D4F-AFD7-76562764893C}.Release|Any CPU.Build.0 = Release|Any CPU + {AF2BFCBB-F396-4340-A612-5D0C2477C1B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AF2BFCBB-F396-4340-A612-5D0C2477C1B3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AF2BFCBB-F396-4340-A612-5D0C2477C1B3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AF2BFCBB-F396-4340-A612-5D0C2477C1B3}.Release|Any CPU.Build.0 = Release|Any CPU + {53EFB3C6-771E-44D2-9E77-5E70C86D00A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {53EFB3C6-771E-44D2-9E77-5E70C86D00A5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {53EFB3C6-771E-44D2-9E77-5E70C86D00A5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {53EFB3C6-771E-44D2-9E77-5E70C86D00A5}.Release|Any CPU.Build.0 = Release|Any CPU + {AD3FF8DE-B9C1-4242-ACC5-4E9400A883B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AD3FF8DE-B9C1-4242-ACC5-4E9400A883B5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AD3FF8DE-B9C1-4242-ACC5-4E9400A883B5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AD3FF8DE-B9C1-4242-ACC5-4E9400A883B5}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {98BE5DC9-7F81-4673-9202-9C334802DAD9} + EndGlobalSection +EndGlobal diff --git a/Modbus.Net/Modbus.Net.Modbus/Modbus.Net.Modbus-vs2015.csproj b/Modbus.Net/Modbus.Net.Modbus/Modbus.Net.Modbus-vs2015.csproj new file mode 100644 index 0000000..bf2c313 --- /dev/null +++ b/Modbus.Net/Modbus.Net.Modbus/Modbus.Net.Modbus-vs2015.csproj @@ -0,0 +1,80 @@ + + + + + Debug + AnyCPU + {AF2BFCBB-F396-4340-A612-5D0C2477C1B3} + Library + Properties + Modbus.Net.Modbus + Modbus.Net.Modbus + v4.5 + 512 + + + + true + full + false + bin\Debug\ + TRACE;DEBUG;NET45 + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE;NET45 + prompt + 4 + + + + ..\packages\Serilog.2.5.0\lib\net45\Serilog.dll + + + + + + ..\packages\System.ValueTuple.4.4.0\lib\netstandard1.0\System.ValueTuple.dll + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {0c662f94-7362-4d4f-afd7-76562764893c} + Modbus.Net-vs2015 + + + + + + + + \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.Modbus/Modbus.Net.Modbus.csproj b/Modbus.Net/Modbus.Net.Modbus/Modbus.Net.Modbus.csproj index 98477b8..0e03ef2 100644 --- a/Modbus.Net/Modbus.Net.Modbus/Modbus.Net.Modbus.csproj +++ b/Modbus.Net/Modbus.Net.Modbus/Modbus.Net.Modbus.csproj @@ -26,6 +26,10 @@ bin\Debug\Modbus.Net.Modbus.xml + + + + @@ -35,6 +39,6 @@ - + \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocolLinker.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocolLinker.cs index ba6074d..1e8e38a 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocolLinker.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocolLinker.cs @@ -36,7 +36,7 @@ namespace Modbus.Net.Modbus public override bool? CheckRight(byte[] content) { //ProtocolLinker不会返回null - if (!base.CheckRight(content).Value) return false; + if (base.CheckRight(content) != true) return false; //CRC校验失败 var contentString = Encoding.ASCII.GetString(content); if (!Crc16.GetInstance().LrcEfficacy(contentString)) diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiProtocolLinker.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiProtocolLinker.cs index 46a0354..e2dd16d 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiProtocolLinker.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiProtocolLinker.cs @@ -17,7 +17,7 @@ namespace Modbus.Net.Modbus public ModbusAsciiProtocolLinker(string com, int slaveAddress) : base(com, 9600, Parity.None, StopBits.One, 8, slaveAddress) { - ((BaseConnector)BaseConnector).AddController(new MatchController(new ICollection[] { new List { 0, 1 }}, 500)); + ((BaseConnector)BaseConnector).AddController(new MatchController(new ICollection<(int,int)>[] { new List<(int,int)> { (0,0), (1,1) }}, 100)); } /// @@ -28,7 +28,7 @@ namespace Modbus.Net.Modbus public override bool? CheckRight(byte[] content) { //ProtocolLinker不会返回null - if (!base.CheckRight(content).Value) return false; + if (base.CheckRight(content) != true) return false; //CRC校验失败 var contentString = Encoding.ASCII.GetString(content); if (!Crc16.GetInstance().LrcEfficacy(contentString)) diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocolLinker.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocolLinker.cs index 14713de..82e36b0 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocolLinker.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocolLinker.cs @@ -35,7 +35,7 @@ namespace Modbus.Net.Modbus public override bool? CheckRight(byte[] content) { //ProtocolLinker的CheckRight不会返回null - if (!base.CheckRight(content).Value) return false; + if (base.CheckRight(content) != true) return false; //CRC校验失败 if (!Crc16.GetInstance().CrcEfficacy(content)) throw new ModbusProtocolErrorException(501); diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusRtuProtocolLinker.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusRtuProtocolLinker.cs index aa42ce7..1e07eb9 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusRtuProtocolLinker.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusRtuProtocolLinker.cs @@ -16,7 +16,7 @@ namespace Modbus.Net.Modbus public ModbusRtuProtocolLinker(string com, int slaveAddress) : base(com, 9600, Parity.None, StopBits.One, 8, slaveAddress) { - ((BaseConnector)BaseConnector).AddController(new MatchController(new ICollection[]{new List{0}}, 500)); + ((BaseConnector)BaseConnector).AddController(new MatchController(new ICollection<(int,int)>[]{new List<(int,int)>{(0,0), (1,1)}}, 100)); } /// @@ -27,7 +27,7 @@ namespace Modbus.Net.Modbus public override bool? CheckRight(byte[] content) { //ProtocolLinker的CheckRight不会返回null - if (!base.CheckRight(content).Value) return false; + if (base.CheckRight(content) != true) return false; //CRC校验失败 if (!Crc16.GetInstance().CrcEfficacy(content)) throw new ModbusProtocolErrorException(501); diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusTcpProtocolLinker.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusTcpProtocolLinker.cs index 250aecb..e6bacc5 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusTcpProtocolLinker.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusTcpProtocolLinker.cs @@ -34,7 +34,7 @@ namespace Modbus.Net.Modbus public override bool? CheckRight(byte[] content) { //ProtocolLinker的CheckRight不会返回null - if (!base.CheckRight(content).Value) return false; + if (base.CheckRight(content) != true) return false; //长度校验失败 if (content[5] != content.Length - 6) throw new ModbusProtocolErrorException(500); diff --git a/Modbus.Net/Modbus.Net.Modbus/Properties/AssemblyInfo.cs b/Modbus.Net/Modbus.Net.Modbus/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..65191f2 --- /dev/null +++ b/Modbus.Net/Modbus.Net.Modbus/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Modbus.Net.Modbus")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Modbus.Net")] +[assembly: AssemblyCopyright("")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("07bcb9ac-a522-4348-8774-2825fd8a6e64")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("0.0.0.1")] +[assembly: AssemblyFileVersion("0.0.0.1")] diff --git a/Modbus.Net/Modbus.Net.Modbus/packages.config b/Modbus.Net/Modbus.Net.Modbus/packages.config new file mode 100644 index 0000000..f531b99 --- /dev/null +++ b/Modbus.Net/Modbus.Net.Modbus/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.Modbus/project.json b/Modbus.Net/Modbus.Net.Modbus/project.json new file mode 100644 index 0000000..b3f452a --- /dev/null +++ b/Modbus.Net/Modbus.Net.Modbus/project.json @@ -0,0 +1,5 @@ +{ + "runtimes": { + "win": {} + } +} diff --git a/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC-vs2015.csproj b/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC-vs2015.csproj new file mode 100644 index 0000000..983db3e --- /dev/null +++ b/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC-vs2015.csproj @@ -0,0 +1,101 @@ + + + + + Debug + AnyCPU + {AD3FF8DE-B9C1-4242-ACC5-4E9400A883B5} + Library + Properties + Modbus.Net.OPC + Modbus.Net.OPC + v4.5 + 512 + + + + true + full + false + bin\Debug\ + TRACE;DEBUG;NET45 + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE;NET45 + prompt + 4 + + + + ..\packages\H.Opc.0.9.3\lib\h-opc.dll + + + ..\packages\H.Opc.0.9.3\lib\Opc.Ua.Client.dll + + + ..\packages\H.Opc.0.9.3\lib\Opc.Ua.Configuration.dll + + + ..\packages\H.Opc.0.9.3\lib\Opc.Ua.Core.dll + + + ..\packages\H.Opc.0.9.3\lib\OpcComRcw.dll + True + + + ..\packages\H.Opc.0.9.3\lib\OpcNetApi.dll + + + ..\packages\H.Opc.0.9.3\lib\OpcNetApi.Com.dll + + + ..\packages\Serilog.2.5.0\lib\net45\Serilog.dll + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {0c662f94-7362-4d4f-afd7-76562764893c} + Modbus.Net-vs2015 + + + + + + + + \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC.csproj b/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC.csproj index bcf83bf..4d25d78 100644 --- a/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC.csproj +++ b/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC.csproj @@ -26,6 +26,10 @@ bin\Debug\Modbus.Net.OPC.xml + + + + @@ -38,4 +42,8 @@ + + + + \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.OPC/Properties/AssemblyInfo.cs b/Modbus.Net/Modbus.Net.OPC/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..abee20c --- /dev/null +++ b/Modbus.Net/Modbus.Net.OPC/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Modbus.Net.OPC")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Modbus.Net")] +[assembly: AssemblyCopyright("")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("07bcb9ac-a522-4348-8774-2825fd8a6e64")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("0.0.0.1")] +[assembly: AssemblyFileVersion("0.0.0.1")] diff --git a/Modbus.Net/Modbus.Net.OPC/packages.config b/Modbus.Net/Modbus.Net.OPC/packages.config new file mode 100644 index 0000000..c84fd0e --- /dev/null +++ b/Modbus.Net/Modbus.Net.OPC/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.OPC/project.json b/Modbus.Net/Modbus.Net.OPC/project.json new file mode 100644 index 0000000..b3f452a --- /dev/null +++ b/Modbus.Net/Modbus.Net.OPC/project.json @@ -0,0 +1,5 @@ +{ + "runtimes": { + "win": {} + } +} diff --git a/Modbus.Net/Modbus.Net.Siemens/Modbus.Net.Siemens-vs2015.csproj b/Modbus.Net/Modbus.Net.Siemens/Modbus.Net.Siemens-vs2015.csproj new file mode 100644 index 0000000..5d4cabe --- /dev/null +++ b/Modbus.Net/Modbus.Net.Siemens/Modbus.Net.Siemens-vs2015.csproj @@ -0,0 +1,75 @@ + + + + + Debug + AnyCPU + {53EFB3C6-771E-44D2-9E77-5E70C86D00A5} + Library + Properties + Modbus.Net.Siemens + Modbus.Net.Siemens + v4.5 + 512 + + + + true + full + false + bin\Debug\ + TRACE;DEBUG;NET45 + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE;NET45 + prompt + 4 + + + + ..\packages\Serilog.2.5.0\lib\net45\Serilog.dll + + + + + + ..\packages\System.ValueTuple.4.4.0\lib\netstandard1.0\System.ValueTuple.dll + + + + + + + + + + + + + + + + + + + + + + + + + {0c662f94-7362-4d4f-afd7-76562764893c} + Modbus.Net-vs2015 + + + + + + + + \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.Siemens/Modbus.Net.Siemens.csproj b/Modbus.Net/Modbus.Net.Siemens/Modbus.Net.Siemens.csproj index 7956459..6335f93 100644 --- a/Modbus.Net/Modbus.Net.Siemens/Modbus.Net.Siemens.csproj +++ b/Modbus.Net/Modbus.Net.Siemens/Modbus.Net.Siemens.csproj @@ -25,6 +25,10 @@ bin\Debug\Modbus.Net.Siemens.xml + + + + @@ -33,4 +37,8 @@ + + + + \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.Siemens/Properties/AssemblyInfo.cs b/Modbus.Net/Modbus.Net.Siemens/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..3894d13 --- /dev/null +++ b/Modbus.Net/Modbus.Net.Siemens/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Modbus.Net.Siemens")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Modbus.Net")] +[assembly: AssemblyCopyright("")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("07bcb9ac-a522-4348-8774-2825fd8a6e64")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("0.0.0.1")] +[assembly: AssemblyFileVersion("0.0.0.1")] diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocolLinker.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocolLinker.cs index ae5dfb3..dc631da 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocolLinker.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocolLinker.cs @@ -18,7 +18,7 @@ namespace Modbus.Net.Siemens public SiemensPpiProtocolLinker(string com, int slaveAddress) : base(com, 9600, Parity.Even, StopBits.One, 8, slaveAddress) { - ((BaseConnector)BaseConnector).AddController(new MatchController(new ICollection[] { new List { 5 }, new List { 6 }, new List { 11, 12 } }, 500)); + ((BaseConnector)BaseConnector).AddController(new MatchController(new ICollection<(int,int)>[] { new List<(int,int)> { (4,5) }, new List<(int,int)> {(5,4) }, new List<(int,int)> { (11 ,11), (12,12) } }, 100)); } /// @@ -84,7 +84,7 @@ namespace Modbus.Net.Siemens /// 报文是否正确 public override bool? CheckRight(byte[] content) { - if (!base.CheckRight(content).Value) return false; + if (base.CheckRight(content) != true) return false; var fcsCheck = 0; if (content.Length == 1 && content[0] == 0xe5) return true; diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocolLinker.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocolLinker.cs index 1ad2e3f..6211972 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocolLinker.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocolLinker.cs @@ -26,7 +26,7 @@ namespace Modbus.Net.Siemens public SiemensTcpProtocolLinker(string ip, int port) : base(ip, port) { - ((BaseConnector)BaseConnector).AddController(new MatchController(new ICollection[] { new List { 11, 12 } }, 500)); + ((BaseConnector)BaseConnector).AddController(new MatchDirectlySendController(new ICollection<(int,int)>[] { new List<(int,int)> { (11,11), (12,12) } }, 500)); } /// @@ -36,7 +36,7 @@ namespace Modbus.Net.Siemens /// 报文是否正确 public override bool? CheckRight(byte[] content) { - if (!base.CheckRight(content).Value) return false; + if (base.CheckRight(content) != true) return false; switch (content[5]) { case 0xd0: diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensUtility.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensUtility.cs index b2e18d5..4188e44 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensUtility.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensUtility.cs @@ -67,7 +67,7 @@ namespace Modbus.Net.Siemens private readonly ushort _tsapDst; private ushort _sendCount; - private object _counterLock = new object(); + private readonly object _counterLock = new object(); private SiemensType _siemensType; diff --git a/Modbus.Net/Modbus.Net.Siemens/packages.config b/Modbus.Net/Modbus.Net.Siemens/packages.config new file mode 100644 index 0000000..f531b99 --- /dev/null +++ b/Modbus.Net/Modbus.Net.Siemens/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.Siemens/project.json b/Modbus.Net/Modbus.Net.Siemens/project.json new file mode 100644 index 0000000..b3f452a --- /dev/null +++ b/Modbus.Net/Modbus.Net.Siemens/project.json @@ -0,0 +1,5 @@ +{ + "runtimes": { + "win": {} + } +} diff --git a/Modbus.Net/Modbus.Net/BaseConnector.cs b/Modbus.Net/Modbus.Net/BaseConnector.cs index b14b680..cefc94e 100644 --- a/Modbus.Net/Modbus.Net/BaseConnector.cs +++ b/Modbus.Net/Modbus.Net/BaseConnector.cs @@ -39,7 +39,8 @@ namespace Modbus.Net public override async Task SendMsgAsync(byte[] message) { var ans = await SendMsgCtrl(message); - return ans?.ReceiveMessage; + if (ans == null) return new byte[0]; + return ans.ReceiveMessage; } /// diff --git a/Modbus.Net/Modbus.Net/BaseController.cs b/Modbus.Net/Modbus.Net/BaseController.cs index 2358b71..43ed15a 100644 --- a/Modbus.Net/Modbus.Net/BaseController.cs +++ b/Modbus.Net/Modbus.Net/BaseController.cs @@ -34,7 +34,7 @@ namespace Modbus.Net { var def = new MessageWaitingDef { - Key = GetKeyFromMessage(sendMessage), + Key = GetKeyFromMessage(sendMessage)?.Item1, SendMessage = sendMessage, SendMutex = new AutoResetEvent(false), ReceiveMutex = new AutoResetEvent(false) @@ -94,7 +94,7 @@ namespace Modbus.Net /// /// ȷϵϢ /// Ϣļؼ - protected abstract string GetKeyFromMessage(byte[] message); + protected abstract (string,string)? GetKeyFromMessage(byte[] message); /// public bool ConfirmMessage(byte[] receiveMessage) diff --git a/Modbus.Net/Modbus.Net/BaseProtocol.cs b/Modbus.Net/Modbus.Net/BaseProtocol.cs index 369f59c..2d7db2c 100644 --- a/Modbus.Net/Modbus.Net/BaseProtocol.cs +++ b/Modbus.Net/Modbus.Net/BaseProtocol.cs @@ -114,10 +114,8 @@ namespace Modbus.Net else { //自动寻找存在的协议并将其加载 - var protocalUnit = - Activator.CreateInstance(type.GetTypeInfo().Assembly - .GetType(protocalName)) as TProtocolUnit; - if (protocalUnit == null) + if (!(Activator.CreateInstance(type.GetTypeInfo().Assembly + .GetType(protocalName)) is TProtocolUnit protocalUnit)) throw new InvalidCastException($"No ProtocolUnit {nameof(TProtocolUnit)} implemented"); protocalUnit.Endian = Endian; Register(protocalUnit); diff --git a/Modbus.Net/Modbus.Net/FifoController.cs b/Modbus.Net/Modbus.Net/FifoController.cs index 58134fb..6cbf4a6 100644 --- a/Modbus.Net/Modbus.Net/FifoController.cs +++ b/Modbus.Net/Modbus.Net/FifoController.cs @@ -92,7 +92,7 @@ namespace Modbus.Net } /// - protected override string GetKeyFromMessage(byte[] message) + protected override (string,string)? GetKeyFromMessage(byte[] message) { return null; } diff --git a/Modbus.Net/Modbus.Net/MatchController.cs b/Modbus.Net/Modbus.Net/MatchController.cs index 252f811..aed4571 100644 --- a/Modbus.Net/Modbus.Net/MatchController.cs +++ b/Modbus.Net/Modbus.Net/MatchController.cs @@ -11,114 +11,47 @@ namespace Modbus.Net /// /// 通讯号匹配模式的控制器 /// - public class MatchController : BaseController + public class MatchController : FifoController { - private MessageWaitingDef _currentSendingPos; - - private bool _taskCancel = false; - - /// - /// 获取间隔 - /// - public int AcquireTime { get; } - /// /// 匹配字典 /// - protected ICollection[] KeyMatches { get; } + protected ICollection<(int,int)>[] KeyMatches { get; } /// /// 构造器 /// /// 匹配字典,每个Collection代表一个匹配集合,每一个匹配集合中的数字代表需要匹配的位置,最后计算出来的数字是所有位置数字按照集合排序后叠放在一起 /// 获取间隔 - public MatchController(ICollection[] keyMatches, int acquireTime) + public MatchController(ICollection<(int,int)>[] keyMatches, int acquireTime) : base(acquireTime) { KeyMatches = keyMatches; - AcquireTime = acquireTime; } /// - protected override void SendingMessageControlInner() + protected override (string,string)? GetKeyFromMessage(byte[] message) { - try - { - while (!_taskCancel) - { - if (AcquireTime > 0) - { - Thread.Sleep(AcquireTime); - } - lock (WaitingMessages) - { - if (_currentSendingPos == null) - { - if (WaitingMessages.Count > 0) - { - _currentSendingPos = WaitingMessages.First(); - _currentSendingPos.SendMutex.Set(); - } - } - if (_currentSendingPos != null) - { - if (WaitingMessages.Count <= 0) - { - _currentSendingPos = null; - } - if (WaitingMessages.Count > WaitingMessages.IndexOf(_currentSendingPos) + 1) - { - _currentSendingPos = WaitingMessages[WaitingMessages.IndexOf(_currentSendingPos) + 1]; - _currentSendingPos.SendMutex.Set(); - } - } - } - } - } - catch (ObjectDisposedException) - { - //ignore - } - catch (Exception e) - { - Log.Error(e, "Controller throws exception"); - } - - } - - /// - public override void SendStart() - { - _taskCancel = false; - base.SendStart(); - } - - /// - public override void SendStop() - { - _taskCancel = false; - } - - /// - protected override string GetKeyFromMessage(byte[] message) - { - string ans = ""; + string ans1 = ""; + string ans2 = ""; foreach (var matchPoses in KeyMatches) { - int tmpCount = 0; + int tmpCount = 0, tmpCount2 = 0; foreach (var matchPos in matchPoses) { - tmpCount = tmpCount * 256 + message[matchPos]; + tmpCount = tmpCount * 256 + message[matchPos.Item1]; + tmpCount2 = tmpCount2 * 256 + message[matchPos.Item2]; } - ans += tmpCount + " "; + ans1 += tmpCount + " "; + ans2 += tmpCount2 + " "; } - return ans; + return (ans1, ans2); } /// protected override MessageWaitingDef GetMessageFromWaitingList(byte[] receiveMessage) { var returnKey = GetKeyFromMessage(receiveMessage); - return WaitingMessages.FirstOrDefault(p=>p.Key == returnKey); + return WaitingMessages.FirstOrDefault(p=>returnKey.HasValue && p.Key == returnKey.Value.Item2); } } } diff --git a/Modbus.Net/Modbus.Net/MatchDirectlySendController.cs b/Modbus.Net/Modbus.Net/MatchDirectlySendController.cs new file mode 100644 index 0000000..8559ea4 --- /dev/null +++ b/Modbus.Net/Modbus.Net/MatchDirectlySendController.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Modbus.Net +{ + /// + /// 匹配控制器,载入队列后直接发送 + /// + public class MatchDirectlySendController : MatchController + { + /// + public MatchDirectlySendController(ICollection<(int, int)>[] keyMatches, int acquireTime) : base(keyMatches, + acquireTime) + { + } + + /// + protected override bool AddMessageToList(MessageWaitingDef def) + { + var ans = base.AddMessageToList(def); + if (ans) + { + def.SendMutex.Set(); + } + return ans; + } + + /// + protected override void SendingMessageControlInner() + { + //empty + } + } +} diff --git a/Modbus.Net/Modbus.Net/Modbus.Net-vs2015.csproj b/Modbus.Net/Modbus.Net/Modbus.Net-vs2015.csproj new file mode 100644 index 0000000..c21d71d --- /dev/null +++ b/Modbus.Net/Modbus.Net/Modbus.Net-vs2015.csproj @@ -0,0 +1,124 @@ + + + + + Debug + AnyCPU + {0C662F94-7362-4D4F-AFD7-76562764893C} + Library + Properties + Modbus.Net + Modbus.Net + v4.5 + 512 + + + + + + true + full + false + bin\Debug\ + TRACE;DEBUG;NET45 + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE;NET45 + prompt + 4 + + + + ..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.dll + + + ..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.Extensions.dll + + + ..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.Extensions.Desktop.dll + + + ..\packages\Nito.AsyncEx.4.0.1\lib\net45\Nito.AsyncEx.dll + + + ..\packages\Nito.AsyncEx.4.0.1\lib\net45\Nito.AsyncEx.Concurrent.dll + + + ..\packages\Nito.AsyncEx.4.0.1\lib\net45\Nito.AsyncEx.Enlightenment.dll + + + ..\packages\Serilog.2.6.0\lib\net45\Serilog.dll + + + + + + + ..\packages\System.ValueTuple.4.4.0\lib\netstandard1.0\System.ValueTuple.dll + + + + + + + + + + + + + + + + + + + + + + Component + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Designer + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net/Modbus.Net.csproj b/Modbus.Net/Modbus.Net/Modbus.Net.csproj index fe806af..53b38d7 100644 --- a/Modbus.Net/Modbus.Net/Modbus.Net.csproj +++ b/Modbus.Net/Modbus.Net/Modbus.Net.csproj @@ -26,8 +26,13 @@ bin\Debug\Modbus.Net.xml + + + + + @@ -46,5 +51,9 @@ + + + + \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net/Properties/AssemblyInfo.cs b/Modbus.Net/Modbus.Net/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..acd8e07 --- /dev/null +++ b/Modbus.Net/Modbus.Net/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Modbus.Net")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Modbus.Net")] +[assembly: AssemblyCopyright("")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("07bcb9ac-a522-4348-8774-2825fd8a6e64")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("0.0.0.1")] +[assembly: AssemblyFileVersion("0.0.0.1")] diff --git a/Modbus.Net/Modbus.Net/ProtocalLinker.cs b/Modbus.Net/Modbus.Net/ProtocalLinker.cs index 1521db7..91f77d3 100644 --- a/Modbus.Net/Modbus.Net/ProtocalLinker.cs +++ b/Modbus.Net/Modbus.Net/ProtocalLinker.cs @@ -65,8 +65,19 @@ namespace Modbus.Net IProtocolLinkerBytesExtend; return bytesExtend?.BytesDecact(content); } - } + public override bool? CheckRight(byte[] content) + { + if (content == null) + { + Disconnect(); + return false; + } + if (content.Length == 0) return null; + return true; + } + } + /// /// 基本的协议连接器 /// diff --git a/Modbus.Net/Modbus.Net/ProtocalUnit.cs b/Modbus.Net/Modbus.Net/ProtocalUnit.cs index db44eea..6de5503 100644 --- a/Modbus.Net/Modbus.Net/ProtocalUnit.cs +++ b/Modbus.Net/Modbus.Net/ProtocalUnit.cs @@ -53,7 +53,11 @@ namespace Modbus.Net /// 结构化的输出数据 public T Unformat(TParamOut messageBytes, ref int pos) where T : class, IOutputStruct { - return Unformat(messageBytes, ref pos) as T; + if (messageBytes != null) + { + return Unformat(messageBytes, ref pos) as T; + } + return null; } /// diff --git a/Modbus.Net/Modbus.Net/package-lock.json b/Modbus.Net/Modbus.Net/package-lock.json new file mode 100644 index 0000000..48e341a --- /dev/null +++ b/Modbus.Net/Modbus.Net/package-lock.json @@ -0,0 +1,3 @@ +{ + "lockfileVersion": 1 +} diff --git a/Modbus.Net/Modbus.Net/packages.config b/Modbus.Net/Modbus.Net/packages.config new file mode 100644 index 0000000..dd574be --- /dev/null +++ b/Modbus.Net/Modbus.Net/packages.config @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net/project.json b/Modbus.Net/Modbus.Net/project.json new file mode 100644 index 0000000..b3f452a --- /dev/null +++ b/Modbus.Net/Modbus.Net/project.json @@ -0,0 +1,5 @@ +{ + "runtimes": { + "win": {} + } +} diff --git a/Tests/Modbus.Net.PersistedTests/Program.cs b/Tests/Modbus.Net.PersistedTests/Program.cs index 5d88624..81171a4 100644 --- a/Tests/Modbus.Net.PersistedTests/Program.cs +++ b/Tests/Modbus.Net.PersistedTests/Program.cs @@ -12,7 +12,7 @@ namespace Modbus.Net.PersistedTests { Log.Logger = new LoggerConfiguration().MinimumLevel.Verbose().WriteTo.Console().CreateLogger(); - IMachineProperty machine = new ModbusMachine(1, ModbusType.Rtu, "COM1", + IMachineProperty machine = new ModbusMachine(1, ModbusType.Ascii, "COM5", new List() { new AddressUnit() @@ -40,65 +40,9 @@ namespace Modbus.Net.PersistedTests DataType = typeof(ushort) }, }, true, 2, 1); - IMachineProperty machine2 = new ModbusMachine(2, ModbusType.Rtu, "COM1", - new List() - { - new AddressUnit() - { - Id = "1", - Area = "4X", - Address = 11, - Name = "test 1", - DataType = typeof(ushort) - }, - new AddressUnit() - { - Id = "2", - Area = "4X", - Address = 12, - Name = "test 2", - DataType = typeof(ushort) - }, - new AddressUnit() - { - Id = "3", - Area = "4X", - Address = 13, - Name = "test 3", - DataType = typeof(ushort) - }, - }, true, 3, 1); - IMachineProperty machine3 = new ModbusMachine(3, ModbusType.Rtu, "COM1", - new List() - { - new AddressUnit() - { - Id = "1", - Area = "4X", - Address = 21, - Name = "test 1", - DataType = typeof(ushort) - }, - new AddressUnit() - { - Id = "2", - Area = "4X", - Address = 22, - Name = "test 2", - DataType = typeof(ushort) - }, - new AddressUnit() - { - Id = "3", - Area = "4X", - Address = 23, - Name = "test 3", - DataType = typeof(ushort) - }, - }, true, 4, 1); - + TaskManager manager = new TaskManager(20, true); - manager.AddMachines(new List>{machine, machine2, machine3}); + manager.AddMachines(new List>{machine}); Random r = new Random(); manager.InvokeTimerForMachine(1, new TaskItemSetData(() => new Dictionary {