⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tty.c

📁 本程序中有main.c、rfid.h、rfid.c、tty.h、tty.c和makefile六个文件
💻 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 + -