| @@ -0,0 +1,77 @@ | |||
| <?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> | |||
| </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="main.cpp" /> | |||
| <ClCompile Include="RTU_common.cpp" /> | |||
| </ItemGroup> | |||
| <ItemGroup> | |||
| <ClInclude Include="main.h" /> | |||
| <ClInclude Include="RTU_common.h" /> | |||
| </ItemGroup> | |||
| <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | |||
| <ImportGroup Label="ExtensionTargets"> | |||
| </ImportGroup> | |||
| </Project> | |||
| @@ -0,0 +1,33 @@ | |||
| <?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_common.cpp"> | |||
| <Filter>源文件</Filter> | |||
| </ClCompile> | |||
| </ItemGroup> | |||
| <ItemGroup> | |||
| <ClInclude Include="main.h"> | |||
| <Filter>头文件</Filter> | |||
| </ClInclude> | |||
| <ClInclude Include="RTU_common.h"> | |||
| <Filter>头文件</Filter> | |||
| </ClInclude> | |||
| </ItemGroup> | |||
| </Project> | |||
| @@ -0,0 +1,176 @@ | |||
| #include "RTU_common.h" | |||
| char read_buf[MAX_NUMBER]; | |||
| /******************************************************************************************* | |||
| * 功能 : 打开串口 | |||
| * port : 串口号, 如("COM1") | |||
| * baud_rate: 波特率 | |||
| * date_bits: 数据位(有效范围4~8) | |||
| * stop_bit : 停止位 | |||
| * parity : 奇偶校验。默认为无校验。NOPARITY 0; ODDPARITY 1;EVENPARITY 2 | |||
| * dcb.StopBits= 0,1,2对应的是1bit,1.5bits,2bits. | |||
| * dcb.ByteSize=6,7,8时 dcb.StopBits不能为1 | |||
| * dcb.ByteSize=5时 dcb.StopBits不能为2 | |||
| ********************************************************************************************/ | |||
| HANDLE InitCOM(LPCTSTR Port, int baud_rate, BYTE date_bits, BYTE stop_bit, BYTE parity) | |||
| { | |||
| HANDLE hCom = CreateFile(Port, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);//同步方式打开串口 | |||
| if (INVALID_HANDLE_VALUE == hCom) | |||
| { | |||
| return INVALID_HANDLE_VALUE; | |||
| } | |||
| SetupComm(hCom, 4096, 4096);//设置缓存 | |||
| DCB dcb; | |||
| if (!GetCommState(hCom, &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(hCom, &dcb) << endl; | |||
| PurgeComm(hCom, PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT);//清除缓存 | |||
| //设置串口读写时间 | |||
| COMMTIMEOUTS CommTimeOuts; | |||
| GetCommTimeouts(hCom, &CommTimeOuts); | |||
| CommTimeOuts.ReadIntervalTimeout = 5; | |||
| CommTimeOuts.ReadTotalTimeoutMultiplier = 1; | |||
| CommTimeOuts.ReadTotalTimeoutConstant = 0; | |||
| CommTimeOuts.WriteTotalTimeoutMultiplier = 10; | |||
| CommTimeOuts.WriteTotalTimeoutConstant = 1000; | |||
| if (!SetCommTimeouts(hCom, &CommTimeOuts)) { | |||
| return INVALID_HANDLE_VALUE; | |||
| } | |||
| //创建线程,读取数据 | |||
| //HANDLE hReadCommThread = (HANDLE)CreateThread(NULL, 0, CommProc, &hCom, 0, NULL); | |||
| return hCom; | |||
| } | |||
| /********************************************************************************************* | |||
| * 功能 : 发送响应数据 | |||
| * 描述 : 向串口写入数据 | |||
| * 返回值 : 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; | |||
| } | |||
| /********************************************************************************************* | |||
| * 功能 : 获取串口读取缓存区中数据的字节数 | |||
| * 描述 : 向串口写入数据 | |||
| * 返回值 : 缓存区中数据的字节数 | |||
| * m_hComm : 串口句柄 | |||
| ********************************************************************************************/ | |||
| unsigned int GetBytesInCOM(HANDLE m_hComm) | |||
| { | |||
| DWORD dwError = 0; /** 错误码 */ | |||
| COMSTAT comstat; /** COMSTAT结构体,记录通信设备的状态信息 */ | |||
| memset(&comstat, 0, sizeof(COMSTAT)); | |||
| unsigned int BytesInQue = 0; | |||
| /** 在调用ReadFile和WriteFile之前,通过本函数清除以前遗留的错误标志 */ | |||
| if (ClearCommError(m_hComm, &dwError, &comstat)) | |||
| { | |||
| BytesInQue = comstat.cbInQue; /** 获取在输入缓冲区中的字节数 */ | |||
| } | |||
| return BytesInQue; | |||
| } | |||
| 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); | |||
| } | |||
| int test(void) | |||
| { | |||
| string COMM; | |||
| cin >> COMM; | |||
| HANDLE H_Com = InitCOM((LPCTSTR)COMM.c_str(), 9600, 8, 0, 1); | |||
| if (H_Com == INVALID_HANDLE_VALUE) | |||
| { | |||
| cout << "初始化串口失败" << endl; | |||
| getchar(); | |||
| return 0; | |||
| } | |||
| char write_buf[MAX_NUMBER]; | |||
| memset(write_buf, 0, MAX_NUMBER); | |||
| DWORD dwRead; | |||
| while (true) | |||
| { | |||
| //RTU主站,生成并发送请求,计时,读取响应报文,先发送后接收 | |||
| //生成请求报文 | |||
| //发送请求报文 | |||
| PurgeComm(H_Com, PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT);//清除缓存 | |||
| while (1) | |||
| { | |||
| if (true == SendData(H_Com, "ok", 2)) | |||
| { | |||
| cout << "发送请求报文成功" << endl; | |||
| break; | |||
| } | |||
| } | |||
| BOOL bReadOK = ReadFile(H_Com, read_buf, 256, &dwRead, NULL); | |||
| if (bReadOK && (dwRead > 0)) | |||
| { | |||
| read_buf[dwRead] = '\0'; | |||
| printf("收到响应:%s \r\n", read_buf); | |||
| } | |||
| else | |||
| cout << "loss" << endl; | |||
| } | |||
| CloseHandle(H_Com); | |||
| getchar(); | |||
| return 0; | |||
| } | |||
| @@ -0,0 +1,12 @@ | |||
| #ifndef __RTU_COMMON_H | |||
| #define __RTU_COMMON_H | |||
| #include <Windows.h> | |||
| #include <string> | |||
| #include <iostream> | |||
| using namespace std; | |||
| #define MAX_NUMBER 300 | |||
| int test(); | |||
| #endif | |||
| @@ -0,0 +1,8 @@ | |||
| #include "main.h" | |||
| int main() | |||
| { | |||
| test(); | |||
| getchar(); | |||
| return 0; | |||
| } | |||
| @@ -0,0 +1,6 @@ | |||
| #ifndef __MAIN_H | |||
| #define __MAIN_H | |||
| #include "RTU_common.h" | |||
| #endif | |||
| @@ -3,7 +3,19 @@ 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 | |||
| 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 | |||
| EndGlobalSection | |||
| GlobalSection(SolutionProperties) = preSolution | |||
| HideSolutionNode = FALSE | |||
| EndGlobalSection | |||