📄 systime.c
字号:
#include <stdio.h> #include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <fcntl.h>#include <time.h>#include <linux/sem.h>#include <errno.h>#include <signal.h>#include <termios.h> #define SIGNFILE "/wml/sign_file/handle_record.txt" #define OSD_COL_BLK 0#define OSD_COL_50WHT 1#define OSD_COL_75WHT 2#define OSD_COL_25WHT 3#define OSD_COL_100WHT 4#define OSD_COL_BLU 5#define OSD_COL_RED 6#define OSD_COL_ORNG 7#define OSD_COL_CYN 8#define OSD_COL_MGN 9#define OSD_COL_GRN 10#define OSD_COL_OCN 11#define OSD_COL_YLW 12#define OSD_BLINK 0x80 //闪烁#define OSD_UNBLINK 0x00 //正常显示//add by zhangfeng#define white_fg 0x01#define red_fg 0x04#define green_fg 0x08#define blue_fg 0x20#define black_fg 0x12#define blink 0x80 //闪烁#define unblink 0x00 //正常显示#define SEMKEY 100#define TIMEOUT_SEC(buflen,baud) (buflen*20/baud+2)#define TIMEOUT_USEC 0int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600, B4800, B2400, B1200, B300, };int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 38400, 19200, 9600, 4800, 2400, 1200, 300, };static int ttys_fd; static fd_set fs_read, fs_write;static struct timeval tv_timeout;static unsigned char buffer[10];static char *dev = "/dev/ttyS1"; //串口三static unsigned char hour,minute,second;static int osd_char = 1,osd_time = 1;void set_speed(int fd, int speed){ int i; int status; struct termios Opt; tcgetattr(fd, &Opt); for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) { if (speed == name_arr[i]) { tcflush(fd, TCIOFLUSH); cfsetispeed(&Opt, speed_arr[i]); cfsetospeed(&Opt, speed_arr[i]); status = tcsetattr(fd, TCSANOW, &Opt); if (status != 0) { perror("tcsetattr fd1"); return; } tcflush(fd,TCIOFLUSH); } }}int set_Parity(int fd,int databits,int stopbits,int parity){ struct termios options; if ( tcgetattr( fd,&options) != 0) { perror("SetupSerial 1"); return -1; } options.c_cflag &= ~CSIZE; switch (databits) /*设置数据位数*/ { case 7: options.c_cflag |= CS7; break; case 8: options.c_cflag |= CS8; break; default: fprintf(stderr,"Unsupported data size\n"); return -1; }switch (parity) { case 'n': case 'N': options.c_cflag &= ~PARENB; /* Clear parity enable */ options.c_iflag &= ~INPCK; /* Enable parity checking */ break; case 'o': case 'O': options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/ options.c_iflag |= INPCK; /* Disnable parity checking */ break; case 'e': case 'E': options.c_cflag |= PARENB; /* Enable parity */ options.c_cflag &= ~PARODD; /* 转换为偶效验*/ options.c_iflag |= INPCK; /* Disnable parity checking */ break; case 'S': case 's': /*as no parity*/ options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB;break; default: fprintf(stderr,"Unsupported parity\n"); return -1; } /* 设置停止位*/ switch (stopbits) { case 1: options.c_cflag &= ~CSTOPB; break; case 2: options.c_cflag |= CSTOPB; break; default: fprintf(stderr,"Unsupported stop bits\n"); return -1; } /* Set input parity option */ if (parity != 'n') options.c_iflag |= INPCK; tcflush(fd,TCIFLUSH); options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/ options.c_cc[VMIN] = 0; /* Update the options and do it NOW */ options.c_lflag &= ~(ISIG|ICANON); if (tcsetattr(fd,TCSANOW,&options) != 0) { perror("SetupSerial 3"); return -1; } return 0; }int OpenDev(char * Dev){ ttys_fd= open (Dev, O_RDWR | O_NOCTTY | O_NONBLOCK); // fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY if (ttys_fd== -1) { perror("Can't Open Serial Port"); return -1; } return ttys_fd;}int WriteComPort (char * data, int datalength){ int retval, len = 0, total_len = 0, i; FD_ZERO (&fs_write); FD_SET (ttys_fd, &fs_write); tv_timeout.tv_sec = TIMEOUT_SEC (datalength, 38400); tv_timeout.tv_usec = 0; for (total_len = 0, len = 0; total_len < datalength;) { retval = select (ttys_fd + 1, NULL, &fs_write, NULL, &tv_timeout); if (retval) { if(datalength - total_len> 8) len = write (ttys_fd, &data[total_len], 8); else len = write (ttys_fd, &data[total_len], datalength - total_len); #if 0 printf("time send data length:%d\n", len); for(i=total_len; i<total_len + len ; i++) printf("%02x ", data[i]); printf("\n"); #endif if (len > 0) { total_len += len; } } else { tcflush (ttys_fd, TCOFLUSH); /* flush all output data */ break; } } return (total_len);}void ReadOSDSetFile(){ FILE *file; char osdset[5]; file = fopen("/wml/osd_time_set.txt", "r"); if(!file) { printf("open osd_time_set.txt fail ! \r\n"); } memset(osdset, 0, 5); if (fgets(osdset, sizeof(osdset), file) == NULL) { fclose(file); } osd_char = osdset[0] -48; osd_time = osdset[1] -48; fclose(file); }int checksum(char * strbuf ,int len){ int i, temp = 0; for(i=0; i<len; i++) temp += strbuf[i]; return temp;}void SysTimeOSD() //add by zhangfeng{ int temp; unsigned char strbuf[10]; int hour, minute, second; time_t t; struct tm *tm_t; int semid; union semun sunionr; struct sembuf askfor_res, free_res; if(osd_time == 0) return ;/* initialization semahphore. */ semid=semget(SEMKEY, 1, IPC_CREAT); if(semid==-1){ printf("Semaphore request failed: %s. \n", strerror(errno)); return ; } askfor_res.sem_num = 0; askfor_res.sem_op = -1; askfor_res.sem_flg = SEM_UNDO; free_res.sem_num = 0; free_res.sem_op = 1; free_res.sem_flg = SEM_UNDO; if(semop(semid, &askfor_res, 1)== -1) //ask for resource perror("semop error "); if(OpenDev(dev) == -1) return ; set_speed(ttys_fd, 38400); if (set_Parity(ttys_fd,8,1,'N') == -1) { printf("Set Parity Error\n"); exit (0); } memset(strbuf, 0, 10); time(&t); tm_t=localtime(&t); hour = tm_t->tm_hour; minute = tm_t->tm_min; second = tm_t->tm_sec; strbuf[0] = 0xA5; strbuf[1] = 0x05; strbuf[2] = 7; strbuf[3] = 34; strbuf[4] = 27; strbuf[5] = (white_fg<<1)|OSD_UNBLINK+ 0x01; strbuf[6] = hour; strbuf[7] = minute; strbuf[8] = second; temp = checksum( strbuf,strbuf[2] + 2); strbuf[9]= temp; if(WriteComPort(strbuf, strbuf[2] + 3) < 0) { printf("write failed\n"); } usleep(5000); close(ttys_fd); //usleep(500000); if(semop(semid, &free_res, 1) == -1) //free the resource if(errno == EIDRM) printf("the semaphore set was removed \n"); if(hour==0&&minute==0&&second<10) //更新日期 { sleep(2); system("/wml/gpio_osd 8 &"); } sleep(600); return ;}//killall -10 systimevoid sigusr1_handle(int sig){ unsigned char timebuf[10]; FILE *fb_sign = NULL; ReadOSDSetFile(); if(osd_time == 0) { if(OpenDev(dev) == -1) return ; set_speed(ttys_fd, 38400); if (set_Parity(ttys_fd,8,1,'N') == -1) { printf("Set Parity Error\n"); exit (0); } memset(timebuf, 0, 10); //清除系统时间 timebuf[0] = 0xA5; timebuf[1] = 0x11; timebuf[2] = 0x03; timebuf[3] = 23; timebuf[4] = 27; timebuf[5] = 0xEB; if(WriteComPort(timebuf, timebuf[2] + 3) < 0) { printf("write failed\n"); } close(ttys_fd); } else { system("/wml/gpio_osd 8 "); } if(osd_char == 0) { system("/wml/gpio_osd 7 &"); system("/wml/gpio_osd 10 &"); } else { fb_sign = fopen(SIGNFILE, "r"); if(fb_sign != NULL) { fclose(fb_sign); fb_sign = NULL; system("/wml/gpio_osd 6 &"); } } }//killall -12 systimevoid sigusr2_handle(int sig){ system("/wml/gpio_osd 8 ");}int main(){ unsigned int temp; struct sigaction act_usr1; struct sigaction act_usr2; act_usr1.sa_handler=sigusr1_handle; act_usr1.sa_flags=SA_NOMASK; sigemptyset(&act_usr1.sa_mask); sigaction(SIGUSR1, &act_usr1, NULL);//10 act_usr2.sa_handler=sigusr2_handle; act_usr2.sa_flags=SA_NOMASK; sigemptyset(&act_usr2.sa_mask); sigaction(SIGUSR2, &act_usr2, NULL);//12 ReadOSDSetFile(); while(1) { SysTimeOSD(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -