将控制串口收发的流程的时间秒,改用毫秒

serial
huyizhong 1 year ago
parent 549cda6b7b
commit 811d9ea593

@ -16,6 +16,7 @@
#include <time.h> #include <time.h>
#include "GPIOControl.h" #include "GPIOControl.h"
#include "WeatherComm.h" #include "WeatherComm.h"
#include <sys/time.h>
SIO_PARAM_SERIAL_DEF serialport; SIO_PARAM_SERIAL_DEF serialport;
float weatherpntmsg[10]; 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 inum =0;
//int itimecnt=0; //int itimecnt=0;
static int weather_comm(SERIAL_PARAM weatherport) static int weather_comm(SERIAL_PARAM weatherport)
{ {
int fd = -1; 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}; unsigned char sendbuf[] = {0x68,0x00,0x00,0x68,0x01,0x09,0x0a,0x16};
char recvbuf[256], szbuf[512]; char recvbuf[256], szbuf[512];
//char serial_description[] = "/dev/ttyS0"; //char serial_description[] = "/dev/ttyS0";
@ -402,8 +413,9 @@ static int weather_comm(SERIAL_PARAM weatherport)
set12VEnable(true); set12VEnable(true);
setCam3V3Enable(true); setCam3V3Enable(true);
setRS485Enable(true); setRS485Enable(true);
sleep(2);
ictime = (int)time(NULL); //ictime = (int)time(NULL);
ictime = get_msec();
for(;;) for(;;)
{ {
if(fd < 0) if(fd < 0)
@ -426,13 +438,14 @@ static int weather_comm(SERIAL_PARAM weatherport)
} }
usleep(10000); usleep(10000);
iruntime = (int)time(NULL); //iruntime = (int)time(NULL);
if((iruntime - ictime > 120) || (iruntime - ictime < 0)) iruntime = get_msec();
if((iruntime - ictime > 120000) || (iruntime - ictime < 0))
ictime = iruntime; ictime = iruntime;
if(iruntime - ictime > 19) if(iruntime - ictime > 20000)
{ {
memset(szbuf, 0, sizeof(szbuf)); memset(szbuf, 0, sizeof(szbuf));
sprintf(szbuf, "气象采样时间=%d,停止采样!", iruntime-ictime); sprintf(szbuf, "气象采样时间=%0.3f秒,停止采样!", (iruntime-ictime)/1000.0);
LOGE("%s", szbuf); LOGE("%s", szbuf);
break; break;
} }
@ -443,7 +456,9 @@ static int weather_comm(SERIAL_PARAM weatherport)
len = write(fd, sendbuf, sizeof(sendbuf));/* 向串囗发送字符串 */ len = write(fd, sendbuf, sizeof(sendbuf));/* 向串囗发送字符串 */
serialport.RevCmdFlag = 0; serialport.RevCmdFlag = 0;
isendtime = time(NULL); LOGE("发送命令时间差%ld毫秒", get_msec()-isendtime);
//isendtime = time(NULL);
isendtime = get_msec();
if (len < 0) { if (len < 0) {
LOGE("write data error \n"); LOGE("write data error \n");
return -1; return -1;
@ -462,12 +477,13 @@ static int weather_comm(SERIAL_PARAM weatherport)
} }
else else
{ {
irecvtime = time(NULL); //irecvtime = time(NULL);
if((irecvtime-isendtime > 6) ||(irecvtime - isendtime < 0)) irecvtime = get_msec();
if((irecvtime-isendtime > 6000) ||(irecvtime - isendtime < 0))
isendtime = irecvtime; isendtime = irecvtime;
if (irecvtime-isendtime > 1) if (irecvtime-isendtime > 300)
{ {
LOGE("传感器超过%d秒未应答", irecvtime-isendtime); LOGE("传感器超过%ld秒未应答", irecvtime-isendtime);
serialport.RevCmdFlag = 1; serialport.RevCmdFlag = 1;
serialport.m_iRecvLen = 0; serialport.m_iRecvLen = 0;
serialport.m_iRevStatus = 0; serialport.m_iRevStatus = 0;
@ -516,6 +532,14 @@ static int weather_comm(SERIAL_PARAM weatherport)
int serial_port_comm() int serial_port_comm()
{ {
SERIAL_PARAM portparm; 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 #if 1
memset(portparm.pathname, 0, sizeof(portparm.pathname)); memset(portparm.pathname, 0, sizeof(portparm.pathname));
sprintf(portparm.pathname, "/dev/ttyS0"); sprintf(portparm.pathname, "/dev/ttyS0");

@ -16,6 +16,7 @@
#include <time.h> #include <time.h>
#include "GPIOControl.h" #include "GPIOControl.h"
#include "WeatherComm.h" #include "WeatherComm.h"
#include <sys/time.h>
SIO_PARAM_SERIAL_DEF serialport; SIO_PARAM_SERIAL_DEF serialport;
float weatherpntmsg[10]; 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 inum =0;
//int itimecnt=0; //int itimecnt=0;
static int weather_comm(SERIAL_PARAM weatherport) static int weather_comm(SERIAL_PARAM weatherport)
{ {
int fd = -1; 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}; unsigned char sendbuf[] = {0x68,0x00,0x00,0x68,0x01,0x09,0x0a,0x16};
char recvbuf[256], szbuf[512]; char recvbuf[256], szbuf[512];
//char serial_description[] = "/dev/ttyS0"; //char serial_description[] = "/dev/ttyS0";
@ -402,8 +413,9 @@ static int weather_comm(SERIAL_PARAM weatherport)
set12VEnable(true); set12VEnable(true);
setCam3V3Enable(true); setCam3V3Enable(true);
setRS485Enable(true); setRS485Enable(true);
sleep(2);
ictime = (int)time(NULL); //ictime = (int)time(NULL);
ictime = get_msec();
for(;;) for(;;)
{ {
if(fd < 0) if(fd < 0)
@ -426,13 +438,14 @@ static int weather_comm(SERIAL_PARAM weatherport)
} }
usleep(10000); usleep(10000);
iruntime = (int)time(NULL); //iruntime = (int)time(NULL);
if((iruntime - ictime > 120) || (iruntime - ictime < 0)) iruntime = get_msec();
if((iruntime - ictime > 120000) || (iruntime - ictime < 0))
ictime = iruntime; ictime = iruntime;
if(iruntime - ictime > 19) if(iruntime - ictime > 20000)
{ {
memset(szbuf, 0, sizeof(szbuf)); memset(szbuf, 0, sizeof(szbuf));
sprintf(szbuf, "气象采样时间=%d,停止采样!", iruntime-ictime); sprintf(szbuf, "气象采样时间=%0.3f秒,停止采样!", (iruntime-ictime)/1000.0);
LOGE("%s", szbuf); LOGE("%s", szbuf);
break; break;
} }
@ -443,7 +456,9 @@ static int weather_comm(SERIAL_PARAM weatherport)
len = write(fd, sendbuf, sizeof(sendbuf));/* 向串囗发送字符串 */ len = write(fd, sendbuf, sizeof(sendbuf));/* 向串囗发送字符串 */
serialport.RevCmdFlag = 0; serialport.RevCmdFlag = 0;
isendtime = time(NULL); LOGE("发送命令时间差%ld毫秒", get_msec()-isendtime);
//isendtime = time(NULL);
isendtime = get_msec();
if (len < 0) { if (len < 0) {
LOGE("write data error \n"); LOGE("write data error \n");
return -1; return -1;
@ -462,12 +477,13 @@ static int weather_comm(SERIAL_PARAM weatherport)
} }
else else
{ {
irecvtime = time(NULL); //irecvtime = time(NULL);
if((irecvtime-isendtime > 6) ||(irecvtime - isendtime < 0)) irecvtime = get_msec();
if((irecvtime-isendtime > 6000) ||(irecvtime - isendtime < 0))
isendtime = irecvtime; isendtime = irecvtime;
if (irecvtime-isendtime > 1) if (irecvtime-isendtime > 300)
{ {
LOGE("传感器超过%d秒未应答", irecvtime-isendtime); LOGE("传感器超过%ld秒未应答", irecvtime-isendtime);
serialport.RevCmdFlag = 1; serialport.RevCmdFlag = 1;
serialport.m_iRecvLen = 0; serialport.m_iRecvLen = 0;
serialport.m_iRevStatus = 0; serialport.m_iRevStatus = 0;
@ -513,10 +529,17 @@ static int weather_comm(SERIAL_PARAM weatherport)
return(0); return(0);
} }
/* 测试代码使用*/
int serial_port_comm() int serial_port_comm()
{ {
SERIAL_PARAM portparm; 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 #if 1
memset(portparm.pathname, 0, sizeof(portparm.pathname)); memset(portparm.pathname, 0, sizeof(portparm.pathname));
sprintf(portparm.pathname, "/dev/ttyS0"); sprintf(portparm.pathname, "/dev/ttyS0");
@ -528,7 +551,7 @@ int serial_port_comm()
#endif #endif
//itimecnt = (int)time(NULL); //itimecnt = (int)time(NULL);
for(;;) //for(;;)
weather_comm(portparm); weather_comm(portparm);
return 0; return 0;
} }

Loading…
Cancel
Save