From 558873c767a140c6fbac602dbb4bf6be3bc632db Mon Sep 17 00:00:00 2001 From: zcn1123 <2363211205@qq.com> Date: Tue, 1 Sep 2020 19:55:35 +0800 Subject: [PATCH] =?UTF-8?q?=20=E6=B7=BB=E5=8A=A0=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=BA=90=E6=96=87=E4=BB=B6=E5=92=8C=E9=83=A8=E5=88=86=E9=80=9A?= =?UTF-8?q?=E4=BF=A1=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Modbus_RTU_Salve/Modbus_RTU_Salve.vcxproj | 77 ++++++++ .../Modbus_RTU_Salve.vcxproj.filters | 33 ++++ .../Modbus_RTU_Salve/RTU_common.cpp | 176 ++++++++++++++++++ .../Modbus_RTU_Salve/RTU_common.h | 12 ++ .../Modbus_RTU_Salve/main.cpp | 8 + Modbus_communication/Modbus_RTU_Salve/main.h | 6 + Modbus_communication/Modbus_communication.sln | 12 ++ 7 files changed, 324 insertions(+) create mode 100644 Modbus_communication/Modbus_RTU_Salve/Modbus_RTU_Salve.vcxproj create mode 100644 Modbus_communication/Modbus_RTU_Salve/Modbus_RTU_Salve.vcxproj.filters create mode 100644 Modbus_communication/Modbus_RTU_Salve/RTU_common.cpp create mode 100644 Modbus_communication/Modbus_RTU_Salve/RTU_common.h create mode 100644 Modbus_communication/Modbus_RTU_Salve/main.cpp create mode 100644 Modbus_communication/Modbus_RTU_Salve/main.h diff --git a/Modbus_communication/Modbus_RTU_Salve/Modbus_RTU_Salve.vcxproj b/Modbus_communication/Modbus_RTU_Salve/Modbus_RTU_Salve.vcxproj new file mode 100644 index 0000000..06c5c74 --- /dev/null +++ b/Modbus_communication/Modbus_RTU_Salve/Modbus_RTU_Salve.vcxproj @@ -0,0 +1,77 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {8C67F4EB-BDBE-4605-9291-5A535AC44AE2} + Modbus_RTU_Salve + + + + Application + true + v120 + MultiByte + + + Application + false + v120 + true + MultiByte + + + + + + + + + + + + + + + Level3 + Disabled + true + + + true + + + + + Level3 + MaxSpeed + true + true + true + + + true + true + true + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Modbus_communication/Modbus_RTU_Salve/Modbus_RTU_Salve.vcxproj.filters b/Modbus_communication/Modbus_RTU_Salve/Modbus_RTU_Salve.vcxproj.filters new file mode 100644 index 0000000..db7e967 --- /dev/null +++ b/Modbus_communication/Modbus_RTU_Salve/Modbus_RTU_Salve.vcxproj.filters @@ -0,0 +1,33 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + 源文件 + + + + + 头文件 + + + 头文件 + + + \ No newline at end of file diff --git a/Modbus_communication/Modbus_RTU_Salve/RTU_common.cpp b/Modbus_communication/Modbus_RTU_Salve/RTU_common.cpp new file mode 100644 index 0000000..2dd4875 --- /dev/null +++ b/Modbus_communication/Modbus_RTU_Salve/RTU_common.cpp @@ -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> 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; +} \ No newline at end of file diff --git a/Modbus_communication/Modbus_RTU_Salve/RTU_common.h b/Modbus_communication/Modbus_RTU_Salve/RTU_common.h new file mode 100644 index 0000000..114ffed --- /dev/null +++ b/Modbus_communication/Modbus_RTU_Salve/RTU_common.h @@ -0,0 +1,12 @@ +#ifndef __RTU_COMMON_H +#define __RTU_COMMON_H + +#include +#include +#include +using namespace std; + +#define MAX_NUMBER 300 + +int test(); +#endif \ No newline at end of file diff --git a/Modbus_communication/Modbus_RTU_Salve/main.cpp b/Modbus_communication/Modbus_RTU_Salve/main.cpp new file mode 100644 index 0000000..294c7c4 --- /dev/null +++ b/Modbus_communication/Modbus_RTU_Salve/main.cpp @@ -0,0 +1,8 @@ +#include "main.h" + +int main() +{ + test(); + getchar(); + return 0; +} \ No newline at end of file diff --git a/Modbus_communication/Modbus_RTU_Salve/main.h b/Modbus_communication/Modbus_RTU_Salve/main.h new file mode 100644 index 0000000..9524beb --- /dev/null +++ b/Modbus_communication/Modbus_RTU_Salve/main.h @@ -0,0 +1,6 @@ +#ifndef __MAIN_H +#define __MAIN_H + +#include "RTU_common.h" + +#endif \ No newline at end of file diff --git a/Modbus_communication/Modbus_communication.sln b/Modbus_communication/Modbus_communication.sln index 437b87b..b8f423a 100644 --- a/Modbus_communication/Modbus_communication.sln +++ b/Modbus_communication/Modbus_communication.sln @@ -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