From 7bf0ff7cb640274893bef60954ad068aed93247b Mon Sep 17 00:00:00 2001 From: zcn1123 <2363211205@qq.com> Date: Thu, 3 Sep 2020 21:39:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Modbus_RTU_Salve/RTU_Salve.cpp | 2 + .../Modbus_RTU_Salve/common.cpp | 51 +++++++++++++++++++ .../Modbus_RTU_Salve/common.h | 2 + 3 files changed, 55 insertions(+) diff --git a/Modbus_communication/Modbus_RTU_Salve/RTU_Salve.cpp b/Modbus_communication/Modbus_RTU_Salve/RTU_Salve.cpp index c68824c..fe55266 100644 --- a/Modbus_communication/Modbus_RTU_Salve/RTU_Salve.cpp +++ b/Modbus_communication/Modbus_RTU_Salve/RTU_Salve.cpp @@ -404,6 +404,8 @@ int Modbus_RTU_Salve(void) { printf("发送失败,重新发送"); } + Log_Note(Requst_Message, 1, Read_len); + Log_Note(Response_Message, 0, Response_Message_Len); } else continue; diff --git a/Modbus_communication/Modbus_RTU_Salve/common.cpp b/Modbus_communication/Modbus_RTU_Salve/common.cpp index 38eea15..935d04e 100644 --- a/Modbus_communication/Modbus_RTU_Salve/common.cpp +++ b/Modbus_communication/Modbus_RTU_Salve/common.cpp @@ -374,3 +374,54 @@ UINT16 CRC_16(UINT8 *Data, unsigned int CRC_Len) 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; +} + + diff --git a/Modbus_communication/Modbus_RTU_Salve/common.h b/Modbus_communication/Modbus_RTU_Salve/common.h index b68fe29..be8c489 100644 --- a/Modbus_communication/Modbus_RTU_Salve/common.h +++ b/Modbus_communication/Modbus_RTU_Salve/common.h @@ -4,6 +4,7 @@ #include #include #include +#include using namespace std; unsigned int Count_Read_date_number(int Function_code, unsigned int Operations_Number); @@ -15,5 +16,6 @@ BYTE Input_Date_Bits(void); BYTE Input_Stop_Bits(BYTE Date_Bits); BYTE Input_Parity(void); UINT16 CRC_16(UINT8 *Data, unsigned int CRC_Len); +void Log_Note(UINT8 *Message, int flage, int Message_len) #endif \ No newline at end of file