Browse Source

添加日志记录模块

Modbus_RTU
zcn1123 4 years ago
parent
commit
7bf0ff7cb6
3 changed files with 55 additions and 0 deletions
  1. +2
    -0
      Modbus_communication/Modbus_RTU_Salve/RTU_Salve.cpp
  2. +51
    -0
      Modbus_communication/Modbus_RTU_Salve/common.cpp
  3. +2
    -0
      Modbus_communication/Modbus_RTU_Salve/common.h

+ 2
- 0
Modbus_communication/Modbus_RTU_Salve/RTU_Salve.cpp View File

@@ -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;


+ 51
- 0
Modbus_communication/Modbus_RTU_Salve/common.cpp View File

@@ -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;
}



+ 2
- 0
Modbus_communication/Modbus_RTU_Salve/common.h View File

@@ -4,6 +4,7 @@
#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);
@@ -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

||||||
x
 
000:0
Loading…
Cancel
Save