当前位置: 首页 > news >正文

linux下串口的阻塞和非阻塞操作

有两个可以进行控制串口阻塞性(同时控制read和write):一个是在打开串口的时候,open函数是否带O_NDELAY;第二个是可以在打开串口之后通过fcntl()函数进行控制。

阻塞的定义:

       对于read,block指当串口输入缓冲区没有数据的时候,read函数将会阻塞在这里,移植到串口输入缓冲区中有数据可读取,read读到了需要的字节数之后,返回值为读到的字节数;

对于write,block指当串口输出缓冲区满,或剩下的空间小于将要写入的字节数,则write将阻塞,一直到串口输出缓冲区中剩下的空间大于等于将要写入的字节数,执行写入操作,返回写入的字节数。

非阻塞的定义:

对于read,no block指当串口输入缓冲区没有数据的时候,read函数立即返回,返回值为0。

对于write,no block指当串口输出缓冲区满,或剩下的空间小于将要写入的字节数,则write将进行写操作,写入当前串口输出缓冲区剩下空间允许的字节数,然后返回写入的字节数。

 

[cpp]  view plain copy
 
  1. static int set_opt(int fd, int nSpeed, int nBits, char nEvent, int nStop)     
  2. {     
  3.     struct termios newtio;     
  4.     struct termios oldtio;     
  5.          
  6.     if(tcgetattr(fd,&oldtio) != 0)     
  7.     {     
  8.         perror("SetupSerial 1");     
  9.         return -1;     
  10.     }     
  11.          
  12.     bzero(&newtio,sizeof(newtio));     
  13.     newtio.c_cflag |= CLOCAL |CREAD;     
  14.     newtio.c_cflag &= ~CSIZE;     
  15.       
  16. /***********数据位选择****************/      
  17.     switch(nBits)     
  18.     {     
  19.         case 7:     
  20.         newtio.c_cflag |= CS7;     
  21.         break;     
  22.         case 8:     
  23.         newtio.c_cflag |= CS8;     
  24.         break;         
  25.     }     
  26. /***********校验位选择****************/    
  27.     switch(nEvent)     
  28.     {     
  29.         case 'O':     
  30.         newtio.c_cflag |= PARENB;     
  31.         newtio.c_cflag |= PARODD;     
  32.         newtio.c_iflag |= (INPCK | ISTRIP);     
  33.             break;     
  34.         case 'E':     
  35.         newtio.c_iflag |= (INPCK |ISTRIP);     
  36.         newtio.c_cflag |= PARENB;     
  37.         newtio.c_cflag &= ~PARODD;     
  38.             break;     
  39.          case 'N':     
  40.         newtio.c_cflag &= ~PARENB;     
  41.             break;     
  42.     }     
  43. /***********波特率选择****************/     
  44.     switch(nSpeed)     
  45.     {     
  46.         case 2400:     
  47.         cfsetispeed(&newtio,B2400);     
  48.         cfsetospeed(&newtio,B2400);     
  49.             break;     
  50.          case 4800:     
  51.         cfsetispeed(&newtio,B4800);     
  52.         cfsetospeed(&newtio,B4800);     
  53.             break;     
  54.          case 9600:     
  55.         cfsetispeed(&newtio,B9600);     
  56.         cfsetospeed(&newtio,B9600);     
  57.             break;   
  58.          case 57600:     
  59.         cfsetispeed(&newtio,B57600);     
  60.         cfsetospeed(&newtio,B57600);     
  61.             break;     
  62.          case 115200:     
  63.         cfsetispeed(&newtio,B115200);     
  64.         cfsetospeed(&newtio,B115200);     
  65.             break;     
  66.          case 460800:     
  67.         cfsetispeed(&newtio,B460800);     
  68.         cfsetospeed(&newtio,B460800);     
  69.             break;               
  70.          default:     
  71.         cfsetispeed(&newtio,B9600);     
  72.         cfsetospeed(&newtio,B9600);     
  73.                 break;     
  74.     }     
  75. /***********停止位选择****************/    
  76.     if(nStop == 1){     
  77.         newtio.c_cflag &= ~CSTOPB;     
  78.     }     
  79.     else if(nStop ==2){     
  80.         newtio.c_cflag |= CSTOPB;     
  81.     }     
  82.     newtio.c_cc[VTIME] = 1;     
  83.     newtio.c_cc[VMIN] = FRAME_MAXSIZE;   //阻塞条件下有效  
  84.     
  85.     tcflush(fd,TCIFLUSH);     
  86.     if((tcsetattr(fd,TCSANOW,&newtio)) != 0)     
  87.     {     
  88.         perror("com set error");     
  89.         return -1;     
  90.     }     
  91.     printf("set done!\n");     
  92.     return 0;     
  93. }     

 

[cpp]  view plain copy
 
  1. static int open_port(int fd,int comport)     
  2. {       
  3. /***********打开串口1****************/  
  4.     if(comport == 1)     
  5.     {     
  6.         fd = open("/dev/ttyAT1",O_RDWR|O_NOCTTY|O_NDELAY);     
  7.     if(fd == -1){     
  8.         perror("Can't Open Serial Port");     
  9.         return -1;     
  10.         }     
  11.     }     
  12.  /***********打开串口2****************/   
  13.     else if(comport == 2)     
  14.     {     
  15.         fd = open("/dev/ttyAT2",O_RDWR|O_NOCTTY|O_NDELAY);     
  16.         if(fd == -1){     
  17.             perror("Can't Open Serial Port");     
  18.             return -1;     
  19.         }     
  20.     }     
  21. /***********打开串口3****************/    
  22.     else if(comport == 3)     
  23.     {     
  24.         fd = open("/dev/ttyAT3",O_RDWR|O_NOCTTY|O_NDELAY);     
  25.         if(fd == -1){     
  26.             perror("Can't Open Serial Port");     
  27.             return -1;     
  28.         }     
  29.     }     
  30.     if(comport == 1)  
  31.     {  
  32.         if(fcntl(fd,F_SETFL,FNDELAY) < 0)//非阻塞,覆盖前面open的属性  
  33.         {     
  34.             printf("fcntl failed\n");     
  35.         }     
  36.         else{     
  37.         printf("fcntl=%d\n",fcntl(fd,F_SETFL,FNDELAY));     
  38.         }   
  39.     }  
  40.     else  
  41.     {  
  42.         if(fcntl(fd,F_SETFL,0) < 0){   //阻塞,即使前面在open串口设备时设置的是非阻塞的,这里设为阻塞后,以此为准  
  43.         printf("fcntl failed\n");     
  44.         }     
  45.         else{     
  46.         printf("fcntl=%d\n",fcntl(fd,F_SETFL,0));     
  47.         }   
  48.     }     
  49.     if(isatty(STDIN_FILENO) == 0){   
  50.         
  51.     printf("standard input is not a terminal device\n");     
  52.     }     
  53.     else{     
  54.           
  55.         printf("isatty sucess!\n");     
  56.     }    
  57.   
  58.     printf("fd-open=%d\n",fd);     
  59.     return fd;     
  60. }  

 

所以,linux的串口的阻塞性通过fcntl()函数进行设置即可。

 

[cpp]  view plain copy
 
  1. 阻塞:fcntl(fd,F_SETFL,0)  
[cpp]  view plain copy
 
    1. 非阻塞:fcntl(fd,F_SETFL,FNDELAY)  

相关文章:

  • shell 中 21 的使用
  • Java静态代码分析工具Infer
  • 图书馆管理系统程序设计
  • 二进制、八进制、十进制、十六进制之间转换
  • WindowsFormApplication 基础篇0:30分钟撸出自己的浏览器
  • LeetCode:Reverse LinkedList
  • Oracle的rownum原理和使用(整理几个达人的帖子)
  • 安装cacti可能出现的问题
  • json_encode详解,转义
  • 真正的人工智能离我们有多远
  • 我的菜谱:鱼香肉丝
  • 页面制作部分之PS切图
  • NSA用OpenFlow,间谍机构的SDN轰趴
  • ThinkPhp学习11
  • 微软的操作系统中让 32 位支持大于 4GB 的内存。
  • 【编码】-360实习笔试编程题(二)-2016.03.29
  • Android组件 - 收藏集 - 掘金
  • Angular 2 DI - IoC DI - 1
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • HomeBrew常规使用教程
  • iOS小技巧之UIImagePickerController实现头像选择
  • Java教程_软件开发基础
  • java取消线程实例
  • JAVA之继承和多态
  • overflow: hidden IE7无效
  • Promise面试题,控制异步流程
  • SpiderData 2019年2月16日 DApp数据排行榜
  • VUE es6技巧写法(持续更新中~~~)
  • 爱情 北京女病人
  • 得到一个数组中任意X个元素的所有组合 即C(n,m)
  • 机器人定位导航技术 激光SLAM与视觉SLAM谁更胜一筹?
  • 记录一下第一次使用npm
  • 批量截取pdf文件
  • 手机app有了短信验证码还有没必要有图片验证码?
  • 学习使用ExpressJS 4.0中的新Router
  • MyCAT水平分库
  • ​LeetCode解法汇总1276. 不浪费原料的汉堡制作方案
  • ​插件化DPI在商用WIFI中的价值
  • # Pytorch 中可以直接调用的Loss Functions总结:
  • #每日一题合集#牛客JZ23-JZ33
  • ( 用例图)定义了系统的功能需求,它是从系统的外部看系统功能,并不描述系统内部对功能的具体实现
  • (13)Hive调优——动态分区导致的小文件问题
  • (9)STL算法之逆转旋转
  • (C++)栈的链式存储结构(出栈、入栈、判空、遍历、销毁)(数据结构与算法)
  • (java版)排序算法----【冒泡,选择,插入,希尔,快速排序,归并排序,基数排序】超详细~~
  • (二)Linux——Linux常用指令
  • (翻译)Entity Framework技巧系列之七 - Tip 26 – 28
  • (附源码)python旅游推荐系统 毕业设计 250623
  • (附源码)spring boot球鞋文化交流论坛 毕业设计 141436
  • (附源码)ssm高校运动会管理系统 毕业设计 020419
  • (机器学习的矩阵)(向量、矩阵与多元线性回归)
  • (九十四)函数和二维数组
  • (生成器)yield与(迭代器)generator
  • (原創) 未来三学期想要修的课 (日記)
  • (转)Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案一