有關linux串口的操作

有關串口阻塞非阻塞的問題

有兩個可以進行控制串口阻塞性(同時控制read和write):一個是在打開串口的時候,open函數是否帶O_NDELAY;第二個是可以在打開串口之後通過fcntl()函數進行控制:阻塞爲fcntl(fd,F_SETFL,0);非阻塞爲fcntl(fd,F_SETFL,FNDELAY);


static int set_opt(int fd, int nSpeed, int nBits, char nEvent, int nStop) 
{       
	struct termios newtio;       
	struct termios oldtio;              
	if(tcgetattr(fd,&oldtio) != 0){           
		perror("SetupSerial 1");           
		return -1;       
	}              
	bzero(&newtio,sizeof(newtio));       
	newtio.c_cflag |= CLOCAL |CREAD;       
	newtio.c_cflag &= ~CSIZE;   	

	/***********數據位選擇****************/        
	switch(nBits){          
		case 7:           
			newtio.c_cflag |= CS7;           
			break;           
		case 8:          
			newtio.c_cflag |= CS8;          
			break;           
	}   

	/***********校驗位選擇****************/      
	switch(nEvent){           
		case 'O':           
			newtio.c_cflag |= PARENB;           
			newtio.c_cflag |= PARODD;           
			newtio.c_iflag |= (INPCK | ISTRIP);           	
			break;           
		case 'E':           
			newtio.c_iflag |= (INPCK |ISTRIP);           
			newtio.c_cflag |= PARENB;           
			newtio.c_cflag &= ~PARODD;           	
			break;       	 
		case 'N':           
			newtio.c_cflag &= ~PARENB;           	
			break;       
	}   

	/***********波特率選擇****************/       
	switch(nSpeed){           
		case 2400:           
			cfsetispeed(&newtio,B2400);           
			cfsetospeed(&newtio,B2400);           	
			break;       	 
		case 4800:           
			cfsetispeed(&newtio,B4800);           
			cfsetospeed(&newtio,B4800);           	
			break;       	 
		case 9600:           
			cfsetispeed(&newtio,B9600);           
			cfsetospeed(&newtio,B9600);           	
			break;     	 
		case 57600:           
			cfsetispeed(&newtio,B57600);           
			cfsetospeed(&newtio,B57600);           	
			break;       	 
		case 115200:           
			cfsetispeed(&newtio,B115200);           
			cfsetospeed(&newtio,B115200);           	
			break;        	 
		case 460800:           
			cfsetispeed(&newtio,B460800);           
			cfsetospeed(&newtio,B460800);           	
			break;                 	 
		default:           
			cfsetispeed(&newtio,B9600);           
			cfsetospeed(&newtio,B9600);               	
			break;       
	}   

	/***********停止位選擇****************/      
	if(nStop == 1){           
		newtio.c_cflag &= ~CSTOPB;       
	}       
	else if(nStop ==2){           
		newtio.c_cflag |= CSTOPB;       
	}       

	newtio.c_cc[VTIME] = 1;       
	newtio.c_cc[VMIN] = FRAME_MAXSIZE;   //阻塞條件下有效      
	tcflush(fd,TCIFLUSH);       
	if((tcsetattr(fd,TCSANOW,&newtio)) != 0){           
		perror("com set error");           
		return -1;       
	}       
	printf("set done!\n");       
	return 0;   
}

static int open_port(int comport)   
{    
	int fd;
	/***********打開串口1****************/    
	if(comport == 1){           
		fd = open("/dev/ttyAT1",O_RDWR|O_NOCTTY|O_NDELAY);       
		if(fd == -1){           
			perror("Can't Open Serial Port");           
			return -1;           
		}       
	}    

	/***********打開串口2****************/     
	else if(comport == 2){           
		fd = open("/dev/ttyAT2",O_RDWR|O_NOCTTY|O_NDELAY);           
		if(fd == -1){              
			perror("Can't Open Serial Port");               
			return -1;           
		}       
	}   

	/***********打開串口3****************/      
	else if(comport == 3){           
		fd = open("/dev/ttyAT3",O_RDWR|O_NOCTTY|O_NDELAY);           
		if(fd == -1){              
			perror("Can't Open Serial Port");               
			return -1;           
		}       
	}   	

	if(comport == 1){		
		if(fcntl(fd,F_SETFL,FNDELAY) < 0){//非阻塞,覆蓋前面open的屬性		   			
			printf("fcntl failed\n");   		
		}   		
		else{   		
			printf("fcntl=%d\n",fcntl(fd,F_SETFL,FNDELAY));   		
		} 	
	}	
	else{		
		if(fcntl(fd,F_SETFL,0) < 0){	//阻塞,即使前面在open串口設備時設置的是非阻塞的,這裏設爲阻塞後,以此爲準		
			printf("fcntl failed\n");   		
		}   		
		else{   		
			printf("fcntl=%d\n",fcntl(fd,F_SETFL,0));   		
		} 	
	}       

	if(isatty(STDIN_FILENO) == 0){ 	      
		printf("standard input is not a terminal device\n");       
	}       
	else{   		        
		printf("isatty sucess!\n");       
	}      
	printf("fd-open=%d\n",fd);       
	return fd;   
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章