📄 main.c.bak
字号:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <time.h>
#ifdef DEC
#include <sys/termio.h>
#else
#include <sys/termios.h>
#endif
#include <sys/termios.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
char device[20];
int FileHandle;
int year,mon,day,hour,min,sec,usec;
int timeout;
#define IS ==
#define ISNOT !=
/* #define DEVICE "tty1e" */
#define IFLAG 0
#define OFLAG 0
#define CFLAG B4800|CS8|CREAD|CLOCAL
#define LFLAG 0
void alarm_handler_1(int sig);
void exit_exe();
void closeSeri();
void openSeri();
void initSeri();
int reader();
int getDate();
void setDate();
#ifdef NCCVS
#include "version.h"
#endif
static char *version = "2.0.2";
/********************************************************************************
@ 2006\08\30 Ver2.0.0 initial the first cvs version infomation by icelee
@ 2006\09\12 Ver2.0.1 remove the Sleep(1) by icelee
@ 2006\09\12 Ver2.0.2 add communication's fault Alarm by Zhouxiyang
*********************************************************************************/
void exit_exe()
{
exit(-1);
}
void closeSeri()
{
close(FileHandle);
}
void openSeri()
{
extern int errno;
FileHandle=open(device,O_RDONLY);
if (FileHandle<0) {
fprintf(stderr, "open fail\ncode = %d\n",errno);
exit_exe();
}
}
void initSeri()
{
#ifdef DEC
struct termio TermBuf;
#else
struct termios TermBuf;
#endif
extern int errno;
#ifdef DEC
if (ioctl(FileHandle, TCGETA, &TermBuf) < 0) {
#else
if (tcgetattr(FileHandle, &TermBuf) < 0) {
#endif
close(FileHandle);
printf("get ioctl fail \n code = %d \n",errno);
exit_exe();
}
TermBuf.c_iflag = IFLAG;
TermBuf.c_oflag = OFLAG;
TermBuf.c_cflag = CFLAG;
TermBuf.c_lflag = LFLAG;
TermBuf.c_cc[4] = 1;
TermBuf.c_cc[5] = 0;
#ifdef DEC
if (ioctl(FileHandle, TCSETAF, &TermBuf) < 0) {
#else
if (tcsetattr(FileHandle, TCSANOW, &TermBuf) < 0) {
#endif
close(FileHandle);
printf("set ioctl fail \n code = %d \n",errno);
exit_exe();
}
}
void alarm_handler_1(int sig)
{
timeout = 1;
}
int reader(buf_ptr,nbyte)
unsigned char *buf_ptr;
unsigned nbyte;
{
int len;
timeout =0;
sigset(SIGALRM,alarm_handler_1);
alarm(2);
len = read(FileHandle,buf_ptr,nbyte);
alarm(0);
sigrelse(SIGALRM);
if (timeout) return(-1);
return(len);
}
int getDate(buf)
unsigned char buf[];
{
int i,len,length,ret;
unsigned char tmpchar,head[4]={'B','J','T',','};
/* read head from com */
len = 0; length = 0;
do {
len = reader(&tmpchar,1);
if (len < 0) return(0);
if (tmpchar == head[length]) length++;
else length = 0;
} while (length < 4);
i = 0;
do {
len = reader(&tmpchar,1);
if (len < 0) return(0);
if (tmpchar == ',') continue;
if (tmpchar<'0' && tmpchar>'9') return(0);
buf[i] = tmpchar;
printf("%c ",buf[i]);
i++;
} while (i<12);
printf("\n");
ret = sscanf((char *)buf,"%2d %2d %2d %2d %2d %2d",&year,&mon,&day,&hour,&min,&sec);
return (i);
}
void setDate(buf)
unsigned char buf[];
{
struct tm *ptr;
struct timeval tp;
struct timezone tzp;
time_t clock, clock1, diff;
if (hour < 0 || hour > 24 || min < 0 || min > 60 || sec < 0 || sec > 60)
return;
gettimeofday(&tp,&tzp);
clock=time((time_t *)NULL);
ptr = localtime(&clock);
/* ptr->tm_mday = day; */
ptr->tm_hour = hour;
ptr->tm_min = min;
ptr->tm_sec = sec;
clock1 = mktime(ptr);
diff = clock1 - clock;
if (diff < 300 && diff > -300) {
tp.tv_sec = (long)clock1;
tp.tv_usec = 0;
settimeofday(&tp,&tzp);
}
}
int main(int argc, char **argv)
{
int k,errcnt=0,alarmstat=0;
unsigned char buf[40];
#ifdef NCCVS
cvsinit(version,argc,argv);
#endif
if (argc < 2) {
printf("Usage: st_clock device\n");
exit(-1);
}
else sprintf(device,"/dev/%s",argv[1]);
printf("st_clock run\n");
openSeri();
if (FileHandle > 0)
initSeri();
while ( 1 ) {
if (getDate(buf) > 0) {
errcnt = 0;
if (alarmstat == 1) {
Write_Log("GPS时钟同步接收装置通讯恢复!", 2, 1, 1, 1, 1);
alarmstat = 0;
}
setDate(buf);
}
else
errcnt++;
if (errcnt > 10 && alarmstat == 0) {
Write_Log("GPS时钟同步接收装置通讯故障!", 2, 1, 1, 1, 1);
alarmstat = 1;
}
//sleep(1);
}
closeSeri();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -