📄 tty.c
字号:
/************************************************ * TTY SERIAL ROUTES * use ttyS1 to ctrol GPRS * by Zou jian guo <ah_zou@163.com> * 2004-11-02 * * edited by wbin <wbinbuaa@163.com> * 2005-01-19 * *************************************************/#include <sys/time.h>#include <time.h>#include <errno.h>#include <stdio.h>#include <signal.h>#include <fcntl.h>#include <termio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <sys/types.h> #include <sys/stat.h> #define BAUDRATE B115200//#define COM1 "/dev/ttse/1"#define COM2 "/dev/ttse/0"static int fd;static struct termio term, tstdin,term_save, stdin_save;;//==============================================================int tty_end(){ ioctl(fd, TCSETA, &term_save); close(fd); ioctl(fileno(stdin), TCSETA, &stdin_save);}//==============================================================/*static void do_exit(){ tty_end(); exit(1);}*///==============================================================int tty_read(char *buf,int nbytes){ return read(fd,buf,nbytes);}//==============================================================int tty_write(char *buf,int nbytes){ int i; for(i=0; i<nbytes; i++) { write(fd,&buf[i],1); usleep(100); } return tcdrain(fd);}//==============================================================int tty_writecmd(char *buf,int nbytes){ int i; for(i=0; i<nbytes; i++) { write(fd,&buf[i],1); usleep(100); } write(fd,"\r",1); usleep(300000); return tcdrain(fd);}//==============================================================/*int tty_writebyte(char *buf){ write(fd,&buf[0],1); usleep(10);// write(fd,buf,nbytes); return tcdrain(fd);}*///==============================================================extern int baud;int tty_init(){// fd = open(COM2, O_RDWR ); //| O_NONBLOCK);// fd = open(COM2, O_RDWR | O_NDELAY); if (fd <0) { perror(COM2); exit(-1); } ioctl(fd, TCGETA, &term_save); ioctl(fileno(stdin), TCGETA, &stdin_save);// signal(SIGHUP, Exit);// signal(SIGINT, Exit);// signal(SIGQUIT, Exit);// signal(SIGTERM, Exit); /* Перевести stdin в линейный режим, выключить эхо */ ioctl(fileno(stdin), TCGETA, &tstdin); tstdin.c_iflag = 0; tstdin.c_lflag &= ~(ICANON | ECHO); tstdin.c_cc[VMIN] = 0; tstdin.c_cc[VTIME] = 0; ioctl(fileno(stdin), TCSETA, &tstdin); /* Задать состояние tty */ ioctl(fd, TCGETA, &term); term.c_cflag |= CLOCAL|HUPCL;// if (baud > 0)// { term.c_cflag &= ~CBAUD; term.c_cflag |= baud;// } term.c_lflag &= ~(ICANON | ECHO); /* линейный режим */ term.c_iflag &= ~ICRNL; /* исключить пустые строки */ term.c_cc[VMIN] = 0; term.c_cc[VTIME] = 10; ioctl(fd, TCSETA, &term); fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NDELAY); /* Открыть tty для чтения и записи */ return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -