一、设置uart的例子,测试uart,发出方波,或去读数据

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <linux/fs.h>
#include <linux/gpio.h>
#include <stdio.h>       
#include <stdlib.h>
#include <termios.h>
#include <string.h>

int main()
{  	
	char *filename1 = NULL;
    char buf1[100]={0x55,0x55,0x55,0x55};
	char buf2[100]={0x55,0x55,0x55,0x55};
	int ret = -1;
	int Oflags = 0;
	int fd1 = -1;
	int len = 0;

	filename1 = "/dev/ttyAMA2";
	struct termios options;

    fd1 = open(filename1, O_RDWR);
   	if (fd1 < 0) {
        	printf("app open %s fail\n", filename1);
        	return 0;
    } else {
		printf("app open  %s success\r\n",filename1);
    }
	//int flags = fcntl(fd1, F_GETFL, 0);
	//fcntl(fd1, F_SETFL, flags | O_NONBLOCK);


	if(tcgetattr(fd1,&options)  !=  0)  
	{  
		printf("get serial attibute fail");     
		return(-1);   
	}
	else{
		printf("get serial attibute success");
		printf("control model options.c_cflag : %d\n",options.c_cflag);
		printf("data bit options.c_cflag : %d\n",options.c_cflag);
		printf("parity bit options.c_cflag : %d\n",options.c_cflag);
		printf("raw data output options.c_oflag : %d\n",options.c_oflag);
	} 
	

	cfsetispeed(&options, B115200);   
	cfsetospeed(&options, B115200);   


  	//set control model 
    options.c_cflag |= CLOCAL;  
    options.c_cflag |= CREAD;

	options.c_cflag &= ~CRTSCTS;  

    //select data bit   
    options.c_cflag &= ~CSIZE; 
	
	options.c_cflag |= CS8;  
	
	//select parity bit   
    options.c_cflag &= ~PARENB;   
    options.c_iflag &= ~INPCK;
	
	// set stopbit  
	options.c_cflag &= ~CSTOPB;  

	//set raw data output 
	options.c_oflag &= ~OPOST;     
	options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);  
	//options.c_lflag &= ~(ISIG | ICANON);  
     
	//set wait time  
	options.c_cc[VTIME] = 1;    
	options.c_cc[VMIN] = 1; 
     
	tcflush(fd1,TCIFLUSH);  
     
	//set the attribute to HiSerial device 
	if (tcsetattr(fd1,TCSANOW,&options) != 0)    
	{  
   		printf("set serial attibute fail");
		return (-1);   
	}
	else
	{  
   		printf("set serial attibute success");
		printf("control model options.c_cflag : %d\n",options.c_cflag);
		printf("data bit options.c_cflag : %d\n",options.c_cflag);
		printf("parity bit options.c_cflag : %d\n",options.c_cflag);
		printf("raw data output options.c_oflag : %d\n",options.c_oflag);
	}

	printf("\r\n");

	memset(buf1,0x0,sizeof(buf1));
	memset(buf2,0x0,sizeof(buf2));
	buf2[0]= 0x55;
	buf2[1]= 0x55;
	buf2[2]= 0x55;
	buf2[3]= 0x55;
	buf2[4]= 0x55;

	while(1)
	{
		#if 1
		len = 0;
		len = read(fd1, buf1,10);
		if(len < 0)
		{
			printf("app read serial fail\n");
		}
		else
		{	
			printf("app read serial success %d byte\r\n",len);

			printf("app read serial success: ASCLL ");
			for(int i = 0; i < len; i++)
			printf(" %c ",buf1[i]);
		}
		printf("\r\n");
		#endif

		//len = 0;
		len = write(fd1,buf1,len);
		if(len < 0)
		{
			printf("app write serial fail\r\n");
		}
		else
		{			
			printf("app write buf1 serial success %d byte\r\n",len);
		}
		
		memset(buf1,0,sizeof(buf1));

		sleep(1);
		//len = 0;
		len = write(fd1,buf2,len);
		if(len < 0)
		{
			printf("app write serial fail\n");
		}
		else
		{			
			printf("app write buf2 serial success %d byte\r\n",len);
		}
		
		printf("app is run ......\r\n");
	}

	close(fd1);
   	return 0;
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐