From c7d3455d3c32befa4d17c678d8b79b1722c6ed66 Mon Sep 17 00:00:00 2001 From: huyizhong Date: Thu, 18 Jul 2024 09:52:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=81=9AEMC=E5=AE=9E?= =?UTF-8?q?=E9=AA=8C=E6=96=B9=E4=BE=BF=E6=9F=A5=E7=9C=8B=E7=9A=84log?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=96=87=E4=BB=B6=EF=BC=8C=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E6=9A=82=E5=AD=98=E8=B7=AF=E5=BE=84=E6=94=B9=E4=B8=BA=E7=94=B1?= =?UTF-8?q?=E5=A4=96=E9=83=A8=E4=BC=A0=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TestComm/app/src/main/cpp/SensorsProtocol.cpp | 122 ++++++++++++++---- TestComm/app/src/main/cpp/SensorsProtocol.h | 7 +- app/src/main/cpp/SensorsProtocol.cpp | 122 ++++++++++++++---- app/src/main/cpp/SensorsProtocol.h | 7 +- 4 files changed, 202 insertions(+), 56 deletions(-) diff --git a/TestComm/app/src/main/cpp/SensorsProtocol.cpp b/TestComm/app/src/main/cpp/SensorsProtocol.cpp index 0bb649cf..ce30591e 100644 --- a/TestComm/app/src/main/cpp/SensorsProtocol.cpp +++ b/TestComm/app/src/main/cpp/SensorsProtocol.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -853,7 +854,7 @@ void Gm_InitSerialComm_Test() //sprintf(devparam[0].stopbit, "1"); sensorParam[0].stopbit = 1; sensorParam[0].parity = 'n'; - sensorParam[0].IsNoInsta = 0; + sensorParam[0].IsNoInsta = 1; sensorParam[0].devaddr = 4; memset(sensorParam[0].pathname, 0, sizeof(sensorParam[0].pathname)); sprintf(sensorParam[0].pathname, "/dev/ttysWK3"); @@ -865,7 +866,7 @@ void Gm_InitSerialComm_Test() //sprintf(devparam[1].stopbit, "1"); sensorParam[1].stopbit = 1; sensorParam[1].parity = 0; - sensorParam[1].IsNoInsta = 0; + sensorParam[1].IsNoInsta = 1; sensorParam[1].devaddr = 1; memset(sensorParam[1].pathname, 0, sizeof(sensorParam[1].pathname)); sprintf(sensorParam[1].pathname, "/dev/ttysWK3"); @@ -877,7 +878,7 @@ void Gm_InitSerialComm_Test() //sprintf(devparam[2].stopbit, "1"); sensorParam[2].stopbit = 1; sensorParam[2].parity = 0; - sensorParam[2].IsNoInsta = 0; + sensorParam[2].IsNoInsta = 1; //sensorParam[2].PowerPort = 3; //sensorParam[2].devaddr = 13; sensorParam[2].devaddr = 13; @@ -891,7 +892,7 @@ void Gm_InitSerialComm_Test() //sprintf(devparam[2].stopbit, "1"); sensorParam[5].stopbit = 1; sensorParam[5].parity = 0; - sensorParam[5].IsNoInsta = 0; + sensorParam[5].IsNoInsta = 1; sensorParam[5].devaddr = 10; memset(sensorParam[5].pathname, 0, sizeof(sensorParam[3].pathname)); sprintf(sensorParam[5].pathname, "/dev/ttysWK1"); @@ -903,7 +904,7 @@ void Gm_InitSerialComm_Test() //sprintf(devparam[2].stopbit, "1"); sensorParam[4].stopbit = 1; sensorParam[4].parity = 0; - sensorParam[4].IsNoInsta = 0; + sensorParam[4].IsNoInsta = 1; //sensorParam[2].PowerPort = 3; //sensorParam[2].devaddr = 13; sensorParam[4].devaddr = 12; @@ -932,7 +933,7 @@ void Gm_InitSerialComm_Test() sensorParam[7].stopbit = 1; sensorParam[7].parity = 0; //devparam[13].PowerPort = 4; - sensorParam[7].IsNoInsta = 0; + sensorParam[7].IsNoInsta = 1; sensorParam[7].devaddr = 3; memset(sensorParam[7].pathname, 0, sizeof(sensorParam[7].pathname)); sprintf(sensorParam[7].pathname, "/dev/ttysWK0"); @@ -945,7 +946,7 @@ void Gm_InitSerialComm_Test() sensorParam[13].stopbit = 1; sensorParam[13].parity = 0; //devparam[13].PowerPort = 4; - sensorParam[13].IsNoInsta = 0; + sensorParam[13].IsNoInsta = 1; sensorParam[13].devaddr = 5; memset(sensorParam[13].pathname, 0, sizeof(sensorParam[13].pathname)); sprintf(sensorParam[13].pathname, "/dev/ttysWK0"); @@ -977,18 +978,18 @@ void Gm_InitSerialComm_Test() sensorParam[3].databit = 8; sensorParam[3].stopbit = 1; sensorParam[3].parity = 0; - sensorParam[3].IsNoInsta = 0; + sensorParam[3].IsNoInsta = 1; //devparam[3].PowerPort = 15; sensorParam[3].CameraChannel = 1; sensorParam[3].devaddr = 0; memset(sensorParam[3].pathname, 0, sizeof(sensorParam[3].pathname)); sprintf(sensorParam[3].pathname, "/dev/ttyS1"); //#endif - Gm_InitSerialComm(sensorParam); + Gm_InitSerialComm(sensorParam, "/sdcard/photo/"); } // 初始化所有串口及所接传感器的配置 -void Gm_InitSerialComm(SENSOR_PARAM *sensorParam) +void Gm_InitSerialComm(SENSOR_PARAM *sensorParam, char *filedir) { int i; char szbuf[128]; @@ -1073,6 +1074,10 @@ void Gm_InitSerialComm(SENSOR_PARAM *sensorParam) DebugLog(8, szbuf, 'I'); } } + if(NULL == filedir) + return; + memset(srdt.filedir, 0, sizeof(srdt.filedir)); + memmove(srdt.filedir, filedir, min(sizeof(srdt.filedir), strlen(filedir))); FindDevUseSerialCommNo(); #if 0 //Collect_sensor_data(); @@ -1112,13 +1117,13 @@ void testComm() //memset(sensorParam, 0, sizeof(sensorParam)); Gm_InitSerialComm_Test(); - -#if COLLECT_DATA - for(;;) - Collect_sensor_data(); -#else for(;;) { +#if COLLECT_DATA + + Collect_sensor_data(); +#else + sleep(15); CameraPhotoCmd(time(NULL), 1, 0, 6, 1); sleep(5); @@ -1141,9 +1146,9 @@ void testComm() sleep(5); CameraPhotoCmd(time(NULL), 1, 0, 6, 1); sleep(5); - } -#endif +#endif + } #if 1 //******************** 端口基本信息 ************************ /*for(i=0; iimage.photoname, 0, sizeof(pPortParam->image.photoname)); - sprintf(pPortParam->image.photoname, "%s%d-%d-%d.jpg", filedir,devparam[devno].CameraChannel, pPortParam->image.presetno, pPortParam->image.phototime); + sprintf(pPortParam->image.photoname, "%s%d-%d-%d.jpg", srdt.filedir,devparam[devno].CameraChannel, pPortParam->image.presetno, pPortParam->image.phototime); fp = fopen(pPortParam->image.photoname, "wb+"); if(NULL == fp) return -1; @@ -2917,7 +2922,7 @@ void MakeShxyProtocolPollCommand(int portno, BYTE cmdidx) sendbuf = serialport[devparam[portno].commid].PollCmd; /* 测试变量*/ - cmdidx =2; + cmdidx =cmdidx; i = 0; sendbuf[i++] = 0x00; // 强制等待时间 @@ -3364,6 +3369,54 @@ void ShxyProtocolDataProcess( int devno) } } +void delete_old_files(const char *path, int days) +{ + struct stat file_stat; + struct tm *file_tm; + time_t now = time(NULL); + DIR *dir = opendir(path); + struct dirent *entry; + char szbuf[1024]; + char fullpath[256]; + + memset(szbuf, 0, sizeof(szbuf)); + if (!dir) + { + sprintf(szbuf, "delete_old_files opendir %s error ", path); + DebugLog(8, szbuf, 'E'); + return; + } + + while ((entry = readdir(dir))) + { + memset(szbuf, 0, sizeof(szbuf)); + if (entry->d_type == DT_REG) + { // 只处理普通文件 + snprintf(fullpath, sizeof(fullpath), "%s/%s", path, entry->d_name); + + if (stat(fullpath, &file_stat) == -1) + { + perror("stat"); + sprintf(szbuf, "stat"); + DebugLog(8, szbuf, 'E'); + continue; + } + + localtime_r(&(file_stat.st_mtime), file_tm); + //file_tm = localtime(&(file_stat.st_mtime)); + + if (difftime(now, mktime(file_tm)) > days * 24 * 60 * 60) + { + if (unlink(fullpath) == -1) + { // 删除文件 + perror("unlink"); + } + } + } + } + + closedir(dir); +} /********************************************************************************* 把16进制和10进制ASCII字符串转换成int整数 *********************************************************************************/ @@ -3462,6 +3515,23 @@ void Collect_sensor_data() devparam[i].CameraChannel = sensorParam[i].CameraChannel; devparam[i].Phase = sensorParam[i].Phase; } +#endif +#if 1 + static int ideletefile =0; + time_t now; + struct tm t0; + const char *path = "/sdcard/log"; // 指定目录路径 + int days = 15; // 删除15天前的log文件 + + now = time(NULL); + localtime_r(&now, &t0); + if((0 == t0.tm_hour) && (0 == ideletefile)) + { + delete_old_files(path, days); + ideletefile++; + } + if(0 < t0.tm_hour) + ideletefile = 0; #endif GM_StartSerialComm(); } diff --git a/TestComm/app/src/main/cpp/SensorsProtocol.h b/TestComm/app/src/main/cpp/SensorsProtocol.h index 004de32a..6b9cc830 100644 --- a/TestComm/app/src/main/cpp/SensorsProtocol.h +++ b/TestComm/app/src/main/cpp/SensorsProtocol.h @@ -34,6 +34,7 @@ typedef long long LONG; #define LOWORD(l) ((WORD)(l)) #define HIWORD(l) ((WORD)((DWORD)(l) >> 16)) +#define min(a, b) ((a) < (b) ? (a) : (b)) #define MAX_STRING_LEN 32 #define IOT_PARAM_WRITE 0xAE #define IOT_PARAM_READ 0xAF @@ -133,7 +134,7 @@ typedef long long LONG; #define SPEED_DOME_CAMERA 0 /* 球机摄像机*/ #define SERIAL_CAMERA 2 /* 串口摄像机a*/ -#define COLLECT_DATA 1 /* 调试使用*/ +#define COLLECT_DATA 0 /* 调试使用*/ #define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, "serial_port_comm", fmt, ##args) /* 红色*/ #define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, "Sensors_Protocol", fmt, ##args) /* 草绿色*/ @@ -313,6 +314,7 @@ typedef struct int iLastGetPhotoNo; /* 设置串口摄像机参数时暂存拍照命令序号*/ u_char bImageSize; /* 用于临时存储接收上层命令的图片大小*/ u_char presetno; /* 用于临时存储接收上层命令的预置点*/ + char filedir[512]; /* 用于摄像机拍照之后暂时存放的路径*/ #endif u_char errorPhotoNoCnt; /* 串口摄像机拍照时回应错误包号计数(如:召第6包回应第3包)*/ u_char RephotographCnt; /* 串口摄像机重拍计数(只在读照片数据应答出错时才重拍)*/ @@ -347,11 +349,12 @@ void DebugLog(int commid, char *szbuf, char flag); int SaveLogTofile(int commid, char *szbuf); // 功能说明:串口发送数据 返回实际发送的字节数 int GM_SerialComSend(const unsigned char * cSendBuf, LONG nSendLen, int commid); -void Gm_InitSerialComm(SENSOR_PARAM *sensorParam); +void Gm_InitSerialComm(SENSOR_PARAM *sensorParam, char *filedir); // 启动串口通讯 void GM_StartSerialComm(); // 启动使用串口拍照 void GM_StartSerialCameraPhoto(BYTE channel, int cmdidx); +void delete_old_files(const char *path, int days); // 串口轮询通讯定时器 int GM_SerialTimer(); diff --git a/app/src/main/cpp/SensorsProtocol.cpp b/app/src/main/cpp/SensorsProtocol.cpp index 0bb649cf..ce30591e 100644 --- a/app/src/main/cpp/SensorsProtocol.cpp +++ b/app/src/main/cpp/SensorsProtocol.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -853,7 +854,7 @@ void Gm_InitSerialComm_Test() //sprintf(devparam[0].stopbit, "1"); sensorParam[0].stopbit = 1; sensorParam[0].parity = 'n'; - sensorParam[0].IsNoInsta = 0; + sensorParam[0].IsNoInsta = 1; sensorParam[0].devaddr = 4; memset(sensorParam[0].pathname, 0, sizeof(sensorParam[0].pathname)); sprintf(sensorParam[0].pathname, "/dev/ttysWK3"); @@ -865,7 +866,7 @@ void Gm_InitSerialComm_Test() //sprintf(devparam[1].stopbit, "1"); sensorParam[1].stopbit = 1; sensorParam[1].parity = 0; - sensorParam[1].IsNoInsta = 0; + sensorParam[1].IsNoInsta = 1; sensorParam[1].devaddr = 1; memset(sensorParam[1].pathname, 0, sizeof(sensorParam[1].pathname)); sprintf(sensorParam[1].pathname, "/dev/ttysWK3"); @@ -877,7 +878,7 @@ void Gm_InitSerialComm_Test() //sprintf(devparam[2].stopbit, "1"); sensorParam[2].stopbit = 1; sensorParam[2].parity = 0; - sensorParam[2].IsNoInsta = 0; + sensorParam[2].IsNoInsta = 1; //sensorParam[2].PowerPort = 3; //sensorParam[2].devaddr = 13; sensorParam[2].devaddr = 13; @@ -891,7 +892,7 @@ void Gm_InitSerialComm_Test() //sprintf(devparam[2].stopbit, "1"); sensorParam[5].stopbit = 1; sensorParam[5].parity = 0; - sensorParam[5].IsNoInsta = 0; + sensorParam[5].IsNoInsta = 1; sensorParam[5].devaddr = 10; memset(sensorParam[5].pathname, 0, sizeof(sensorParam[3].pathname)); sprintf(sensorParam[5].pathname, "/dev/ttysWK1"); @@ -903,7 +904,7 @@ void Gm_InitSerialComm_Test() //sprintf(devparam[2].stopbit, "1"); sensorParam[4].stopbit = 1; sensorParam[4].parity = 0; - sensorParam[4].IsNoInsta = 0; + sensorParam[4].IsNoInsta = 1; //sensorParam[2].PowerPort = 3; //sensorParam[2].devaddr = 13; sensorParam[4].devaddr = 12; @@ -932,7 +933,7 @@ void Gm_InitSerialComm_Test() sensorParam[7].stopbit = 1; sensorParam[7].parity = 0; //devparam[13].PowerPort = 4; - sensorParam[7].IsNoInsta = 0; + sensorParam[7].IsNoInsta = 1; sensorParam[7].devaddr = 3; memset(sensorParam[7].pathname, 0, sizeof(sensorParam[7].pathname)); sprintf(sensorParam[7].pathname, "/dev/ttysWK0"); @@ -945,7 +946,7 @@ void Gm_InitSerialComm_Test() sensorParam[13].stopbit = 1; sensorParam[13].parity = 0; //devparam[13].PowerPort = 4; - sensorParam[13].IsNoInsta = 0; + sensorParam[13].IsNoInsta = 1; sensorParam[13].devaddr = 5; memset(sensorParam[13].pathname, 0, sizeof(sensorParam[13].pathname)); sprintf(sensorParam[13].pathname, "/dev/ttysWK0"); @@ -977,18 +978,18 @@ void Gm_InitSerialComm_Test() sensorParam[3].databit = 8; sensorParam[3].stopbit = 1; sensorParam[3].parity = 0; - sensorParam[3].IsNoInsta = 0; + sensorParam[3].IsNoInsta = 1; //devparam[3].PowerPort = 15; sensorParam[3].CameraChannel = 1; sensorParam[3].devaddr = 0; memset(sensorParam[3].pathname, 0, sizeof(sensorParam[3].pathname)); sprintf(sensorParam[3].pathname, "/dev/ttyS1"); //#endif - Gm_InitSerialComm(sensorParam); + Gm_InitSerialComm(sensorParam, "/sdcard/photo/"); } // 初始化所有串口及所接传感器的配置 -void Gm_InitSerialComm(SENSOR_PARAM *sensorParam) +void Gm_InitSerialComm(SENSOR_PARAM *sensorParam, char *filedir) { int i; char szbuf[128]; @@ -1073,6 +1074,10 @@ void Gm_InitSerialComm(SENSOR_PARAM *sensorParam) DebugLog(8, szbuf, 'I'); } } + if(NULL == filedir) + return; + memset(srdt.filedir, 0, sizeof(srdt.filedir)); + memmove(srdt.filedir, filedir, min(sizeof(srdt.filedir), strlen(filedir))); FindDevUseSerialCommNo(); #if 0 //Collect_sensor_data(); @@ -1112,13 +1117,13 @@ void testComm() //memset(sensorParam, 0, sizeof(sensorParam)); Gm_InitSerialComm_Test(); - -#if COLLECT_DATA - for(;;) - Collect_sensor_data(); -#else for(;;) { +#if COLLECT_DATA + + Collect_sensor_data(); +#else + sleep(15); CameraPhotoCmd(time(NULL), 1, 0, 6, 1); sleep(5); @@ -1141,9 +1146,9 @@ void testComm() sleep(5); CameraPhotoCmd(time(NULL), 1, 0, 6, 1); sleep(5); - } -#endif +#endif + } #if 1 //******************** 端口基本信息 ************************ /*for(i=0; iimage.photoname, 0, sizeof(pPortParam->image.photoname)); - sprintf(pPortParam->image.photoname, "%s%d-%d-%d.jpg", filedir,devparam[devno].CameraChannel, pPortParam->image.presetno, pPortParam->image.phototime); + sprintf(pPortParam->image.photoname, "%s%d-%d-%d.jpg", srdt.filedir,devparam[devno].CameraChannel, pPortParam->image.presetno, pPortParam->image.phototime); fp = fopen(pPortParam->image.photoname, "wb+"); if(NULL == fp) return -1; @@ -2917,7 +2922,7 @@ void MakeShxyProtocolPollCommand(int portno, BYTE cmdidx) sendbuf = serialport[devparam[portno].commid].PollCmd; /* 测试变量*/ - cmdidx =2; + cmdidx =cmdidx; i = 0; sendbuf[i++] = 0x00; // 强制等待时间 @@ -3364,6 +3369,54 @@ void ShxyProtocolDataProcess( int devno) } } +void delete_old_files(const char *path, int days) +{ + struct stat file_stat; + struct tm *file_tm; + time_t now = time(NULL); + DIR *dir = opendir(path); + struct dirent *entry; + char szbuf[1024]; + char fullpath[256]; + + memset(szbuf, 0, sizeof(szbuf)); + if (!dir) + { + sprintf(szbuf, "delete_old_files opendir %s error ", path); + DebugLog(8, szbuf, 'E'); + return; + } + + while ((entry = readdir(dir))) + { + memset(szbuf, 0, sizeof(szbuf)); + if (entry->d_type == DT_REG) + { // 只处理普通文件 + snprintf(fullpath, sizeof(fullpath), "%s/%s", path, entry->d_name); + + if (stat(fullpath, &file_stat) == -1) + { + perror("stat"); + sprintf(szbuf, "stat"); + DebugLog(8, szbuf, 'E'); + continue; + } + + localtime_r(&(file_stat.st_mtime), file_tm); + //file_tm = localtime(&(file_stat.st_mtime)); + + if (difftime(now, mktime(file_tm)) > days * 24 * 60 * 60) + { + if (unlink(fullpath) == -1) + { // 删除文件 + perror("unlink"); + } + } + } + } + + closedir(dir); +} /********************************************************************************* 把16进制和10进制ASCII字符串转换成int整数 *********************************************************************************/ @@ -3462,6 +3515,23 @@ void Collect_sensor_data() devparam[i].CameraChannel = sensorParam[i].CameraChannel; devparam[i].Phase = sensorParam[i].Phase; } +#endif +#if 1 + static int ideletefile =0; + time_t now; + struct tm t0; + const char *path = "/sdcard/log"; // 指定目录路径 + int days = 15; // 删除15天前的log文件 + + now = time(NULL); + localtime_r(&now, &t0); + if((0 == t0.tm_hour) && (0 == ideletefile)) + { + delete_old_files(path, days); + ideletefile++; + } + if(0 < t0.tm_hour) + ideletefile = 0; #endif GM_StartSerialComm(); } diff --git a/app/src/main/cpp/SensorsProtocol.h b/app/src/main/cpp/SensorsProtocol.h index 004de32a..6b9cc830 100644 --- a/app/src/main/cpp/SensorsProtocol.h +++ b/app/src/main/cpp/SensorsProtocol.h @@ -34,6 +34,7 @@ typedef long long LONG; #define LOWORD(l) ((WORD)(l)) #define HIWORD(l) ((WORD)((DWORD)(l) >> 16)) +#define min(a, b) ((a) < (b) ? (a) : (b)) #define MAX_STRING_LEN 32 #define IOT_PARAM_WRITE 0xAE #define IOT_PARAM_READ 0xAF @@ -133,7 +134,7 @@ typedef long long LONG; #define SPEED_DOME_CAMERA 0 /* 球机摄像机*/ #define SERIAL_CAMERA 2 /* 串口摄像机a*/ -#define COLLECT_DATA 1 /* 调试使用*/ +#define COLLECT_DATA 0 /* 调试使用*/ #define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, "serial_port_comm", fmt, ##args) /* 红色*/ #define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, "Sensors_Protocol", fmt, ##args) /* 草绿色*/ @@ -313,6 +314,7 @@ typedef struct int iLastGetPhotoNo; /* 设置串口摄像机参数时暂存拍照命令序号*/ u_char bImageSize; /* 用于临时存储接收上层命令的图片大小*/ u_char presetno; /* 用于临时存储接收上层命令的预置点*/ + char filedir[512]; /* 用于摄像机拍照之后暂时存放的路径*/ #endif u_char errorPhotoNoCnt; /* 串口摄像机拍照时回应错误包号计数(如:召第6包回应第3包)*/ u_char RephotographCnt; /* 串口摄像机重拍计数(只在读照片数据应答出错时才重拍)*/ @@ -347,11 +349,12 @@ void DebugLog(int commid, char *szbuf, char flag); int SaveLogTofile(int commid, char *szbuf); // 功能说明:串口发送数据 返回实际发送的字节数 int GM_SerialComSend(const unsigned char * cSendBuf, LONG nSendLen, int commid); -void Gm_InitSerialComm(SENSOR_PARAM *sensorParam); +void Gm_InitSerialComm(SENSOR_PARAM *sensorParam, char *filedir); // 启动串口通讯 void GM_StartSerialComm(); // 启动使用串口拍照 void GM_StartSerialCameraPhoto(BYTE channel, int cmdidx); +void delete_old_files(const char *path, int days); // 串口轮询通讯定时器 int GM_SerialTimer();