?? 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 <termios.h>//#include <stdio.h>//#include <unistd.h>//#include <fcntl.h>//#include <sys/signal.h>//#include <pthread.h>//#include "tty.h"#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <termios.h> #include <stdio.h> #define BAUDRATE B115200#define COM1 "/dev/ttyS0"#define COM2 "/dev/ttyS2"static int fd;static struct termios oldtio,newtio;//==============================================================int tty_end(){ // tcsetattr(fd,TCSANOW,&oldtio); /* restore old modem setings */// tcsetattr(0,TCSANOW,&oldstdtio); /* restore old tty setings */ printf("fd=%d\n",fd); close(fd); printf("fd=%d has been close\n",fd);}//==============================================================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_init(){ printf("start to open com1\n"); fd = open(COM1, O_RDWR ); //| O_NONBLOCK);// printf("fd=%d\n",fd); if (fd <0) { perror(COM1); exit(-1); } printf("finish opening com1 \n"); tcgetattr(fd,&newtio); bzero(&newtio,sizeof(newtio)); //無奇偶校驗8位 newtio.c_cflag &= ~CSIZE; newtio.c_cflag |= CS8; newtio.c_cflag &= ~PARENB; /* Clear parity enable */ newtio.c_iflag &= ~INPCK; /* Enable parity checking */ newtio.c_cflag &= ~CSTOPB; //停止位1位 newtio.c_cc[VTIME] = 0; newtio.c_cc[VMIN] = 0; newtio.c_cflag |= (CLOCAL|CREAD); newtio.c_oflag |= OPOST; newtio.c_iflag &= ~(IXON|IXOFF|IXANY); cfsetispeed(&newtio,BAUDRATE); cfsetospeed(&newtio,BAUDRATE); tcflush(fd, TCIFLUSH); if (tcsetattr(fd,TCSANOW,&newtio) != 0) { perror("SetupSerial 1"); return -1; } return 0;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -