From a7b7623c932b9afbff67415e939ad9d4d42c0863 Mon Sep 17 00:00:00 2001 From: jxjajs Date: Tue, 10 Dec 2024 19:18:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=B2=E5=8F=A3=E9=80=9A=E8=AE=AF=E5=8A=A0?= =?UTF-8?q?=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/cpp/SensorsProtocol.cpp | 35 ++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/app/src/main/cpp/SensorsProtocol.cpp b/app/src/main/cpp/SensorsProtocol.cpp index 6f38ac76..0727bc6f 100644 --- a/app/src/main/cpp/SensorsProtocol.cpp +++ b/app/src/main/cpp/SensorsProtocol.cpp @@ -22,12 +22,15 @@ #include "GPIOControl.h" #include "SerialComm.h" #include +#include #include #include //#include "Eint.h" #include +pthread_mutex_t serial_mutex = PTHREAD_MUTEX_INITIALIZER; // 定义一个互斥锁 +pthread_mutex_t camera_mutex = PTHREAD_MUTEX_INITIALIZER; // 定义一个互斥锁 SIO_PARAM_SERIAL_DEF serialport[MAX_SERIAL_PORT_NUM]; @@ -1897,13 +1900,13 @@ void CameraPhotoPortDataProcess(SIO_PARAM_SERIAL_DEF *curserial) } else { - memset(&serialport[0].image, 0, sizeof(PHOTO_DEF)); - memmove((void *)&serialport[0].image, (void*)&curserial->image, sizeof(PHOTO_DEF)); if ((1 == SaveImageDataTofile(curserial)) && (SER_SAMPLE == curserial->image.state)) { curserial->image.state = PHOTO_SAVE_SUCC; } recvend = 1; + memset(&serialport[0].image, 0, sizeof(PHOTO_DEF)); + memmove((void *)&serialport[0].image, (void*)&curserial->image, sizeof(PHOTO_DEF)); } } else @@ -3349,11 +3352,15 @@ void Collect_sensor_data() } #endif #if 1 + pthread_mutex_lock(&serial_mutex); // 加锁 static int ideletefile = 0; + char logbuf[64]; time_t now; struct tm t0; const char *path = "/sdcard/log"; // 指定目录路径 int days = 15; // 删除15天前的log文件 + sprintf(logbuf, "进入程序时间:%lld, ideletefile=%d", get_msec(), ideletefile); + DebugLog(8, logbuf, 'I'); now = time(NULL); localtime_r(&now, &t0); @@ -3366,6 +3373,7 @@ void Collect_sensor_data() ideletefile = 0; #endif GM_StartSerialComm(); + pthread_mutex_unlock(&serial_mutex); // 解锁 } int Gm_SetCameraSerialPortParam(int fd, unsigned int baud) @@ -3400,7 +3408,7 @@ int Gm_OpenCameraSerial(SIO_PARAM_SERIAL_DEF *pPortParam, char *serfile, unsigne DebugLog(0, szbuf, 'E'); return -1; } - sprintf(szbuf, "摄像机打开串口%s成功!fd=%d", serfile, fd); + sprintf(szbuf, "摄像机打开串口%s成功!fd=%d 波特率:%d", serfile, fd, baud); DebugLog(0, szbuf, 'I'); pPortParam->fd = fd; serbaud = getBaudrate(baud); @@ -3568,6 +3576,11 @@ int GM_IsCloseCamera(SIO_PARAM_SERIAL_DEF *pPortParam) pPortParam->image.state = SAMPLINGSUCCESS; strcpy(buf, "通道1摄像机使用完毕!可以关闭摄像机电源!"); DebugLog(0, buf, 'I'); + if(pPortParam->fd >= 0) + { + close(pPortParam->fd); + pPortParam->fd = -1; + } } return pPortParam->SerialCmdidx; } @@ -3590,7 +3603,7 @@ void GM_CameraSerialComRecv(SIO_PARAM_SERIAL_DEF *pPortParam) i += recvlen; recvlen = read(pPortParam->fd, &recvbuf[i], sizeof(recvbuf)-i);/* 在串口读取字符串 */ t1 = get_msec(); - if(t1-t0 >= 500) + if(t1-t0 >= 350) break; } @@ -3666,7 +3679,14 @@ int GM_StartSerialCameraPhoto(int phototime, unsigned char channel, int cmdidx, #endif if(-1 == flag) + { + if(NULL != cameraport) + { + free(cameraport); + cameraport = NULL; + } return -1; + } if (0x00 == flag) { @@ -3686,18 +3706,25 @@ int GM_StartSerialCameraPhoto(int phototime, unsigned char channel, int cmdidx, } } } + if(NULL != cameraport) + { + free(cameraport); + cameraport = NULL; + } return 0; } #endif int CameraPhotoCmd(int phototime, unsigned char channel, int cmdidx, unsigned char bImageSize, unsigned char presetno, char *serfile, unsigned int baud, int addr) { + pthread_mutex_lock(&camera_mutex); int flag = 0; srdt.bImageSize = bImageSize; srdt.presetno = presetno; srdt.sendphototime = phototime; flag = GM_StartSerialCameraPhoto(phototime, channel, cmdidx, bImageSize, presetno, serfile, baud, addr); + pthread_mutex_unlock(&camera_mutex); // 解锁 return flag; } /* 串口启动接口函数 结束*/