diff --git a/TestComm/app/src/main/cpp/WeatherComm.cpp b/TestComm/app/src/main/cpp/WeatherComm.cpp index a27f29be..9a2a648a 100644 --- a/TestComm/app/src/main/cpp/WeatherComm.cpp +++ b/TestComm/app/src/main/cpp/WeatherComm.cpp @@ -16,6 +16,7 @@ #include #include "GPIOControl.h" #include "WeatherComm.h" +#include SIO_PARAM_SERIAL_DEF serialport; float weatherpntmsg[10]; @@ -365,12 +366,22 @@ static void RecvData(u_char *buf, int len)// 规约读数据处理 } } +static long get_msec(void ) +{ + struct timeval tv; + + gettimeofday(&tv, NULL); + long time_in_msec = tv.tv_sec * 1000 + tv.tv_usec/1000; + + return time_in_msec; +} //int inum =0; //int itimecnt=0; static int weather_comm(SERIAL_PARAM weatherport) { int fd = -1; - int len, i,ret, ictime, iruntime, isendtime, irecvtime, icnt=0; + int len, i,ret, icnt=0; + long ictime, iruntime, isendtime, irecvtime; unsigned char sendbuf[] = {0x68,0x00,0x00,0x68,0x01,0x09,0x0a,0x16}; char recvbuf[256], szbuf[512]; //char serial_description[] = "/dev/ttyS0"; @@ -402,8 +413,9 @@ static int weather_comm(SERIAL_PARAM weatherport) set12VEnable(true); setCam3V3Enable(true); setRS485Enable(true); - - ictime = (int)time(NULL); + sleep(2); + //ictime = (int)time(NULL); + ictime = get_msec(); for(;;) { if(fd < 0) @@ -426,13 +438,14 @@ static int weather_comm(SERIAL_PARAM weatherport) } usleep(10000); - iruntime = (int)time(NULL); - if((iruntime - ictime > 120) || (iruntime - ictime < 0)) + //iruntime = (int)time(NULL); + iruntime = get_msec(); + if((iruntime - ictime > 120000) || (iruntime - ictime < 0)) ictime = iruntime; - if(iruntime - ictime > 19) + if(iruntime - ictime > 20000) { memset(szbuf, 0, sizeof(szbuf)); - sprintf(szbuf, "气象采样时间=%d,停止采样!", iruntime-ictime); + sprintf(szbuf, "气象采样时间=%0.3f秒,停止采样!", (iruntime-ictime)/1000.0); LOGE("%s", szbuf); break; } @@ -443,7 +456,9 @@ static int weather_comm(SERIAL_PARAM weatherport) len = write(fd, sendbuf, sizeof(sendbuf));/* 向串囗发送字符串 */ serialport.RevCmdFlag = 0; - isendtime = time(NULL); + LOGE("发送命令时间差%ld毫秒", get_msec()-isendtime); + //isendtime = time(NULL); + isendtime = get_msec(); if (len < 0) { LOGE("write data error \n"); return -1; @@ -462,12 +477,13 @@ static int weather_comm(SERIAL_PARAM weatherport) } else { - irecvtime = time(NULL); - if((irecvtime-isendtime > 6) ||(irecvtime - isendtime < 0)) + //irecvtime = time(NULL); + irecvtime = get_msec(); + if((irecvtime-isendtime > 6000) ||(irecvtime - isendtime < 0)) isendtime = irecvtime; - if (irecvtime-isendtime > 1) + if (irecvtime-isendtime > 300) { - LOGE("传感器超过%d秒未应答", irecvtime-isendtime); + LOGE("传感器超过%ld毫秒未应答", irecvtime-isendtime); serialport.RevCmdFlag = 1; serialport.m_iRecvLen = 0; serialport.m_iRevStatus = 0; @@ -516,6 +532,14 @@ static int weather_comm(SERIAL_PARAM weatherport) int serial_port_comm() { SERIAL_PARAM portparm; + + //struct timeval tv; + + //gettimeofday(&tv, NULL); + //long time_in_microseconds = tv.tv_sec * 1000000 + tv.tv_usec; + + //LOGE("Current time in microseconds: %ld\n", time_in_microseconds); + #if 1 memset(portparm.pathname, 0, sizeof(portparm.pathname)); sprintf(portparm.pathname, "/dev/ttyS0"); diff --git a/app/src/main/cpp/WeatherComm.cpp b/app/src/main/cpp/WeatherComm.cpp index 20304c40..9a2a648a 100644 --- a/app/src/main/cpp/WeatherComm.cpp +++ b/app/src/main/cpp/WeatherComm.cpp @@ -16,6 +16,7 @@ #include #include "GPIOControl.h" #include "WeatherComm.h" +#include SIO_PARAM_SERIAL_DEF serialport; float weatherpntmsg[10]; @@ -365,12 +366,22 @@ static void RecvData(u_char *buf, int len)// 规约读数据处理 } } +static long get_msec(void ) +{ + struct timeval tv; + + gettimeofday(&tv, NULL); + long time_in_msec = tv.tv_sec * 1000 + tv.tv_usec/1000; + + return time_in_msec; +} //int inum =0; //int itimecnt=0; static int weather_comm(SERIAL_PARAM weatherport) { int fd = -1; - int len, i,ret, ictime, iruntime, isendtime, irecvtime, icnt=0; + int len, i,ret, icnt=0; + long ictime, iruntime, isendtime, irecvtime; unsigned char sendbuf[] = {0x68,0x00,0x00,0x68,0x01,0x09,0x0a,0x16}; char recvbuf[256], szbuf[512]; //char serial_description[] = "/dev/ttyS0"; @@ -402,8 +413,9 @@ static int weather_comm(SERIAL_PARAM weatherport) set12VEnable(true); setCam3V3Enable(true); setRS485Enable(true); - - ictime = (int)time(NULL); + sleep(2); + //ictime = (int)time(NULL); + ictime = get_msec(); for(;;) { if(fd < 0) @@ -426,13 +438,14 @@ static int weather_comm(SERIAL_PARAM weatherport) } usleep(10000); - iruntime = (int)time(NULL); - if((iruntime - ictime > 120) || (iruntime - ictime < 0)) + //iruntime = (int)time(NULL); + iruntime = get_msec(); + if((iruntime - ictime > 120000) || (iruntime - ictime < 0)) ictime = iruntime; - if(iruntime - ictime > 19) + if(iruntime - ictime > 20000) { memset(szbuf, 0, sizeof(szbuf)); - sprintf(szbuf, "气象采样时间=%d,停止采样!", iruntime-ictime); + sprintf(szbuf, "气象采样时间=%0.3f秒,停止采样!", (iruntime-ictime)/1000.0); LOGE("%s", szbuf); break; } @@ -443,7 +456,9 @@ static int weather_comm(SERIAL_PARAM weatherport) len = write(fd, sendbuf, sizeof(sendbuf));/* 向串囗发送字符串 */ serialport.RevCmdFlag = 0; - isendtime = time(NULL); + LOGE("发送命令时间差%ld毫秒", get_msec()-isendtime); + //isendtime = time(NULL); + isendtime = get_msec(); if (len < 0) { LOGE("write data error \n"); return -1; @@ -462,12 +477,13 @@ static int weather_comm(SERIAL_PARAM weatherport) } else { - irecvtime = time(NULL); - if((irecvtime-isendtime > 6) ||(irecvtime - isendtime < 0)) + //irecvtime = time(NULL); + irecvtime = get_msec(); + if((irecvtime-isendtime > 6000) ||(irecvtime - isendtime < 0)) isendtime = irecvtime; - if (irecvtime-isendtime > 1) + if (irecvtime-isendtime > 300) { - LOGE("传感器超过%d秒未应答", irecvtime-isendtime); + LOGE("传感器超过%ld毫秒未应答", irecvtime-isendtime); serialport.RevCmdFlag = 1; serialport.m_iRecvLen = 0; serialport.m_iRevStatus = 0; @@ -513,10 +529,17 @@ static int weather_comm(SERIAL_PARAM weatherport) return(0); } -/* 测试代码使用*/ int serial_port_comm() { SERIAL_PARAM portparm; + + //struct timeval tv; + + //gettimeofday(&tv, NULL); + //long time_in_microseconds = tv.tv_sec * 1000000 + tv.tv_usec; + + //LOGE("Current time in microseconds: %ld\n", time_in_microseconds); + #if 1 memset(portparm.pathname, 0, sizeof(portparm.pathname)); sprintf(portparm.pathname, "/dev/ttyS0"); @@ -528,7 +551,7 @@ int serial_port_comm() #endif //itimecnt = (int)time(NULL); - for(;;) + //for(;;) weather_comm(portparm); return 0; }