📄 main.c
字号:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <signal.h>#include <sys/time.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <termios.h>#include "protocol.h"#include "device.h"static char g_protocol[MAX_PROTOCOL_SIZE];static int g_address = 0x01;static struct TDVSSS_device g_device;//1. =========================================================================static void usage(int argc,char** argv){ int i; fprintf(stderr,"\nUsage:%s [-d device] [-b baudrate] [-p protocol]\ [-a address] [-h help]\n",argv[0]); fprintf(stderr,"\tdevice = /dev/ttySn\n"); fprintf(stderr,"\tprotocol = "); fprintf(stderr," %s",protocol_entry[0].protocol); for(i = 1; protocol_entry[i].protocol;i++) fprintf(stderr," | %s",protocol_entry[i].protocol); fprintf(stderr,"\n"); return ;}//2.==========================================================================/******************************************************************** Function: void parseCmdLine(int argc,char** argv) Description: 命令行解析函数 Arguments: int argc 参数个数 char** argv 指向参数的指针 Return: none Others:********************************************************************/static int parseCmdLine(int argc,char** argv){ int i; if(argc == 1) return 0; for(i=1; i<argc;i++) { if(argv[i][0] == '-') { switch (argv[i][1]) { case 'p': if((argv[i+1] == NULL)) { usage(argc,argv); exit(1); } strcpy(g_protocol,argv[i+ 1]); break; case 'a': if(argv[i+1] == NULL) { usage(argc,argv); exit(1); } g_address = atoi(argv[i+1]); break; case 'd': if(argv[i+1] == NULL) { usage(argc,argv); exit(1); } if(!strncmp(argv[i+1],"/dev/ttyS",9)) { g_device.type = DEVICE_TTYS; strcpy(g_device.name,argv[i+1]); } else { usage(argc,argv); exit(1); } break; case 'b': if(argv[i+1] == NULL) { usage(argc,argv); exit(1); } g_device.baudrate = atoi(argv[i+1]); break; case 'h': usage(argc,argv); exit(1); default: fprintf(stderr,"error: Invalid Command Line!\n"); usage(argc,argv); exit(1); break; } } } return 0;}//3.*************************************************************int main(int argc,char** argv){ struct TDVSSS_protocol* tp; #ifdef FOR_HTTPD char input[MAX_USER_INPUT_SIZE]; #endif //initial data memset(g_protocol,0,MAX_PROTOCOL_SIZE); memset(&g_device,0,sizeof(g_device)); strcpy(g_protocol,"standard-i2c"); g_address = 0x01; g_device.type = DEVICE_TTYS; strcpy(g_device.name,"/dev/ttyS0"); g_device.baudrate = 9600; //parse command line parseCmdLine(argc,argv); //initial protocol if((tp = protocol_init(g_protocol,g_address,&g_device)) == NULL) { fprintf(stderr,"error:initial protocol failed!\n"); exit(1); } //loop to receive command then handle the command #ifdef FOR_HTTPD fprintf(stderr,"for httpd! protocol: %s\n",tp->protocol); memset(input,0,MAX_USER_INPUT_SIZE); fgets(input,MAX_USER_INPUT_SIZE,stdin); while(strcmp(input,"q\n")) { handle_control(input); memset(input,0,MAX_USER_INPUT_SIZE); fgets(input,MAX_USER_INPUT_SIZE,stdin); } #else fprintf(stderr,"single control! protocol: %s\n",tp->protocol); command_loop(tp); fprintf(stderr,"quit the process\n"); #endif //quit the process close(tp->fd); free(tp); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -