@@ -0,0 +1,80 @@ | |||
<?xml version="1.0" encoding="utf-8"?> | |||
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |||
<ItemGroup Label="ProjectConfigurations"> | |||
<ProjectConfiguration Include="Debug|Win32"> | |||
<Configuration>Debug</Configuration> | |||
<Platform>Win32</Platform> | |||
</ProjectConfiguration> | |||
<ProjectConfiguration Include="Release|Win32"> | |||
<Configuration>Release</Configuration> | |||
<Platform>Win32</Platform> | |||
</ProjectConfiguration> | |||
</ItemGroup> | |||
<PropertyGroup Label="Globals"> | |||
<ProjectGuid>{8C67F4EB-BDBE-4605-9291-5A535AC44AE2}</ProjectGuid> | |||
<RootNamespace>Modbus_RTU_Salve</RootNamespace> | |||
</PropertyGroup> | |||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | |||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> | |||
<ConfigurationType>Application</ConfigurationType> | |||
<UseDebugLibraries>true</UseDebugLibraries> | |||
<PlatformToolset>v120</PlatformToolset> | |||
<CharacterSet>MultiByte</CharacterSet> | |||
</PropertyGroup> | |||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | |||
<ConfigurationType>Application</ConfigurationType> | |||
<UseDebugLibraries>false</UseDebugLibraries> | |||
<PlatformToolset>v120</PlatformToolset> | |||
<WholeProgramOptimization>true</WholeProgramOptimization> | |||
<CharacterSet>MultiByte</CharacterSet> | |||
</PropertyGroup> | |||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> | |||
<ImportGroup Label="ExtensionSettings"> | |||
</ImportGroup> | |||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | |||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | |||
</ImportGroup> | |||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | |||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | |||
</ImportGroup> | |||
<PropertyGroup Label="UserMacros" /> | |||
<PropertyGroup /> | |||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | |||
<ClCompile> | |||
<WarningLevel>Level3</WarningLevel> | |||
<Optimization>Disabled</Optimization> | |||
<SDLCheck>true</SDLCheck> | |||
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions> | |||
</ClCompile> | |||
<Link> | |||
<GenerateDebugInformation>true</GenerateDebugInformation> | |||
</Link> | |||
</ItemDefinitionGroup> | |||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | |||
<ClCompile> | |||
<WarningLevel>Level3</WarningLevel> | |||
<Optimization>MaxSpeed</Optimization> | |||
<FunctionLevelLinking>true</FunctionLevelLinking> | |||
<IntrinsicFunctions>true</IntrinsicFunctions> | |||
<SDLCheck>true</SDLCheck> | |||
</ClCompile> | |||
<Link> | |||
<GenerateDebugInformation>true</GenerateDebugInformation> | |||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | |||
<OptimizeReferences>true</OptimizeReferences> | |||
</Link> | |||
</ItemDefinitionGroup> | |||
<ItemGroup> | |||
<ClCompile Include="common.cpp" /> | |||
<ClCompile Include="main.cpp" /> | |||
<ClCompile Include="RTU_Salve.cpp" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<ClInclude Include="common.h" /> | |||
<ClInclude Include="main.h" /> | |||
<ClInclude Include="RTU_Salve.h" /> | |||
</ItemGroup> | |||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | |||
<ImportGroup Label="ExtensionTargets"> | |||
</ImportGroup> | |||
</Project> |
@@ -0,0 +1,39 @@ | |||
<?xml version="1.0" encoding="utf-8"?> | |||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |||
<ItemGroup> | |||
<Filter Include="源文件"> | |||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> | |||
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions> | |||
</Filter> | |||
<Filter Include="头文件"> | |||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> | |||
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions> | |||
</Filter> | |||
<Filter Include="资源文件"> | |||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> | |||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> | |||
</Filter> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<ClCompile Include="main.cpp"> | |||
<Filter>源文件</Filter> | |||
</ClCompile> | |||
<ClCompile Include="RTU_Salve.cpp"> | |||
<Filter>源文件</Filter> | |||
</ClCompile> | |||
<ClCompile Include="common.cpp"> | |||
<Filter>源文件</Filter> | |||
</ClCompile> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<ClInclude Include="main.h"> | |||
<Filter>头文件</Filter> | |||
</ClInclude> | |||
<ClInclude Include="RTU_Salve.h"> | |||
<Filter>头文件</Filter> | |||
</ClInclude> | |||
<ClInclude Include="common.h"> | |||
<Filter>头文件</Filter> | |||
</ClInclude> | |||
</ItemGroup> | |||
</Project> |
@@ -0,0 +1,276 @@ | |||
2020-9-3 22:1:6 Recv:09 0F 00 00 07 B1 F6 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 20 DB | |||
2020-9-3 22:1:6 Send:09 8F 03 85 F3 | |||
2020-9-3 22:1:7 Recv:09 0F 00 00 07 B1 F6 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 20 DB | |||
2020-9-3 22:1:7 Send:09 8F 03 85 F3 | |||
2020-9-3 22:1:8 Recv:09 0F 00 00 07 B1 F6 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 20 DB | |||
2020-9-3 22:1:8 Send:09 8F 03 85 F3 | |||
2020-9-3 22:1:9 Recv:09 0F 00 00 07 B1 F6 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 20 DB | |||
2020-9-3 22:1:9 Send:09 8F 03 85 F3 | |||
2020-9-3 22:1:10 Recv:09 0F 00 00 07 B1 F6 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 20 DB | |||
2020-9-3 22:1:10 Send:09 8F 03 85 F3 | |||
2020-9-3 22:1:32 Recv:09 0F 00 00 07 B0 F6 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 B8 78 | |||
2020-9-3 22:1:32 Send:09 0F 00 00 07 B0 57 07 | |||
2020-9-7 9:33:32 Recv:09 0F 00 00 07 B0 F6 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 B8 78 | |||
2020-9-7 9:33:32 Send:09 0F 00 00 07 B0 57 07 | |||
2020-9-7 9:33:33 Recv:09 0F 00 00 07 B0 F6 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 B8 78 | |||
2020-9-7 9:33:33 Send:09 0F 00 00 07 B0 57 07 | |||
2020-9-7 9:33:43 Recv:09 0F 00 00 07 B0 F6 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 B8 78 | |||
2020-9-7 9:33:43 Send:09 0F 00 00 07 B0 57 07 | |||
2020-9-7 9:36:22 Recv:09 01 00 00 00 00 3D 42 | |||
2020-9-7 9:36:22 Send:09 81 03 81 93 | |||
2020-9-7 9:37:0 Recv:09 01 00 00 00 C8 3C D4 | |||
2020-9-7 9:37:0 Send:09 01 19 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 E4 D7 | |||
2020-9-7 9:37:51 Recv:09 01 00 00 00 0A BD 45 | |||
2020-9-7 9:37:51 Send:09 01 02 00 00 58 3D | |||
2020-9-7 9:37:52 Recv:09 01 00 00 00 0A BD 45 | |||
2020-9-7 9:37:52 Send:09 01 02 00 00 58 3D | |||
2020-9-7 9:38:9 Recv:09 01 00 00 00 01 FC 82 | |||
2020-9-7 9:38:9 Send:09 01 01 00 53 E8 | |||
2020-9-7 9:41:50 Recv:09 01 00 00 00 01 00 82 41 | |||
2020-9-7 9:41:50 Send:09 81 03 81 93 | |||
2020-9-7 9:50:41 Recv:09 01 27 0F 00 02 86 34 | |||
2020-9-7 9:50:41 Send:09 81 02 40 53 | |||
2020-9-7 9:52:58 Recv:09 01 27 06 00 14 D7 F8 | |||
2020-9-7 9:52:58 Send:09 81 02 40 53 | |||
2020-9-7 9:52:59 Recv:09 01 27 06 00 14 D7 F8 | |||
2020-9-7 9:52:59 Send:09 81 02 40 53 | |||
2020-9-7 10:1:34 Recv:09 03 00 00 00 01 00 83 A3 | |||
2020-9-7 10:1:34 Send:09 83 03 80 F3 | |||
2020-9-7 10:4:34 Recv:09 03 27 0F 00 02 FF F4 | |||
2020-9-7 10:4:34 Send:09 83 02 41 33 | |||
2020-9-7 10:5:16 Recv:09 03 27 06 00 14 AE 38 | |||
2020-9-7 10:5:16 Send:09 83 02 41 33 | |||
2020-9-7 10:13:25 Recv:09 0F 00 00 00 01 01 01 EE F1 | |||
2020-9-7 10:13:25 Send:09 0F 00 00 00 01 95 43 | |||
2020-9-7 10:13:46 Recv:09 0F 00 00 00 02 01 00 DF 31 | |||
2020-9-7 10:13:46 Send:09 0F 00 00 00 02 D5 42 | |||
2020-9-7 10:13:58 Recv:09 0F 00 00 07 B0 F6 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 B8 78 | |||
2020-9-7 10:13:58 Send:09 0F 00 00 07 B0 57 07 | |||
2020-9-7 10:14:15 Recv:09 0F 00 00 07 B1 F6 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 20 DB | |||
2020-9-7 10:14:15 Send:09 8F 03 85 F3 | |||
2020-9-7 10:16:15 Recv:09 0F 00 00 00 00 54 83 | |||
2020-9-7 10:16:15 Send:09 8F 03 85 F3 | |||
2020-9-7 10:19:59 Recv:09 0F 27 0F 00 02 01 01 4C 27 | |||
2020-9-7 10:19:59 Send:09 8F 02 44 33 | |||
2020-9-7 10:20:10 Recv:09 0F 27 0F 00 02 01 01 4C 27 | |||
2020-9-7 10:20:10 Send:09 8F 02 44 33 | |||
2020-9-7 10:20:10 Recv:09 0F 27 0F 00 02 01 01 4C 27 | |||
2020-9-7 10:20:10 Send:09 8F 02 44 33 | |||
2020-9-7 10:20:10 Recv:09 0F 27 0F 00 02 01 01 4C 27 | |||
2020-9-7 10:20:10 Send:09 8F 02 44 33 | |||
2020-9-7 10:20:10 Recv:09 0F 27 0F 00 02 01 01 4C 27 | |||
2020-9-7 10:20:10 Send:09 8F 02 44 33 | |||
2020-9-7 10:20:10 Recv:09 0F 27 0F 00 02 01 01 4C 27 | |||
2020-9-7 10:20:10 Send:09 8F 02 44 33 | |||
2020-9-7 10:20:10 Recv:09 0F 27 0F 00 02 01 01 4C 27 | |||
2020-9-7 10:20:10 Send:09 8F 02 44 33 | |||
2020-9-7 10:21:20 Recv:09 0F 27 06 00 14 03 02 FF FF 54 11 | |||
2020-9-7 10:21:20 Send:09 8F 02 44 33 | |||
2020-9-7 10:21:21 Recv:09 0F 27 06 00 14 03 02 FF FF 54 11 | |||
2020-9-7 10:21:21 Send:09 8F 02 44 33 | |||
2020-9-7 10:24:52 Recv:09 10 00 00 00 01 02 00 00 C1 90 | |||
2020-9-7 10:24:52 Send:09 10 00 00 00 01 00 81 | |||
2020-9-7 10:25:8 Recv:09 10 00 00 00 02 04 00 00 00 00 D9 CF | |||
2020-9-7 10:25:8 Send:09 10 00 00 00 02 40 80 | |||
2020-9-7 10:25:22 Recv:09 10 00 00 00 7B F6 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 CE 42 | |||
2020-9-7 10:25:22 Send:09 10 00 00 00 7B 81 62 | |||
2020-9-7 10:27:18 Recv:09 10 00 00 00 00 C1 41 | |||
2020-9-7 10:27:18 Send:09 90 03 8D C3 | |||
2020-9-7 10:35:5 Recv:09 10 27 0F 00 02 04 00 00 00 00 26 7E | |||
2020-9-7 10:35:5 Send:09 90 02 4C 03 | |||
2020-9-7 10:35:16 Recv:09 10 27 0F 00 02 04 00 00 00 00 26 7E | |||
2020-9-7 10:35:16 Send:09 90 02 4C 03 | |||
2020-9-7 10:35:17 Recv:09 10 27 0F 00 02 04 00 00 00 00 26 7E | |||
2020-9-7 10:35:17 Send:09 90 02 4C 03 | |||
2020-9-7 10:35:17 Recv:09 10 27 0F 00 02 04 00 00 00 00 26 7E | |||
2020-9-7 10:35:17 Send:09 90 02 4C 03 | |||
2020-9-7 10:35:17 Recv:09 10 27 0F 00 02 04 00 00 00 00 26 7E | |||
2020-9-7 10:35:17 Send:09 90 02 4C 03 | |||
2020-9-7 10:35:17 Recv:09 10 27 0F 00 02 04 00 00 00 00 26 7E | |||
2020-9-7 10:35:17 Send:09 90 02 4C 03 | |||
2020-9-7 10:35:17 Recv:09 10 27 0F 00 02 04 00 00 00 00 26 7E | |||
2020-9-7 10:35:17 Send:09 90 02 4C 03 | |||
2020-9-7 10:37:47 Recv:09 10 27 06 00 14 28 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 E1 83 | |||
2020-9-7 10:37:47 Send:09 90 03 8D C3 | |||
2020-9-7 10:39:25 Recv:09 10 27 06 00 14 28 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 D9 BB | |||
2020-9-7 10:39:25 Send:09 90 02 4C 03 | |||
2020-9-7 10:55:43 Recv:09 10 00 00 00 01 02 00 00 00 51 90 | |||
2020-9-7 10:55:43 Send:09 90 03 8D C3 | |||
2020-9-7 10:55:44 Recv:09 10 00 00 00 01 02 00 00 00 51 90 | |||
2020-9-7 10:55:44 Send:09 90 03 8D C3 | |||
2020-9-7 10:55:44 Recv:09 10 00 00 00 01 02 00 00 00 51 90 | |||
2020-9-7 10:55:44 Send:09 90 03 8D C3 | |||
2020-9-7 10:55:45 Recv:09 10 00 00 00 01 02 00 00 00 51 90 | |||
2020-9-7 10:55:45 Send:09 90 03 8D C3 | |||
2020-9-7 10:55:45 Recv:09 10 00 00 00 01 02 00 00 00 51 90 | |||
2020-9-7 10:55:45 Send:09 90 03 8D C3 | |||
2020-9-7 10:56:34 Recv:09 10 00 00 00 01 02 00 00 00 51 90 | |||
2020-9-7 10:56:34 Send:09 90 03 8D C3 | |||
2020-9-7 10:56:35 Recv:09 10 00 00 00 01 02 00 00 00 51 90 | |||
2020-9-7 10:56:35 Send:09 90 03 8D C3 | |||
2020-9-7 10:56:35 Recv:09 10 00 00 00 01 02 00 00 00 51 90 | |||
2020-9-7 10:56:35 Send:09 90 03 8D C3 | |||
2020-9-7 11:2:25 Recv:09 10 00 00 00 01 02 00 00 00 51 90 | |||
2020-9-7 11:2:25 Send:09 90 03 8D C3 | |||
2020-9-7 11:2:25 Recv:09 10 00 00 00 01 02 00 00 00 51 90 | |||
2020-9-7 11:2:25 Send:09 90 03 8D C3 | |||
2020-9-7 11:2:25 Recv:09 10 00 00 00 01 02 00 00 00 51 90 | |||
2020-9-7 11:2:25 Send:09 90 03 8D C3 | |||
2020-9-7 11:3:37 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 11:3:37 Send:09 8F 03 85 F3 | |||
2020-9-7 11:5:12 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 11:5:12 Send:09 8F 03 85 F3 | |||
2020-9-7 11:5:12 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 11:5:12 Send:09 8F 03 85 F3 | |||
2020-9-7 11:5:13 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 11:5:13 Send:09 8F 03 85 F3 | |||
2020-9-7 11:5:14 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 11:5:14 Send:09 8F 03 85 F3 | |||
2020-9-7 12:40:39 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 12:40:39 Send:09 8F 03 85 F3 | |||
2020-9-7 12:40:39 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 12:40:39 Send:09 8F 03 85 F3 | |||
2020-9-7 12:40:39 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 12:40:39 Send:09 8F 03 85 F3 | |||
2020-9-7 12:40:39 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 12:40:39 Send:09 8F 03 85 F3 | |||
2020-9-7 12:40:39 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 12:40:39 Send:09 8F 03 85 F3 | |||
2020-9-7 12:40:40 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 12:40:40 Send:09 8F 03 85 F3 | |||
2020-9-7 12:40:40 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 12:40:40 Send:09 8F 03 85 F3 | |||
2020-9-7 12:40:40 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 12:40:40 Send:09 8F 03 85 F3 | |||
2020-9-7 12:40:40 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 12:40:40 Send:09 8F 03 85 F3 | |||
2020-9-7 12:40:40 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 12:40:40 Send:09 8F 03 85 F3 | |||
2020-9-7 13:6:3 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:3 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:4 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:4 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:4 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:4 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:5 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:5 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:5 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:5 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:5 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:5 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:5 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:5 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:5 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:5 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:6 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:6 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:6 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:6 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:6 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:6 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:6 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:6 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:6 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:6 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:7 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:7 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:7 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:7 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:7 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:7 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:7 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:7 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:7 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:7 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:8 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:8 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:8 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:8 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:8 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:8 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:8 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:8 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:8 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:8 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:9 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:9 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:9 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:9 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:9 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:9 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:9 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:9 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:9 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:9 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:10 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:10 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:10 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:10 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:10 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:10 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:10 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:10 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:10 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:10 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:11 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:11 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:11 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:11 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:11 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:11 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:11 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:11 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:11 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:11 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:11 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:11 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:12 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:12 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:12 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:12 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:12 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:12 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:12 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:12 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:12 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:12 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:13 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:13 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:13 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:13 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:13 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:13 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:13 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:13 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:13 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:13 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:14 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:14 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:14 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:14 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:14 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:14 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:14 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:14 Send:09 8F 04 C4 31 | |||
2020-9-7 13:6:14 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:6:14 Send:09 8F 04 C4 31 | |||
2020-9-7 13:7:2 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:7:2 Send:09 8F 04 C4 31 | |||
2020-9-7 13:7:2 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:7:2 Send:09 8F 04 C4 31 | |||
2020-9-7 13:7:3 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:7:3 Send:09 8F 04 C4 31 | |||
2020-9-7 13:7:3 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:7:3 Send:09 8F 04 C4 31 | |||
2020-9-7 13:7:3 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:7:3 Send:09 8F 04 C4 31 | |||
2020-9-7 13:7:3 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:7:3 Send:09 8F 04 C4 31 | |||
2020-9-7 13:7:3 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:7:3 Send:09 8F 04 C4 31 | |||
2020-9-7 13:7:4 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:7:4 Send:09 8F 04 C4 31 | |||
2020-9-7 13:7:4 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:7:4 Send:09 8F 04 C4 31 | |||
2020-9-7 13:7:4 Recv:09 0F 00 00 00 01 01 01 00 71 4C | |||
2020-9-7 13:7:4 Send:09 8F 04 C4 31 |
@@ -0,0 +1,390 @@ | |||
#include "RTU_Salve.h" | |||
#include <bitset> | |||
char read_buf[MAX_NUMBER]; | |||
bitset<MAX_Address> Coil_date; //0-9999 | |||
UINT16 Register[MAX_Address]; // 0-9999 | |||
unsigned int Response_Message_Len; | |||
unsigned int RTU_Enable = 0; | |||
/********************************************************************************************* | |||
* 功能 : 初始化线圈和寄存器 | |||
* 描述 : 对线圈和寄存器数组赋值为全1 | |||
* 输入 : 无 | |||
* 返回值 : 无 | |||
**********************************************************************************************/ | |||
void Init_Coil_Register(void) | |||
{ | |||
for (int i = 0; i < MAX_Address; i++) | |||
{ | |||
Coil_date[i] = 1; | |||
Register[i] = 0xFFFF; | |||
} | |||
} | |||
/********************************************************************************************* | |||
* 功能 : Bitset转UINT8类型 | |||
* 描述 : Bitset ==》UINT8 | |||
* 输入 : Bitset_Address Bitset的起始地址 Read_Number 要读取的位数 | |||
* 返回值 : Date 转换后的UNIT8数据 | |||
**********************************************************************************************/ | |||
UINT8 Bitset_to_Uint8(unsigned int Bitset_Address, unsigned int Read_Number) | |||
{ | |||
UINT8 Date = 0x00; | |||
if (Read_Number >= 8) | |||
{ | |||
unsigned int len = Bitset_Address + 8; | |||
for (unsigned int i = 0; i < 8; i++) | |||
{ | |||
Date = Date << 1 | (int)Coil_date[len--]; | |||
} | |||
} | |||
else | |||
{ | |||
unsigned int len = Bitset_Address + Read_Number-1; | |||
for (unsigned int i = 0; i < Read_Number; i++) | |||
{ | |||
Date = Date << 1 | (int)Coil_date[len--]; | |||
} | |||
} | |||
return Date; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 生成异常码响应报文 | |||
* 描述 : 对不支持的功能码生成对应的异常响应报文 | |||
* 输入 : *Requst_Message 请求报文 *Response_Message响应报文 | |||
* 返回值 : 无 | |||
**********************************************************************************************/ | |||
void Create_Abnormal_Code_Response_Message(UINT8 *Requst_Message, UINT8 *Response_Message, UINT8 Abnormal_Code) | |||
{ | |||
Response_Message[0] = Device_ID; | |||
Response_Message[1] = Requst_Message[1] + 0x80; | |||
Response_Message[2] = Abnormal_Code; | |||
Response_Message_Len = 3; | |||
UINT16 CRC_date = CRC_16(Response_Message, 3); | |||
Response_Message[3] = CRC_date >> 8;//CRC_H | |||
Response_Message[4] = (UINT8)CRC_date; //CRC_L | |||
Response_Message_Len = 5; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 生成0x01功能码响应报文 | |||
* 描述 : 通过判断地址范围生成0x01对应的响应报文 | |||
* 输入 : *Requst_Message 请求报文 *Response_Message响应报文 | |||
* 返回值 : 无 | |||
**********************************************************************************************/ | |||
void Create_0x01_Response_Message(UINT8 *Requst_Message, UINT8 *Response_Message) | |||
{ | |||
unsigned int Start_Address = Requst_Message[2] << 8 | Requst_Message[3]; | |||
unsigned int Address_range = Start_Address + (Requst_Message[4] << 8 | Requst_Message[5]); | |||
if (Address_range < MAX_Address)//判断地址是否超限 | |||
{ | |||
unsigned int Read_Number = Requst_Message[4] << 8 | Requst_Message[5];//要读取的位数 | |||
unsigned int Read_Len = Count_Read_date_number(Requst_Message[1], Read_Number); //要读取的字节数 | |||
for (unsigned int i = 3; i < Read_Len + 3; i++) | |||
{ | |||
Response_Message[i] = Bitset_to_Uint8(Start_Address, Read_Number); | |||
Start_Address += 8; | |||
Read_Number -= 8; | |||
} | |||
Response_Message[0] = Device_ID; | |||
Response_Message[1] = Requst_Message[1]; | |||
Response_Message[2] = Read_Len; //响应报文中的后续字节数 | |||
UINT16 CRC_date = CRC_16(Response_Message, Read_Len+3); | |||
Response_Message_Len = Read_Len + 2 + 3; | |||
Response_Message[Response_Message_Len - 2] = CRC_date>>8;//CRC_H | |||
Response_Message[Response_Message_Len - 1] = (UINT8)CRC_date; //CRC_L | |||
} | |||
else | |||
{ | |||
Create_Abnormal_Code_Response_Message(Requst_Message, Response_Message,0x02); | |||
} | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 生成0x03功能码响应报文 | |||
* 描述 : 通过判断地址范围生成0x03对应的响应报文 | |||
* 输入 : *Requst_Message 请求报文 *Response_Message响应报文 | |||
* 返回值 : 无 | |||
**********************************************************************************************/ | |||
void Create_0x03_Response_Message(UINT8 *Requst_Message, UINT8 *Response_Message) | |||
{ | |||
unsigned int Start_Address = Requst_Message[2] << 8 | Requst_Message[3]; | |||
unsigned int Address_range = Start_Address + (Requst_Message[4] << 8 | Requst_Message[5]); | |||
if (Address_range < MAX_Address) //判断地址是否超限 | |||
{ | |||
unsigned int Read_Number = Requst_Message[4] << 8 | Requst_Message[5];//要读取的寄存器数量 | |||
unsigned int Read_Len = Count_Read_date_number(Requst_Message[1 | |||
], Read_Number); //要读取的字节数 | |||
for (unsigned int i = 3; i < Read_Len + 3; i= i + 2) | |||
{ | |||
Response_Message[i] = Register[Start_Address] >> 8; | |||
Response_Message[i + 1] = (UINT8)Register[Start_Address]; | |||
Start_Address += 1; | |||
} | |||
Response_Message[0] = Device_ID; | |||
Response_Message[1] = Requst_Message[1]; | |||
Response_Message[2] = Read_Len; //响应报文中的后续字节数 | |||
UINT16 CRC_date = CRC_16(Response_Message, Read_Len + 3); | |||
Response_Message_Len = Read_Len + 2 + 3; | |||
Response_Message[Response_Message_Len - 2] = CRC_date >> 8;//CRC_H | |||
Response_Message[Response_Message_Len - 1] = (UINT8)CRC_date; //CRC_L | |||
} | |||
else //地址超限 | |||
{ | |||
Create_Abnormal_Code_Response_Message(Requst_Message, Response_Message, 0x02); | |||
} | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 写入线圈 | |||
* 描述 : 将数据写入线圈中 | |||
* 输入 : Write_date_Message 要写入的数据 Write_Number要写入的位数 Start_Address起始地址 | |||
* 返回值 : 无 | |||
**********************************************************************************************/ | |||
void Write_Coil_date(UINT8 Write_date_Message, unsigned int Write_Number, unsigned int Start_Address) | |||
{ | |||
if (Write_Number >= 8) | |||
{ | |||
for (unsigned int i = Start_Address; i < Start_Address+8; i++) | |||
{ | |||
Coil_date[i] = Write_date_Message & 1; | |||
Write_date_Message = Write_date_Message >> 1; | |||
} | |||
} | |||
else | |||
{ | |||
for (unsigned int i = Start_Address; i < Start_Address + Write_Number; i++) | |||
{ | |||
Coil_date[i] = Write_date_Message & 1; | |||
Write_date_Message = Write_date_Message >> 1; | |||
} | |||
} | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 生成0x0F功能码响应报文 | |||
* 描述 : 通过判断地址范围生成0x0F对应的响应报文 | |||
* 输入 : *Requst_Message 请求报文 *Response_Message响应报文 | |||
* 返回值 : 无 | |||
**********************************************************************************************/ | |||
void Create_0x0F_Response_Message(UINT8 *Requst_Message, UINT8 *Response_Message) | |||
{ | |||
unsigned int Start_Address = Requst_Message[2] << 8 | Requst_Message[3]; | |||
unsigned int Address_range = Start_Address + (Requst_Message[4] << 8 | Requst_Message[5]); | |||
if (Address_range < MAX_Address)//判断地址是否超限 | |||
{ | |||
unsigned int Write_Number = Requst_Message[4] << 8 | Requst_Message[5];//要写入的位数 | |||
for (int i = 7; i < Requst_Message[6]+7; i++)//执行写入线圈操作 | |||
{ | |||
Write_Coil_date(Requst_Message[i], Write_Number, Start_Address); | |||
Write_Number -= 8; | |||
Start_Address += 8; | |||
} | |||
Response_Message[0] = Device_ID; | |||
Response_Message[1] = Requst_Message[1]; | |||
Response_Message[2] = Requst_Message[2]; | |||
Response_Message[3] = Requst_Message[3]; | |||
Response_Message[4] = Requst_Message[4]; | |||
Response_Message[5] = Requst_Message[5]; | |||
UINT16 CRC_date = CRC_16(Response_Message, 6); | |||
Response_Message[6] = CRC_date >> 8;//CRC_H | |||
Response_Message[7] = (UINT8)CRC_date; //CRC_L | |||
Response_Message_Len = 8; | |||
} | |||
else //地址超限 | |||
{ | |||
Create_Abnormal_Code_Response_Message(Requst_Message, Response_Message, 0x02); | |||
} | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 生成0x10功能码响应报文 | |||
* 描述 : 通过判断地址范围生成0x10对应的响应报文 | |||
* 输入 : *Requst_Message 请求报文 *Response_Message响应报文 | |||
* 返回值 : 无 | |||
**********************************************************************************************/ | |||
void Create_0x10_Response_Message(UINT8 *Requst_Message, UINT8 *Response_Message) | |||
{ | |||
unsigned int Start_Address = Requst_Message[2] << 8 | Requst_Message[3]; | |||
unsigned int Address_range = Start_Address + (Requst_Message[4] << 8 | Requst_Message[5]); | |||
if (Address_range < MAX_Address)//判断地址是否超限 | |||
{ | |||
unsigned int Write_Number = Requst_Message[4] << 8 | Requst_Message[5];//要写入的寄存器个数 | |||
for (int i = 7; i < Requst_Message[6] + 7; i = i + 2)//执行写入寄存器操作 | |||
{ | |||
Register[Start_Address] = Requst_Message[i] << 8 | Requst_Message[i+1]; | |||
Start_Address++; | |||
} | |||
Response_Message[0] = Device_ID; | |||
Response_Message[1] = Requst_Message[1]; | |||
Response_Message[2] = Requst_Message[2]; | |||
Response_Message[3] = Requst_Message[3]; | |||
Response_Message[4] = Requst_Message[4]; | |||
Response_Message[5] = Requst_Message[5]; | |||
UINT16 CRC_date = CRC_16(Response_Message, 6); | |||
Response_Message[6] = CRC_date >> 8;//CRC_H | |||
Response_Message[7] = (UINT8)CRC_date; //CRC_L | |||
Response_Message_Len = 8; | |||
} | |||
else //地址超限 | |||
{ | |||
Create_Abnormal_Code_Response_Message(Requst_Message, Response_Message, 0x02); | |||
} | |||
} | |||
/********************************************************************************************* | |||
* 功能 : CRC校验 | |||
* 描述 : 对请求报文中的数据进行CRC校验 | |||
* 输入 : *Requst_Message 请求报文 Read_len 接收到到的字节数 | |||
* 返回值 : true CRC校验通过 | |||
* false CRC校验不通过 | |||
**********************************************************************************************/ | |||
bool Check_Requst_Message_CRC(UINT8 *Requst_Message, DWORD Read_len) | |||
{ | |||
UINT16 CRC_data = CRC_16(Requst_Message, Read_len-2); | |||
UINT16 Message_CRC = Requst_Message[Read_len - 2] << 8 | Requst_Message[Read_len - 1]; | |||
if (CRC_data != Message_CRC) | |||
return false; | |||
return true; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 异常码03判定--请求报文长度校验 | |||
* 描述 : 对请求报文的字节长度进行计算校验 | |||
* 输入 : *Requst_Message 请求报文 Read_len 接收到到的字节数 | |||
* 返回值 : true 长度校验通过 | |||
* false 长度校验不通过 | |||
**********************************************************************************************/ | |||
bool Check_Requst_Message_Len(UINT8 *Requst_Message, DWORD Read_len) | |||
{ | |||
if (Requst_Message[1] == 0x01 || Requst_Message[1] == 0x03) | |||
{ | |||
if (Read_len != 8) | |||
return false; | |||
} | |||
if (Requst_Message[1] == 0x0F || Requst_Message[1] == 0x10) | |||
{ | |||
unsigned int Number = Requst_Message[4] << 8 | Requst_Message[5]; | |||
unsigned int Count_len = Count_Read_date_number(Requst_Message[1], Number) + 9; | |||
if (Read_len != Count_len) | |||
return false; | |||
} | |||
return true; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 异常码03判定--请求报文中的操作数量判定 | |||
* 描述 : 对请求报文中的操作数量进行判定 | |||
* 输入 : *Requst_Message 请求报文 Read_len 接收到到的字节数 | |||
* 返回值 : true 校验通过 | |||
* false 校验不通过 | |||
**********************************************************************************************/ | |||
bool Check_Operation_Number_Requst_Message(UINT8 *Requst_Message) | |||
{ | |||
unsigned int Operation_Number = Requst_Message[4] << 8 | Requst_Message[5]; | |||
if (Requst_Message[1] == 0x01) | |||
{ | |||
if (Operation_Number == 0 || Operation_Number > 2000) | |||
return false; | |||
} | |||
if (Requst_Message[1] == 0x03) | |||
{ | |||
if (Operation_Number == 0 || Operation_Number > 125) | |||
return false; | |||
} | |||
if (Requst_Message[1] == 0x0F) | |||
{ | |||
if (Operation_Number < 1 || Operation_Number > 1968) | |||
return false; | |||
} | |||
if (Requst_Message[1] == 0x10) | |||
{ | |||
if (Operation_Number < 1 || Operation_Number > 123) | |||
return false; | |||
} | |||
return true; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 生成响应报文 | |||
* 描述 : 检查设备请求报文来生成对应功能的响应报文 | |||
* 输入 : *Requst_Message 请求报文 *Response_Message响应报文 | |||
* 返回值 : true 生成响应报文 | |||
* false 不生成响应报文 | |||
**********************************************************************************************/ | |||
bool Create_Response_Message(UINT8 *Requst_Message, UINT8 *Response_Message, DWORD Read_len) | |||
{ | |||
if (Requst_Message[0] != Device_ID) //检查设备ID一致 | |||
return false; | |||
if (!Check_Requst_Message_CRC(Requst_Message, Read_len))//CRC校验报文是否正确 | |||
return false; | |||
if (RTU_Enable == 0) | |||
{ | |||
Create_Abnormal_Code_Response_Message(Requst_Message, Response_Message, 0x04); | |||
return true; | |||
} | |||
if (!Check_Requst_Message_Len(Requst_Message, Read_len) || !Check_Operation_Number_Requst_Message(Requst_Message))//检查请求报文长度是否正确 | |||
{ | |||
Create_Abnormal_Code_Response_Message(Requst_Message, Response_Message, 0x03); | |||
return true; | |||
} | |||
switch (Requst_Message[1]) | |||
{ | |||
case 0x01:Create_0x01_Response_Message(Requst_Message, Response_Message); break; | |||
case 0x03:Create_0x03_Response_Message(Requst_Message, Response_Message); break; | |||
case 0x0F:Create_0x0F_Response_Message(Requst_Message, Response_Message); break; | |||
case 0x10:Create_0x10_Response_Message(Requst_Message, Response_Message); break; | |||
default: Create_Abnormal_Code_Response_Message(Requst_Message, Response_Message,0x01); | |||
} | |||
return true; | |||
} | |||
int Modbus_RTU_Salve(void) | |||
{ | |||
string COMM = Input_COMM(); | |||
unsigned int Baud_Rate = Input_Baud_Rate(); | |||
BYTE Date_Bits = Input_Date_Bits(); | |||
BYTE Stop_Bits = Input_Stop_Bits(Date_Bits); | |||
BYTE Parity = Input_Parity(); | |||
RTU_Enable = Input_RTU_Enable(); | |||
UINT8 Requst_Message[MAX_NUMBER]; | |||
UINT8 Response_Message[MAX_NUMBER]; | |||
HANDLE Handle_Com = Init_COM((LPCTSTR)COMM.c_str(), Baud_Rate, Date_Bits, Stop_Bits, Parity); | |||
if (Handle_Com == INVALID_HANDLE_VALUE) | |||
{ | |||
cout << "初始化串口失败" << endl; | |||
getchar(); | |||
return 0; | |||
} | |||
else | |||
printf("初始化串口成功"); | |||
Init_Coil_Register(); | |||
DWORD Read_len; | |||
while (true) | |||
{ | |||
PurgeComm(Handle_Com, PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT);//清除缓存 | |||
BOOL Read_flage = ReadFile(Handle_Com, Requst_Message, 256, &Read_len, NULL); //阻塞等待接收请求报文 | |||
if (Read_flage && (Read_len > 0)) | |||
{ | |||
if (Create_Response_Message(Requst_Message, Response_Message, Read_len)) | |||
{ | |||
while (!SendData(Handle_Com, (char*)Response_Message, Response_Message_Len)) | |||
{ | |||
printf("发送失败,重新发送"); | |||
} | |||
Log_Note(Requst_Message, 0, Read_len); | |||
Log_Note(Response_Message, 1, Response_Message_Len); | |||
} | |||
else | |||
continue; | |||
} | |||
} | |||
CloseHandle(Handle_Com); | |||
getchar(); | |||
return 0; | |||
} |
@@ -0,0 +1,11 @@ | |||
#ifndef __RTU_SALVE_H | |||
#define __RTU_SALVE_H | |||
#include "common.h" | |||
#define Device_ID 0x09 | |||
#define MAX_Address 9999 | |||
#define MAX_NUMBER 256 | |||
int Modbus_RTU_Salve(void); | |||
#endif |
@@ -0,0 +1,453 @@ | |||
#include "common.h" | |||
/******************************************************************************************* | |||
* 功能 : 打开串口 | |||
* port : 串口号, 如("COM1") | |||
* baud_rate: 波特率 | |||
* date_bits: 数据位(有效范围4~8) | |||
* stop_bit : 停止位 | |||
* parity : 奇偶校验。默认为无校验。NOPARITY 0; ODDPARITY 1;EVENPARITY 2 | |||
********************************************************************************************/ | |||
HANDLE Init_COM(LPCTSTR Port, int baud_rate, BYTE date_bits, BYTE stop_bit, BYTE parity) | |||
{ | |||
HANDLE Handle_Com = CreateFile(Port, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);//同步方式打开串口 | |||
if (INVALID_HANDLE_VALUE == Handle_Com) | |||
{ | |||
return INVALID_HANDLE_VALUE; | |||
} | |||
SetupComm(Handle_Com, 4096, 4096);//设置缓存 | |||
DCB dcb; | |||
if (!GetCommState(Handle_Com, &dcb)) | |||
{ | |||
cout << "获取串口配置失败" << endl; | |||
} | |||
dcb.BaudRate = baud_rate; //波特率 | |||
dcb.fBinary = TRUE; //二进制模式。必须为TRUE | |||
dcb.ByteSize = date_bits; //数据位。范围4-8 | |||
if (stop_bit == 0) | |||
dcb.StopBits = ONESTOPBIT; //停止位 | |||
if (stop_bit == 1) | |||
dcb.StopBits = ONE5STOPBITS; //停止位 | |||
if (stop_bit == 2) | |||
dcb.StopBits = TWOSTOPBITS; //停止位 | |||
if (parity == NOPARITY) | |||
{ | |||
dcb.fParity = FALSE; //奇偶校验关闭 | |||
dcb.Parity = parity; //校验模式 | |||
} | |||
else | |||
{ | |||
dcb.fParity = TRUE; //奇偶校验开启 | |||
dcb.Parity = parity; //校验模式 | |||
} | |||
cout << SetCommState(Handle_Com, &dcb) << endl; | |||
PurgeComm(Handle_Com, PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT);//清除缓存 | |||
//设置串口读写时间 | |||
COMMTIMEOUTS CommTimeOuts; | |||
GetCommTimeouts(Handle_Com, &CommTimeOuts); | |||
CommTimeOuts.ReadIntervalTimeout = 5; | |||
CommTimeOuts.ReadTotalTimeoutMultiplier = 0; | |||
CommTimeOuts.ReadTotalTimeoutConstant = 0; | |||
CommTimeOuts.WriteTotalTimeoutMultiplier = 10; | |||
CommTimeOuts.WriteTotalTimeoutConstant = 1000; | |||
if (!SetCommTimeouts(Handle_Com, &CommTimeOuts)) { | |||
return INVALID_HANDLE_VALUE; | |||
} | |||
return Handle_Com; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 发送响应数据 | |||
* 描述 : 向串口写入数据 | |||
* 返回值 : true 发送成功 false 发送失败 | |||
* m_hComm : 串口句柄 | |||
* data : 要写入的数据 | |||
* len : 写入数据的长度 | |||
**********************************************************************************************/ | |||
bool SendData(HANDLE m_hComm, char* data, int len) | |||
{ | |||
if (m_hComm == INVALID_HANDLE_VALUE) | |||
return FALSE; | |||
//清空串口 | |||
PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR); | |||
//写串口 | |||
DWORD dwWrite = 0; | |||
DWORD dwRet = WriteFile(m_hComm, data, len, &dwWrite, NULL); | |||
//清空串口 | |||
PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR); | |||
if (!dwRet) | |||
return FALSE; | |||
return TRUE; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : Ms延时器 | |||
* 描述 : ms级准确延时 | |||
* 输入 : lTime 延时的ms数 | |||
* 返回值 : 无 | |||
**********************************************************************************************/ | |||
void MSleep(long lTime) | |||
{ | |||
LARGE_INTEGER litmp; | |||
LONGLONG QPart1, QPart2; | |||
double dfMinus, dfFreq, dfTim, dfSpec; | |||
QueryPerformanceFrequency(&litmp); | |||
dfFreq = (double)litmp.QuadPart; | |||
QueryPerformanceCounter(&litmp); | |||
QPart1 = litmp.QuadPart; | |||
dfSpec = 0.000001*lTime; | |||
do | |||
{ | |||
QueryPerformanceCounter(&litmp); | |||
QPart2 = litmp.QuadPart; | |||
dfMinus = (double)(QPart2 - QPart1); | |||
dfTim = dfMinus / dfFreq; | |||
} while (dfTim<dfSpec); | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 检查端口名称 | |||
* 描述 : 判断端口名称是否正确 | |||
* 输入 : Comm 端口名 | |||
* 返回值 : true 端口名正确 | |||
* false 端口名错误 | |||
**********************************************************************************************/ | |||
bool Check_Input_COMM(string Comm) | |||
{ | |||
string compare1 = "com"; | |||
string compare2 = "COM"; | |||
for (unsigned int i = 0; i < 3; i++) | |||
{ | |||
if (Comm[i] != compare1[i] && Comm[i] != compare2[i]) | |||
return false; | |||
} | |||
if (Comm[3] == 0x30) | |||
return false; | |||
for (unsigned int i = 4; i < Comm.length(); i++) | |||
{ | |||
if (Comm[i] >= 0x30 && Comm[i] <= 0x39) | |||
continue; | |||
} | |||
return true; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 输入端口名称 | |||
* 描述 : 获取串口通信的端口名称 | |||
* 输入 : 无 | |||
* 返回值 : COMM 端口名 | |||
**********************************************************************************************/ | |||
string Input_COMM(void) | |||
{ | |||
int flage = 1; | |||
string Comm; | |||
do | |||
{ | |||
if (flage == 1) | |||
{ | |||
printf("请输入端口名称:"); | |||
flage = 0; | |||
} | |||
else | |||
printf("请重新输入端口名称:"); | |||
cin >> Comm; | |||
cin.clear(); | |||
cin.sync(); | |||
} while (!Check_Input_COMM(Comm)); | |||
return Comm; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 检查波特率 | |||
* 描述 : 检查串口通信的波特率 | |||
* 输入 : Baud_Rate 波特率 | |||
* 返回值 : true 波特率正确 | |||
* false 波特率错误 | |||
**********************************************************************************************/ | |||
bool Check_Input_Baud_Rate(unsigned int Baud_Rate) | |||
{ | |||
unsigned int Compare_Baud_Rate[9] = {300,600,1200,2400,4800,9600,14400,19200,38400}; | |||
for (int i = 0; i < 9; i++) | |||
{ | |||
if (Baud_Rate == Compare_Baud_Rate[i]) | |||
return true; | |||
} | |||
return false; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 输入波特率 | |||
* 描述 : 获取串口通信的波特率 | |||
* 输入 : 无 | |||
* 返回值 : Baud_Rate 波特率 | |||
**********************************************************************************************/ | |||
unsigned int Input_Baud_Rate(void) | |||
{ | |||
unsigned int Baud_Rate; | |||
int flage = 1; | |||
printf("支持的波特率有:300,600,1200,2400,4800,9600,14400,19200,38400 \n"); | |||
do | |||
{ | |||
if (flage == 1) | |||
{ | |||
printf("请输入串口波特率:"); | |||
flage = 0; | |||
} | |||
else | |||
printf("请重新输入串口波特率:"); | |||
cin >> Baud_Rate; | |||
cin.clear(); | |||
cin.sync(); | |||
} while (!Check_Input_Baud_Rate(Baud_Rate)); | |||
return Baud_Rate; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 输入数据位 | |||
* 描述 : 获取串口通信的数据位个数 | |||
* 输入 : 无 | |||
* 返回值 : Date_Bits 数据位 | |||
**********************************************************************************************/ | |||
BYTE Input_Date_Bits(void) | |||
{ | |||
BYTE Date_Bits; | |||
int flage = 1; | |||
unsigned int data_bits = 0; | |||
printf("支持的数据位有:5,6,7,8 \n"); | |||
do | |||
{ | |||
if (flage == 1) | |||
{ | |||
printf("请输入有效数据位:"); | |||
flage = 0; | |||
} | |||
else | |||
printf("请重新输入有效数据位:"); | |||
cin >> data_bits; | |||
cin.clear(); | |||
cin.sync(); | |||
} while (!(data_bits == 5 || data_bits == 6 || data_bits == 7 || data_bits == 8)); | |||
Date_Bits = (BYTE)data_bits; | |||
return Date_Bits; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 检查停止位 | |||
* 描述 : 检测停止位是否和数据位相符 | |||
* 输入 : Date_Bits 数据位 | |||
* Stop_Bits 停止位 | |||
* 返回值 : true 停止位正确 | |||
* false 停止位错误 | |||
**********************************************************************************************/ | |||
bool Check_Input_Stop_Bits(BYTE Date_Bits, BYTE Stop_Bits) | |||
{ | |||
if (Stop_Bits == 0 || Stop_Bits == 1 || Stop_Bits == 2) | |||
{ | |||
if (Stop_Bits == 1 && (Date_Bits == 6 || Date_Bits == 7 || Date_Bits == 8)) | |||
return false; | |||
if (Stop_Bits == 2 && Date_Bits == 5) | |||
return false; | |||
} | |||
else | |||
return false; | |||
return true; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 输入停止位 | |||
* 描述 : 获取串口通信的停止位 | |||
* 输入 : Date_Bits 数据位 | |||
* Stop_Bits= 0,1,2对应的是1bit,1.5bits,2bits. | |||
* Date_Bits=6,7,8时 Stop_Bits不能为1 | |||
* Date_Bits=5时 Stop_Bits不能为2 | |||
* 返回值 : Stop_Bits 数据位 | |||
**********************************************************************************************/ | |||
BYTE Input_Stop_Bits(BYTE Date_Bits) | |||
{ | |||
BYTE Stop_Bits; | |||
int flage = 1; | |||
unsigned int stop_bits = 0; | |||
printf("支持的停止位有:0,1,2 \n"); | |||
do | |||
{ | |||
if (flage == 1) | |||
{ | |||
printf("请输入停止位:"); | |||
flage = 0; | |||
} | |||
else | |||
printf("请重新输入停止位:"); | |||
cin >> stop_bits; | |||
cin.clear(); | |||
cin.sync(); | |||
} while (!Check_Input_Stop_Bits(Date_Bits, (BYTE)stop_bits)); | |||
Stop_Bits = (BYTE)stop_bits; | |||
return Stop_Bits; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 选择校验位 | |||
* 描述 : 获取串口通信的校验位 | |||
* 输入 : 无 | |||
* 返回值 : Parity 校验位 | |||
**********************************************************************************************/ | |||
BYTE Input_Parity(void) | |||
{ | |||
BYTE Parity; | |||
int flage = 1; | |||
unsigned int parity = 0; | |||
printf("支持的校验位有:0- 无校验 1- 奇校验 2- 偶校验 \n"); | |||
do | |||
{ | |||
if (flage == 1) | |||
{ | |||
printf("请输入校验位:"); | |||
flage = 0; | |||
} | |||
else | |||
printf("请重新输入校验位:"); | |||
cin >> parity; | |||
cin.clear(); | |||
cin.sync(); | |||
} while (!(parity == 1 || parity == 0 || parity == 2)); | |||
Parity = (BYTE)parity; | |||
return Parity; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 选择校验位 | |||
* 描述 : 获取串口通信的校验位 | |||
* 输入 : 无 | |||
* 返回值 : Parity 校验位 | |||
**********************************************************************************************/ | |||
unsigned int Input_RTU_Enable(void) | |||
{ | |||
unsigned int RTU_Enable; | |||
int flage = 1; | |||
printf("请选择设备是否故障 0: 故障 1:未故障\n"); | |||
do | |||
{ | |||
if (flage == 1) | |||
{ | |||
printf("请输入故障码:"); | |||
flage = 0; | |||
} | |||
else | |||
printf("请重新输入故障码:"); | |||
cin >> RTU_Enable; | |||
cin.clear(); | |||
cin.sync(); | |||
} while (!(RTU_Enable == 1 || RTU_Enable == 0)); | |||
return RTU_Enable; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 计算写入数据的字节数 | |||
* 描述 : 通过对应的功能码和操作数量计算对应的数据字节数 | |||
* 输入 : Function_code 选择的功能码类型 Operations_Number对应功能码类型的操作数量 | |||
* 输出 : Read_date_number 写入数据的字节数 | |||
*********************************************************************************************/ | |||
unsigned int Count_Read_date_number(int Function_code, unsigned int Operations_Number) | |||
{ | |||
unsigned int Read_date_number = 0; | |||
if (Function_code == 0x01 || Function_code == 0x0F) //读线圈 | |||
{ | |||
Read_date_number = Operations_Number / 8; | |||
if (Operations_Number % 8) | |||
Read_date_number++; | |||
} | |||
if (Function_code == 0x03 || Function_code == 0x10)//读寄存器 | |||
Read_date_number = Operations_Number * 2; | |||
return Read_date_number; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 计算CRC校验 | |||
* 描述 : 获取Modbus—CRC-16的校验数据 | |||
* 输入 : *Data 计算校验数据 CRC_Len 数据长度 | |||
* 返回值 : Ret_CRC_date CRC校验结果 | |||
**********************************************************************************************/ | |||
UINT16 CRC_16(UINT8 *Data, unsigned int CRC_Len) | |||
{ | |||
UINT16 CRC_date = 0XFFFF;//16位crc寄存器预置 | |||
UINT16 temp; | |||
unsigned int i = 0, j = 0; | |||
for (i = 0; i < CRC_Len; i++) | |||
{ | |||
temp = *Data & 0X00FF;//将八位数据与CRC寄存器亦或 | |||
Data++; | |||
CRC_date ^= temp; | |||
for (j = 0; j < 8; j++) | |||
{ | |||
if (CRC_date & 0X0001)//判断右移出的是不是1,如果是1则与多项式进行异或。 | |||
{ | |||
CRC_date >>= 1; | |||
CRC_date ^= 0XA001; | |||
} | |||
else | |||
{ | |||
CRC_date >>= 1; | |||
} | |||
} | |||
} | |||
UINT16 Ret_CRC_date = CRC_date >> 8; | |||
Ret_CRC_date = Ret_CRC_date | CRC_date << 8; | |||
return Ret_CRC_date; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 日志记录 | |||
* 描述 : 记录每次通信的请求和响应报文 | |||
* 输入 : *Message 消息帧存放的数组 flage = 1 请求报文 flage = 其他 响应报文 | |||
* Message_len 报文数据长度 | |||
* 输出 : 无 | |||
*********************************************************************************************/ | |||
void Log_Note(UINT8 *Message, int flage, int Message_len) | |||
{ | |||
FILE *fp = NULL; | |||
time_t timep; | |||
struct tm *p; | |||
time(&timep); | |||
p = gmtime(&timep); | |||
string Recv_str = to_string(1900 + p->tm_year) + "-" + to_string(1 + p->tm_mon) + "-" + to_string(p->tm_mday) | |||
+ " " + to_string(8 + p->tm_hour) + ":" + to_string(p->tm_min) + ":" + to_string(p->tm_sec) + " Recv:"; | |||
string Send_str = to_string(1900 + p->tm_year) + "-" + to_string(1 + p->tm_mon) + "-" + to_string(p->tm_mday) | |||
+ " " + to_string(8 + p->tm_hour) + ":" + to_string(p->tm_min) + ":" + to_string(p->tm_sec) + " Send:"; | |||
/* 打开文件用于读写 */ | |||
if ((fp = fopen("Modbus_TCP_log.txt", "a+")) == NULL) | |||
{ | |||
printf("打开文件失败"); | |||
return; | |||
} | |||
if (flage == 1) | |||
{ | |||
fwrite(Send_str.c_str(), Send_str.length(), 1, fp); | |||
for (int i = 0; i < Message_len; i++) | |||
{ | |||
fprintf(fp, "%02X ", Message[i]); | |||
} | |||
fprintf(fp, "\n"); | |||
} | |||
else | |||
{ | |||
fwrite(Recv_str.c_str(), Recv_str.length(), 1, fp); | |||
for (int i = 0; i < Message_len; i++) | |||
{ | |||
fprintf(fp, "%02X ", Message[i]); | |||
} | |||
fprintf(fp, "\n"); | |||
} | |||
fclose(fp); | |||
fp = NULL; | |||
} | |||
@@ -0,0 +1,22 @@ | |||
#ifndef __COMMON_H | |||
#define __COMMON_H | |||
#include <Windows.h> | |||
#include <string> | |||
#include <iostream> | |||
#include <time.h> | |||
using namespace std; | |||
unsigned int Count_Read_date_number(int Function_code, unsigned int Operations_Number); | |||
HANDLE Init_COM(LPCTSTR Port, int baud_rate, BYTE date_bits, BYTE stop_bit, BYTE parity); | |||
bool SendData(HANDLE m_hComm, char* data, int len); | |||
string Input_COMM(void); | |||
unsigned int Input_Baud_Rate(void); | |||
BYTE Input_Date_Bits(void); | |||
BYTE Input_Stop_Bits(BYTE Date_Bits); | |||
BYTE Input_Parity(void); | |||
unsigned int Input_RTU_Enable(void); | |||
UINT16 CRC_16(UINT8 *Data, unsigned int CRC_Len); | |||
void Log_Note(UINT8 *Message, int flage, int Message_len); | |||
#endif |
@@ -0,0 +1,8 @@ | |||
#include "main.h" | |||
int main() | |||
{ | |||
Modbus_RTU_Salve(); | |||
getchar(); | |||
return 0; | |||
} |
@@ -0,0 +1,6 @@ | |||
#ifndef __MAIN_H | |||
#define __MAIN_H | |||
#include "RTU_Salve.h" | |||
#endif |
@@ -0,0 +1,81 @@ | |||
<?xml version="1.0" encoding="utf-8"?> | |||
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |||
<ItemGroup Label="ProjectConfigurations"> | |||
<ProjectConfiguration Include="Debug|Win32"> | |||
<Configuration>Debug</Configuration> | |||
<Platform>Win32</Platform> | |||
</ProjectConfiguration> | |||
<ProjectConfiguration Include="Release|Win32"> | |||
<Configuration>Release</Configuration> | |||
<Platform>Win32</Platform> | |||
</ProjectConfiguration> | |||
</ItemGroup> | |||
<PropertyGroup Label="Globals"> | |||
<ProjectGuid>{CC08BE54-3DFF-41F2-9F8B-17E0FD5E3757}</ProjectGuid> | |||
<RootNamespace>Modbus_TCP</RootNamespace> | |||
</PropertyGroup> | |||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | |||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> | |||
<ConfigurationType>Application</ConfigurationType> | |||
<UseDebugLibraries>true</UseDebugLibraries> | |||
<PlatformToolset>v120</PlatformToolset> | |||
<CharacterSet>MultiByte</CharacterSet> | |||
</PropertyGroup> | |||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | |||
<ConfigurationType>Application</ConfigurationType> | |||
<UseDebugLibraries>false</UseDebugLibraries> | |||
<PlatformToolset>v120</PlatformToolset> | |||
<WholeProgramOptimization>true</WholeProgramOptimization> | |||
<CharacterSet>MultiByte</CharacterSet> | |||
</PropertyGroup> | |||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> | |||
<ImportGroup Label="ExtensionSettings"> | |||
</ImportGroup> | |||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | |||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | |||
</ImportGroup> | |||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | |||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | |||
</ImportGroup> | |||
<PropertyGroup Label="UserMacros" /> | |||
<PropertyGroup /> | |||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | |||
<ClCompile> | |||
<WarningLevel>Level3</WarningLevel> | |||
<Optimization>Disabled</Optimization> | |||
<SDLCheck>false</SDLCheck> | |||
<BufferSecurityCheck>false</BufferSecurityCheck> | |||
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions> | |||
</ClCompile> | |||
<Link> | |||
<GenerateDebugInformation>true</GenerateDebugInformation> | |||
</Link> | |||
</ItemDefinitionGroup> | |||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | |||
<ClCompile> | |||
<WarningLevel>Level3</WarningLevel> | |||
<Optimization>MaxSpeed</Optimization> | |||
<FunctionLevelLinking>true</FunctionLevelLinking> | |||
<IntrinsicFunctions>true</IntrinsicFunctions> | |||
<SDLCheck>true</SDLCheck> | |||
</ClCompile> | |||
<Link> | |||
<GenerateDebugInformation>true</GenerateDebugInformation> | |||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | |||
<OptimizeReferences>true</OptimizeReferences> | |||
</Link> | |||
</ItemDefinitionGroup> | |||
<ItemGroup> | |||
<ClCompile Include="common.cpp" /> | |||
<ClCompile Include="main.cpp" /> | |||
<ClCompile Include="TCP_client.cpp" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<ClInclude Include="common.h" /> | |||
<ClInclude Include="main.h" /> | |||
<ClInclude Include="TCP_client.h" /> | |||
</ItemGroup> | |||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | |||
<ImportGroup Label="ExtensionTargets"> | |||
</ImportGroup> | |||
</Project> |
@@ -0,0 +1,39 @@ | |||
<?xml version="1.0" encoding="utf-8"?> | |||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |||
<ItemGroup> | |||
<Filter Include="源文件"> | |||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> | |||
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions> | |||
</Filter> | |||
<Filter Include="头文件"> | |||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> | |||
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions> | |||
</Filter> | |||
<Filter Include="资源文件"> | |||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> | |||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> | |||
</Filter> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<ClCompile Include="main.cpp"> | |||
<Filter>源文件</Filter> | |||
</ClCompile> | |||
<ClCompile Include="common.cpp"> | |||
<Filter>源文件</Filter> | |||
</ClCompile> | |||
<ClCompile Include="TCP_client.cpp"> | |||
<Filter>源文件</Filter> | |||
</ClCompile> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<ClInclude Include="common.h"> | |||
<Filter>头文件</Filter> | |||
</ClInclude> | |||
<ClInclude Include="TCP_client.h"> | |||
<Filter>头文件</Filter> | |||
</ClInclude> | |||
<ClInclude Include="main.h"> | |||
<Filter>头文件</Filter> | |||
</ClInclude> | |||
</ItemGroup> | |||
</Project> |
@@ -0,0 +1,54 @@ | |||
2020-9-5 19:23:6 Send:00 00 00 00 00 08 01 10 00 00 00 01 02 FF FF | |||
2020-9-5 19:23:30 Recv:00 00 00 00 00 06 01 10 00 00 00 01 | |||
2020-9-5 19:24:29 Send:00 00 00 00 00 06 01 03 00 00 00 01 | |||
2020-9-5 19:24:50 Send:00 00 00 00 00 06 01 03 00 00 00 01 | |||
2020-9-5 19:26:22 Send:00 00 00 00 00 06 01 03 00 00 00 01 | |||
2020-9-5 19:26:31 Recv:00 00 00 00 00 05 01 03 02 FF FF | |||
2020-9-5 19:27:11 Send:00 00 00 00 00 06 01 03 00 00 00 01 | |||
2020-9-5 19:27:17 Recv:00 00 00 00 00 05 01 03 02 FF FF | |||
2020-9-5 19:27:38 Send:00 00 00 00 00 08 01 10 00 01 00 01 02 11 11 | |||
2020-9-5 19:27:44 Recv:00 00 00 00 00 06 01 10 00 01 00 01 | |||
2020-9-5 19:28:20 Send:00 00 00 00 00 06 01 03 00 01 00 01 | |||
2020-9-5 19:28:55 Send:00 00 00 00 00 06 01 03 00 01 00 01 | |||
2020-9-5 19:30:38 Send:00 00 00 00 00 06 01 03 00 00 00 01 | |||
2020-9-5 19:30:38 Recv:00 00 00 00 00 05 01 03 02 FF FF | |||
2020-9-5 19:30:42 Send:00 00 00 00 00 06 01 03 00 01 00 01 | |||
2020-9-5 19:30:42 Recv:00 00 00 00 00 05 01 03 02 11 FF | |||
2020-9-5 19:31:1 Send:00 00 00 00 00 08 01 10 00 03 00 01 02 11 11 | |||
2020-9-5 19:31:1 Recv:00 00 00 00 00 06 01 10 00 03 00 01 | |||
2020-9-5 19:31:8 Send:00 00 00 00 00 06 01 03 00 03 00 01 | |||
2020-9-5 19:31:49 Send:00 00 00 00 00 06 01 03 00 03 00 01 | |||
2020-9-5 19:32:50 Send:00 00 00 00 00 06 01 03 00 00 00 01 | |||
2020-9-5 19:33:17 Send:00 00 00 00 00 08 01 10 00 02 00 01 02 11 11 | |||
2020-9-5 19:38:12 Send:00 00 00 00 00 06 01 03 00 00 00 01 | |||
2020-9-5 19:38:12 Recv:68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E | |||
2020-9-5 19:38:24 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-5 19:38:24 Recv:68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E 68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E 68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E 68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E | |||
2020-9-5 19:38:37 Send:00 00 00 00 00 06 01 03 00 00 00 01 | |||
2020-9-5 19:38:56 Send:00 00 00 00 00 06 01 03 00 00 00 01 | |||
2020-9-5 19:38:56 Recv:68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E 68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E 68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E 68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E | |||
2020-9-5 19:39:5 Send:00 00 00 00 00 06 01 03 00 00 00 01 | |||
2020-9-5 19:39:5 Recv:68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E | |||
2020-9-5 19:40:28 Send:00 00 00 00 00 06 01 03 00 00 00 0A | |||
2020-9-5 19:40:28 Recv:68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E | |||
2020-9-5 19:40:41 Send:00 00 00 00 00 06 01 03 00 00 00 0A | |||
2020-9-5 19:40:52 Send:00 00 00 00 00 06 01 03 00 00 00 0A | |||
2020-9-5 19:40:52 Recv:68 74 74 70 3A 2F 2F 77 77 77 2E 63 6D 73 6F 66 74 2E 63 6E | |||
2020-9-7 18:40:38 Send:00 00 00 00 00 06 01 01 00 00 00 01 | |||
2020-9-7 18:40:38 Recv:00 00 00 00 00 04 01 01 01 00 | |||
2020-9-7 18:40:51 Send:00 00 00 00 00 06 01 03 00 00 00 64 | |||
2020-9-7 18:40:51 Recv:00 00 00 00 00 CB 01 03 C8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | |||
2020-9-7 18:41:20 Send:00 00 00 00 00 06 01 03 00 63 00 01 | |||
2020-9-7 18:41:20 Recv:00 00 00 00 00 05 01 03 02 00 00 | |||
2020-9-7 18:41:34 Send:00 00 00 00 00 06 01 03 00 63 00 03 | |||
2020-9-7 18:41:34 Recv:00 00 00 00 00 09 01 03 06 00 00 00 00 00 00 | |||
2020-9-7 18:41:55 Send:00 00 00 00 00 06 01 03 00 63 00 03 | |||
2020-9-7 18:41:55 Recv:00 00 00 00 00 09 01 03 06 00 00 00 00 00 00 | |||
2020-9-7 18:43:7 Send:00 00 00 00 00 06 01 03 00 00 00 01 | |||
2020-9-7 18:44:25 Send:00 00 00 00 00 07 01 0F 00 00 00 01 01 01 | |||
2020-9-7 18:44:25 Recv:00 00 00 00 00 06 01 0F 00 00 00 01 | |||
2020-9-7 18:45:27 Send:00 00 00 00 00 07 01 0F 00 00 00 01 01 01 | |||
2020-9-7 18:46:53 Send:00 00 00 00 00 07 01 0F 00 00 00 01 01 01 | |||
2020-9-7 18:47:2 Recv:00 00 00 00 00 06 01 0F 00 00 00 01 | |||
2020-9-7 18:47:16 Send:00 00 00 00 00 07 01 0F 00 00 00 01 01 01 | |||
2020-9-7 18:47:30 Recv:00 00 00 00 00 06 01 0F 00 00 00 01 |
@@ -0,0 +1,261 @@ | |||
#include "TCP_client.h" | |||
/********************************************************************************************* | |||
* 功能 : 检测socket版本 | |||
* 描述 : 检测socket版本是否一致 | |||
* 输入 : 无 | |||
* 输出 : true socket版本一致 | |||
* false socket版本不一致 | |||
*********************************************************************************************/ | |||
bool InitSocket_Version(void) | |||
{ | |||
WORD sockVersion = MAKEWORD(2, 2);//使用winsocket2.2版本 | |||
WSADATA wsaData; | |||
if (WSAStartup(sockVersion, &wsaData) != 0) | |||
{ | |||
return false; | |||
} | |||
return true; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : IP地址有效性检测 | |||
* 描述 : 检测输入的IP地址是否合法 | |||
* 输入 : IP 输入的IP地址 | |||
* 输出 : true IP地址合法 | |||
* false IP地址非法 | |||
*********************************************************************************************/ | |||
bool Check_IP(string IP) | |||
{ | |||
int s[4]; | |||
string ip = IP; | |||
if (ip.length() < 7 || ip.length() > 15) //长度判定 | |||
return false; | |||
if (sscanf_s(IP.c_str(), "%d.%d.%d.%d", &s[0], &s[1], &s[2], &s[3]) != 4) //IPV4格式正确 | |||
{ | |||
return false; | |||
} | |||
string newip = to_string(s[0]) + "." + to_string(s[1]) + "." + to_string(s[2]) + "." + to_string(s[3]); | |||
if (ip != newip) //前导0 | |||
return false; | |||
if ((s[0] & 0xffffff00) || (s[1] & 0xffffff00) || (s[2] & 0xffffff00) || (s[3] & 0xffffff00)) //判断每一段大小是否符合要求 | |||
{ | |||
return false; | |||
} | |||
return true; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 获取从站IP地址和端口号 | |||
* 描述 : 终端输入从站IP地址和端口号 | |||
* 输入 : IP 地址 (IPV4) | |||
* *Port_number 端口号(1-65535) | |||
* 输出 : 无 | |||
*********************************************************************************************/ | |||
void Input_IP(string& IP, unsigned int *Port_number) | |||
{ | |||
int i = 1; | |||
do | |||
{ | |||
if (i == 1) | |||
{ | |||
cout << "请输入从站IP:"; | |||
i = 0; | |||
} | |||
else | |||
cout << "IP地址格式不正确,请重新输入从站IP:"; | |||
cin >> IP; | |||
} while (!Check_IP(IP)); | |||
i = 1; | |||
cout << "IP地址输格式入正确,请输入从站端口号:"; | |||
do | |||
{ | |||
if (i != 1) | |||
cout << "请重新输入从站端口号:"; | |||
cin >> *Port_number; | |||
i = 0; | |||
} while (*Port_number == 0 || *Port_number > 65535); //端口不能为0 ,端口号范围0---65535 | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 初始化客户端 | |||
* 描述 : 根据终端输入从站IP地址和端口号连接对应的服务器,设置超时时间 | |||
* 输入 : IP 地址 (IPV4) | |||
* Port_number 端口号(1-65535) | |||
* 输出 : ClientSocket 连接成功的套接字 | |||
*********************************************************************************************/ | |||
SOCKET Init_client(string IP, unsigned int Port_number) | |||
{ | |||
if (InitSocket_Version() == 0) | |||
return INVALID_SOCKET; | |||
SOCKET ClientSocket = socket(AF_INET, SOCK_STREAM, 0); | |||
sockaddr_in Serversock_in; | |||
Serversock_in.sin_addr.S_un.S_addr = inet_addr(IP.c_str()); | |||
Serversock_in.sin_family = AF_INET; | |||
Serversock_in.sin_port = htons(Port_number); | |||
while (SOCKET_ERROR == connect(ClientSocket, (SOCKADDR*)&Serversock_in, sizeof(SOCKADDR))) | |||
{ | |||
cout << "尝试连接TCP从站失败" << endl; | |||
} | |||
cout << "连接TCP从站成功" << endl; | |||
TIMEVAL timeout; | |||
timeout.tv_sec = 200; //ms | |||
timeout.tv_usec = 0; //us | |||
setsockopt(ClientSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));//设置接收超时时间 | |||
return ClientSocket; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 清除接收缓冲区中的数据 | |||
* 描述 : 每一次发送请求前清空缓存区数据 | |||
* 输入 : ClientSocket 客户端套接字 | |||
* 输出 : 无 | |||
*********************************************************************************************/ | |||
void Clear_recv_buf(SOCKET clientSocket) | |||
{ | |||
struct timeval tmOut; | |||
tmOut.tv_sec = 0; | |||
tmOut.tv_usec = 0; | |||
fd_set fds; | |||
int nRet; | |||
char tmp[2]; | |||
memset(tmp, 0, sizeof(tmp)); | |||
while (1) | |||
{ | |||
FD_ZERO(&fds); | |||
FD_SET(clientSocket, &fds); | |||
nRet = select(FD_SETSIZE, &fds, NULL, NULL, &tmOut); | |||
if (nRet == 0) | |||
break; | |||
recv(clientSocket, tmp, 1, 0); | |||
} | |||
} | |||
void Printf_Coil_date(UINT8 *Response_Message, UINT8 *Request_Message) | |||
{ | |||
printf("从站设备ID %02X 功能码为 %02X\n", Response_Message[6], Response_Message[7]); | |||
unsigned int temp1 = ((Request_Message[8] << 8) | Request_Message[9])+1; | |||
unsigned int Number = ((Request_Message[10] << 8) | Request_Message[11]); | |||
printf("线圈起始地址为%d \n",temp1); | |||
unsigned int temp = temp1; | |||
for (int i = 0; i < Response_Message[8]; i++) | |||
{ | |||
unsigned int temp2 = temp + 7; | |||
if (temp2 > temp1 + Number - 1) | |||
temp2 = temp1 + Number - 1; | |||
printf("线圈第%d --- %d的状态为:%02X \n", temp2, temp, Response_Message[9+i]); | |||
temp = temp + 8; | |||
} | |||
} | |||
void Printf_Register_date(UINT8 *Response_Message, UINT8 *Request_Message) | |||
{ | |||
printf("从站设备ID %02X 功能码为 %02X\n", Response_Message[6], Response_Message[7]); | |||
unsigned int temp1 = ((Request_Message[8] << 8) | Request_Message[9]) + 1; | |||
unsigned int Number = ((Request_Message[10] << 8) | Request_Message[11]); | |||
printf("寄存器起始地址为%d \n", temp1); | |||
for (int i = 0; i < Response_Message[8]; i = i + 2) | |||
{ | |||
printf("寄存器第%d的值为:%02X %02X \n", temp1++, Response_Message[9 + i], Response_Message[10+i]); | |||
} | |||
} | |||
void Printf_Anomaly_date(UINT8 *Response_Message) | |||
{ | |||
UINT8 a = Response_Message[8]; | |||
printf("从站设备ID %02X 功能码为 %02X\n", Response_Message[6], Response_Message[7]); | |||
switch (a) | |||
{ | |||
case 0x01: printf("%02X : 从站设备不支持此功能码",a); break; | |||
case 0x02: printf("%02X : 指定的数据地址在从站设备中不存在",a); break; | |||
case 0x03: printf("%02X : 指定的数据超过范围或者不允许使用",a); break; | |||
case 0x04: printf("%02X : 从站设备处理响应的过程中,出现未知错误等",a); break; | |||
default: printf("Unkown Other Error!!!!!"); | |||
} | |||
} | |||
bool Analysis_Response_Message(UINT8 *Response_Message, UINT8 *Request_Message, int Response_Message_len) | |||
{ | |||
if (Response_Message[7] == Request_Message[7] + 0x80 && Response_Message_len == 9)//先处理异常响应 | |||
{ | |||
Printf_Anomaly_date(Response_Message); | |||
return true; | |||
} | |||
else if (Response_Message[6] == Request_Message[6] && Response_Message_len - 5 == Response_Message[5]) //判断是否是正常响应帧 | |||
{ | |||
if (Response_Message[7] == 0x01) | |||
Printf_Coil_date(Response_Message, Request_Message); | |||
if (Response_Message[7] == 0x03) | |||
Printf_Register_date(Response_Message, Request_Message); | |||
if (Response_Message[7] == 0x10) | |||
printf("成功写入从站线圈%d个", ((Request_Message[10] << 8) | Request_Message[11])); | |||
if (Response_Message[7] == 0x0F) | |||
printf("成功写入从站寄存器%d个" ,((Request_Message[10] << 8) | Request_Message[11])); | |||
return true; | |||
} | |||
return false; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 运行客户端 | |||
* 描述 : 根据终端输入从站IP地址和端口号连接对应的服务器并且生成发送请求等待响应 | |||
* 输入 : IP 地址 (IPV4) | |||
* Port_number 端口号(1-65535) | |||
* 输出 : false 执行出错 true 执行成功 | |||
*********************************************************************************************/ | |||
bool Tcp_client(string IP, unsigned int Port_number) | |||
{ | |||
SOCKET ClientSocket = Init_client(IP, Port_number); | |||
if (INVALID_SOCKET == ClientSocket) | |||
{ | |||
cout << "初始化主站失败" << endl; | |||
return false; | |||
} | |||
UINT8 Request_Message[260]; | |||
UINT8 Response_Message[260]; | |||
while (true) | |||
{ | |||
int Function_code = Input_Function_code(); | |||
unsigned int Operations_Number = Input_Operations_Number(Function_code); | |||
unsigned int Starting_address = Input_Starting_address(); | |||
string Write_date = Input_Write_date(Function_code, Operations_Number); | |||
int Request_Message_len = Crate_TCP_Message(Request_Message, Function_code, Operations_Number, Starting_address, Write_date); | |||
printf("主站请求 :"); | |||
for (int i = 0; i < Request_Message_len; i++) | |||
{ | |||
printf("%02x ", Request_Message[i]); | |||
} | |||
printf("\n"); | |||
Log_Note(Request_Message, 1, Request_Message_len); | |||
Clear_recv_buf(ClientSocket); | |||
send(ClientSocket, (char*)Request_Message, Request_Message_len, 0); | |||
memset(Response_Message, 0, 260); | |||
int Response_Message_len = recv(ClientSocket, (char*)Response_Message, 260, 0); | |||
if (Response_Message_len > 0) | |||
{ | |||
Log_Note(Response_Message, 0, Response_Message_len); | |||
printf("从站响应 :"); | |||
for (int i = 0; i < Response_Message_len; i++) | |||
{ | |||
printf("%02x ", Response_Message[i]); | |||
} | |||
printf("\n"); | |||
if (Analysis_Response_Message(Response_Message, Request_Message, Response_Message_len)) | |||
printf("响应报文异常\n\n"); | |||
} | |||
else | |||
cout << "响应超时" << endl; | |||
} | |||
//关闭套接字 | |||
closesocket(ClientSocket); | |||
//关闭服务 | |||
WSACleanup(); | |||
return true; | |||
} |
@@ -0,0 +1,14 @@ | |||
#ifndef __TCP_CLIENT_H | |||
#define __TCP_CLIENT_H | |||
#include "common.h" | |||
bool InitSocket_Version(void); | |||
bool Check_IP(char* IP); | |||
void Input_IP(string& IP, unsigned int *Port_number); | |||
bool Tcp_client(string IP, unsigned int Port_number); | |||
#endif |
@@ -0,0 +1,311 @@ | |||
#include "common.h" | |||
/********************************************************************************************* | |||
* 功能 : 选择功能码 | |||
* 描述 : 在功能码 0x01 0x03 0x0F 0x10中选择一个功能码 | |||
* 输入 : 无 | |||
* 输出 : Function_code 选择的功能码 | |||
*********************************************************************************************/ | |||
int Input_Function_code(void) | |||
{ | |||
int Function_code; | |||
cin >> hex; | |||
int i = 1; | |||
printf("功能码 : 0x01 0x03 0x0F 0x10 \n"); | |||
printf("说明 : 读线圈 读寄存器 写多个线圈 写多个寄存器\n"); | |||
do | |||
{ | |||
if (i == 1) | |||
{ | |||
i = 0; | |||
cout << "请输入选择的功能码:"; | |||
} | |||
else | |||
cout << "输入功能码错误,请重新选择的功能码"; | |||
cin >> Function_code; | |||
cin.clear(); | |||
cin.sync(); | |||
} while (Function_code != 0x01 && Function_code != 0x03 && Function_code != 0x0F && Function_code != 0x10); | |||
return Function_code; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 输入操作起始地址码 | |||
* 描述 : 起始地址码,最大值65535 | |||
* 输入 : 无 | |||
* 输出 : Starting_address 操作起始地址码 | |||
*********************************************************************************************/ | |||
unsigned int Input_Starting_address(void) | |||
{ | |||
unsigned int Starting_address; | |||
int i = 1; | |||
cin >> dec; | |||
do | |||
{ | |||
if (i == 1) | |||
{ | |||
i = 0; | |||
cout << "请输入操作(十进制)起始地址:"; | |||
} | |||
else | |||
cout << "请重新输入操作(十进制)起始地址"; | |||
cin >> Starting_address; | |||
cin.clear(); | |||
cin.sync(); | |||
} while (Starting_address > 65535 || Starting_address < 0); | |||
return Starting_address; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 输入操作数量 | |||
* 描述 : 操作数量,0x01 1-200 0x03 1-125 0x0xF 1-1968 0x10 1-123 | |||
* 输入 : Function_code 选择的功能码类型 | |||
* 输出 : Operations_Number 对应功能码类型的操作数量 | |||
*********************************************************************************************/ | |||
unsigned int Input_Operations_Number(int Function_code) | |||
{ | |||
unsigned int Operations_Number; | |||
int i = 1; | |||
bool flage = true; | |||
printf(" 功能码 0x01 0x03 0x0F 0x10\n"); | |||
printf("操作数量 1-2000 1-125 1-1968 1-123\n"); | |||
cin >> dec; | |||
do | |||
{ | |||
if (i == 1) | |||
{ | |||
i = 0; | |||
cout << "请输入操作(十进制)数量:"; | |||
} | |||
else | |||
cout << "请重新输入操作(十进制)数量"; | |||
cin >> Operations_Number; | |||
cin.clear(); | |||
cin.sync(); | |||
if (Function_code == 0x01 && Operations_Number >= 1 && Operations_Number <= 2000) | |||
flage = false; | |||
if (Function_code == 0x03 && Operations_Number >= 1 && Operations_Number <= 125) | |||
flage = false; | |||
if (Function_code == 0x0F && Operations_Number >= 1 && Operations_Number <= 1968) | |||
flage = false; | |||
if (Function_code == 0x10 && Operations_Number >= 1 && Operations_Number <= 123) | |||
flage = false; | |||
} while (flage); | |||
return Operations_Number; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 检测写入数据 | |||
* 描述 : 检测写入数据格式是否正确 | |||
* 输入 : Write_date 写入的数据 Write_date_number写入数据的组数 | |||
* 输出 : true 写入数据格式正确 | |||
* false 写入数据格式错误 | |||
*********************************************************************************************/ | |||
bool Check_Write_date(string Write_date, int Write_date_number) | |||
{ | |||
int Write_date_len = 2 * Write_date_number + Write_date_number - 1; | |||
if (Write_date.length() != Write_date_len) | |||
return false; | |||
int Space_number = 0; | |||
int j = -1; | |||
for (unsigned int i = 0; i < Write_date.length(); i++) | |||
{ | |||
if (Write_date[i] == 0x20)//空格位置判定 | |||
{ | |||
if (i - j == 3) | |||
{ | |||
Space_number++; | |||
j = i; | |||
continue; | |||
} | |||
else | |||
return false; | |||
} | |||
if ((Write_date[i] >= 0x30 && Write_date[i] <= 0x39)) // 1-9 | |||
continue; | |||
else if ((Write_date[i] >= 0x41 && Write_date[i] <= 0x5A)) //A-Z | |||
continue; | |||
else if ((Write_date[i] >= 0x61 && Write_date[i] <= 0x7A)) //a-z | |||
continue; | |||
else | |||
return false; | |||
} | |||
if (Space_number != Write_date_number - 1) | |||
return false; | |||
return true; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 计算写入数据的字节数 | |||
* 描述 : 通过对应的功能码和操作数量计算对应的数据字节数 | |||
* 输入 : Function_code 选择的功能码类型 Operations_Number对应功能码类型的操作数量 | |||
* 输出 : Write_date_number 写入数据的字节数 | |||
*********************************************************************************************/ | |||
unsigned int Count_Write_date_number(int Function_code, unsigned int Operations_Number) | |||
{ | |||
unsigned int Write_date_number = 0; | |||
if (Function_code == 0x0F) //读线圈 | |||
{ | |||
Write_date_number = Operations_Number / 8; | |||
if (Operations_Number % 8) | |||
Write_date_number++; | |||
} | |||
if (Function_code == 0x10) | |||
Write_date_number = Operations_Number * 2; | |||
return Write_date_number; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 输入写入数据 | |||
* 描述 : 根据写入数量和功能码类型输入相应的数据 | |||
* 输入 : Function_code 选择的功能码类型 Operations_Number对应功能码类型的操作数量 | |||
* 输出 : Write_date 写入的数据 | |||
*********************************************************************************************/ | |||
string Input_Write_date(int Function_code, unsigned int Operations_Number) | |||
{ | |||
int i = 1; | |||
string Write_date; | |||
unsigned int Write_date_number = Count_Write_date_number(Function_code, Operations_Number); | |||
if (Function_code == 0x01 || Function_code == 0x03) | |||
return Write_date;//读线圈/寄存器不需要输入写入数据 | |||
printf("请输入N组写入数据(2位16进制为一组,空格间隔)例如:00 00 01 00 20 05\n"); | |||
do | |||
{ | |||
if (i == 1) | |||
{ | |||
i = 0; | |||
printf("请输入%d组写入数据 :", Write_date_number); | |||
} | |||
else | |||
printf("请重新输入%d组写入数据 :", Write_date_number); | |||
getline(cin, Write_date); | |||
cin.clear(); | |||
cin.sync(); | |||
} while (!Check_Write_date(Write_date, Write_date_number)); | |||
return Write_date; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 生成MBAP报头 | |||
* 描述 : MBAP报文头的包括的内容: | |||
* +-------------+---------+--------+--------+------------------------------+ | |||
* | 域 | 长度 | 客户机 | 服务器 | 描述 | | |||
* +-------------+---------+--------+--------+------------------------------+ | |||
* |事务元标识符 | 2个字节 | 启动 | 复制 |请求/响应事务处理的识别码 | | |||
* +-------------+---------+--------+--------+------------------------------+ | |||
* |协议标识符 | 2个字节 | 启动 | 复制 |0=MODBUS 协议 | | |||
* +-------------+---------+--------+--------+------------------------------+ | |||
* | 长度 | 2个字节 | 启动 | 启动 |以下字节的数量 | | |||
* +-------------+---------+--------+--------+------------------------------+ | |||
* |单元标识符 | 1个字节 | 启动 | 复制 |连接的远程从站的识别码 | | |||
* 输入 : 无 | |||
* 输出 : MBAP报文头内容 | |||
*********************************************************************************************/ | |||
void Crate_MBAP(UINT8 *Message, int Function_code, unsigned int Operations_Number) | |||
{ | |||
Message[0] = 0x00; | |||
Message[1] = 0x00; | |||
Message[2] = 0x00; | |||
Message[3] = 0x00; | |||
Message[4] = 0x00; | |||
Message[5] = Count_Write_date_number(Function_code, Operations_Number) + 0x06; | |||
Message[6] = DEVICE_ID; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 字符串转UINT8类型 | |||
* 描述 : 根据写入数量和功能码类型输入相应的数据 | |||
* 输入 : *Message 消息帧存放的数组 Write_date 要转换的字符串 | |||
* Message_len消息帧数组的起始位置 | |||
* 输出 : Message_len 消息帧的长度 | |||
*********************************************************************************************/ | |||
int HexStringtoByte(UINT8 *Message, string Write_date, int Message_len, int Function_code, unsigned int Operations_Number) | |||
{ | |||
if (Write_date.length() == 0) | |||
return Message_len; | |||
Message[Message_len] = Count_Write_date_number(Function_code, Operations_Number); | |||
Message_len++; | |||
const char *b = Write_date.c_str(); | |||
for (unsigned int i = 0; i + 3 < Write_date.length(); i = i + 3) | |||
{ | |||
sscanf_s(b + i, "%02X", (int *)(Message + Message_len)); | |||
Message_len++; | |||
} | |||
sscanf_s(b + Write_date.length() - 2, "%02X", (int *)(Message + Message_len)); | |||
return ++Message_len; | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 生成TCP模式下的消息帧 | |||
* 描述 : 根据要写入的数据生成消息帧 | |||
* 输入 : *Message 消息帧存放的数组 Write_date 写入数据的字符串 | |||
* Function_code 功能码 Operations_Number 操作数量 Starting_address起始地址 | |||
* 输出 : 消息帧的总长度 | |||
*********************************************************************************************/ | |||
int Crate_TCP_Message(UINT8 *Message, int Function_code, unsigned int Operations_Number, unsigned int Starting_address, string Write_date) | |||
{ | |||
Crate_MBAP(Message, Function_code, Operations_Number); | |||
Message[7] = Function_code;//功能码 | |||
Message[8] = Starting_address >> 8;//起始地址H | |||
Message[9] = Starting_address;//起始地址L | |||
Message[10] = Operations_Number >> 8;//操作数量H | |||
Message[11] = Operations_Number;//操作数量L | |||
return HexStringtoByte(Message, Write_date, 12, Function_code, Operations_Number); | |||
} | |||
/********************************************************************************************* | |||
* 功能 : 日志记录 | |||
* 描述 : 记录每次通信的请求和响应报文 | |||
* 输入 : *Message 消息帧存放的数组 flage = 1 请求报文 flage = 其他 响应报文 | |||
* Message_len 报文数据长度 | |||
* 输出 : 无 | |||
*********************************************************************************************/ | |||
void Log_Note(UINT8 *Message, int flage, int Message_len) | |||
{ | |||
FILE *fp = NULL; | |||
time_t timep; | |||
struct tm *p; | |||
time(&timep); | |||
p = gmtime(&timep); | |||
string Recv_str = to_string(1900 + p->tm_year) + "-" + to_string(1 + p->tm_mon) + "-" + to_string(p->tm_mday) | |||
+ " " + to_string(8 + p->tm_hour) + ":" + to_string(p->tm_min) + ":" + to_string(p->tm_sec) + " Recv:"; | |||
string Send_str= to_string(1900 + p->tm_year) + "-" + to_string(1 + p->tm_mon) + "-" + to_string(p->tm_mday) | |||
+ " " + to_string(8 + p->tm_hour) + ":" + to_string(p->tm_min) + ":" + to_string(p->tm_sec) + " Send:"; | |||
/* 打开文件用于读写 */ | |||
if ((fp = fopen("Modbus_TCP_log.txt", "a+")) == NULL) | |||
{ | |||
printf("打开文件失败"); | |||
return ; | |||
} | |||
if (flage == 1) | |||
{ | |||
fwrite(Send_str.c_str(), Send_str.length(), 1, fp); | |||
for (int i = 0; i < Message_len; i++) | |||
{ | |||
fprintf(fp, "%02X ", Message[i]); | |||
} | |||
fprintf(fp,"\n"); | |||
} | |||
else | |||
{ | |||
fwrite(Recv_str.c_str(), Recv_str.length(), 1, fp); | |||
for (int i = 0; i < Message_len; i++) | |||
{ | |||
fprintf(fp, "%02X ", Message[i]); | |||
} | |||
fprintf(fp, "\n"); | |||
} | |||
fclose(fp); | |||
fp = NULL; | |||
} |
@@ -0,0 +1,30 @@ | |||
#ifndef __COMMON_H | |||
#define __COMMON_H | |||
#include <stdio.h> | |||
#include <winsock2.h> | |||
#include <WS2tcpip.h> | |||
#include <vector> | |||
#include <string> | |||
#include <iostream> | |||
#include <time.h> | |||
#pragma comment(lib,"ws2_32.lib") | |||
using namespace std; | |||
#define DEVICE_ID 0x01 //É豸ID | |||
int Input_Function_code(void); | |||
unsigned int Input_Starting_address(void); | |||
unsigned int Input_Operations_Number(int Function_code); | |||
bool Check_Write_date(string Write_date, int Write_date_number); | |||
unsigned int Count_Write_date_number(int Function_code, unsigned int Operations_Number); | |||
string Input_Write_date(int Function_code, unsigned int Operations_Number); | |||
void Crate_MBAP(UINT8 *Message, int Function_code, unsigned int Operations_Number); | |||
int HexStringtoByte(UINT8 *Message, string Write_date, int Message_len); | |||
int Crate_TCP_Message(UINT8 *Message, int Function_code, unsigned int Operations_Number, unsigned int Starting_address, string Write_date); | |||
void Log_Note(UINT8 *Message, int flage, int Message_len); | |||
#endif |
@@ -0,0 +1,11 @@ | |||
#include "main.h" | |||
int main() | |||
{ | |||
string IP; | |||
unsigned int Port; | |||
Input_IP(IP,&Port); | |||
Tcp_client(IP,Port); | |||
getchar(); | |||
return 0; | |||
} |
@@ -0,0 +1,6 @@ | |||
#ifndef __MAIN_H | |||
#define __MAIN_H | |||
#include "TCP_client.h" | |||
#endif |
@@ -3,7 +3,25 @@ Microsoft Visual Studio Solution File, Format Version 12.00 | |||
# Visual Studio 2013 | |||
VisualStudioVersion = 12.0.30501.0 | |||
MinimumVisualStudioVersion = 10.0.40219.1 | |||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Modbus_RTU_Salve", "Modbus_RTU_Salve\Modbus_RTU_Salve.vcxproj", "{8C67F4EB-BDBE-4605-9291-5A535AC44AE2}" | |||
EndProject | |||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Modbus_TCP", "Modbus_TCP\Modbus_TCP.vcxproj", "{CC08BE54-3DFF-41F2-9F8B-17E0FD5E3757}" | |||
EndProject | |||
Global | |||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | |||
Debug|Win32 = Debug|Win32 | |||
Release|Win32 = Release|Win32 | |||
EndGlobalSection | |||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | |||
{8C67F4EB-BDBE-4605-9291-5A535AC44AE2}.Debug|Win32.ActiveCfg = Debug|Win32 | |||
{8C67F4EB-BDBE-4605-9291-5A535AC44AE2}.Debug|Win32.Build.0 = Debug|Win32 | |||
{8C67F4EB-BDBE-4605-9291-5A535AC44AE2}.Release|Win32.ActiveCfg = Release|Win32 | |||
{8C67F4EB-BDBE-4605-9291-5A535AC44AE2}.Release|Win32.Build.0 = Release|Win32 | |||
{CC08BE54-3DFF-41F2-9F8B-17E0FD5E3757}.Debug|Win32.ActiveCfg = Debug|Win32 | |||
{CC08BE54-3DFF-41F2-9F8B-17E0FD5E3757}.Debug|Win32.Build.0 = Debug|Win32 | |||
{CC08BE54-3DFF-41F2-9F8B-17E0FD5E3757}.Release|Win32.ActiveCfg = Release|Win32 | |||
{CC08BE54-3DFF-41F2-9F8B-17E0FD5E3757}.Release|Win32.Build.0 = Release|Win32 | |||
EndGlobalSection | |||
GlobalSection(SolutionProperties) = preSolution | |||
HideSolutionNode = FALSE | |||
EndGlobalSection | |||