linux 串口驱动与应用编程(三)

一、设置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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值