linux 串口 用户空间编程,linux串口空间编程


打开串口:

fd = open(dev,O_RDWR|O_NOCTTY|O_NONBLOCK)

设置属性:

#include     //在编译器中

struct termios newtio;
newtio.c_cflag |= CLOCAL | CREAD;   //CLOCAL:保证程序不会占用串口  
                                    //CREAD:能够从串口中读取输入数据
/*数据位*/ 
newtio.c_cflag |= CS8;          //8位
/*停止位*/
newtio.c_cflag &= ~CSTOPB;     //1位                    
/*奇偶校验位*/
newtio.c_cflag &= ~PARODD;     //不校验       
/*设置波特率*/
cfsetispeed(&newtio,B115200);   //115200
cfsetospeed(&newtio,B115200);
//设置等待时间和最小接收字符数
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
//处理未接收字符
tcflush(fd,TCIFLUSH);
//激活配置
tcsetattr(fd,TCSANOW,&newtio)   //成功返回0

读写操作:

char uart0_byte = 0;

write(fd,"1",1);
read(uart0_fd,&uart0_byte,1);

 

相关内容