转自:http://www.cnblogs.com/darryo/p/selectpollepoll-on-serial-port.html

In this article, I will use three asynchronous conferencing--select, poll and epoll on serial port to transmit data between PC and Raspberry pi.


Outline

  1. Character device file of serial port
  2. Naive serial communication
  3. Asynchronous conferencing
  4. Select
  5. Poll
  6. Epoll

Character device of serial port

My device is Raspberry pi with debian system and PC with ubuntu12.04 system.

And I have used a USB-TTL to link the these device.

The character device files on the two device is :

/dev/ttyUSB0 #Ubuntu

/dev/ttyAMA0 #Debian Raspberry pi

These two files are what we must use to achieve the lab.

But there is a little trap of /dev/ttyAMA0.

By default, Raspberry pi uses /dev/ttyAMA0 as a output of serial. Therefor we could use minicom or putty to control our device. However, we have to modify the default function of serial, since we will use our own method to use serial port.

So we should modify two files:

/boot/comdline.txt

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 rootfstype=ext4 elevator=deadline rootwait console=tty1 root=/dev/mmcblk0p2

Delete console=ttyAMA0,115200

/etc/inittab

T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

Comment the line above.


Now, start to code:

Naive version

The naive serial port communication version

Open the device, set the baud rate, and set parity

#include     <stdio.h>      /*标准输入输出定义*/
#include     <string.h>
#include     <stdlib.h>     /*标准函数库定义*/
#include     <unistd.h>     /*Unix标准函数定义*/
#include     <sys/types.h>  /**/
#include     <sys/stat.h>   /**/
#include     <fcntl.h>      /*文件控制定义*/
#include     <termios.h>    /*PPSIX终端控制定义*/
#include     <errno.h>      /*错误号定义*/
#define FALSE 0
#define TRUE 1void set_speed(int fd) {struct  termios Opt;tcgetattr(fd, &Opt);cfsetispeed(&Opt,B115200);cfsetospeed(&Opt,B115200);tcsetattr(fd,TCSANOW,&Opt);return;
}void set_Parity(int fd) {struct termios options;tcgetattr(fd, &options);options.c_lflag  &= ~(ICANON | ECHO | ECHOE | ISIG);  /*Input*/options.c_oflag  &= ~OPOST;   /*Output*/tcsetattr(fd,TCSANOW,&options);return;
}int OpenSerial(char *Dev) {int fd = open( Dev, O_RDWR );         //| O_NOCTTY | O_NDELAYif (-1 == fd) { /*设置数据位数*/perror("Can't Open Serial Port");return -1;}else {set_speed(fd);set_Parity(fd);return fd;}}int main(){int fd; ssize_t length;char buff[512];char *dev ="/dev/ttyAMA0";fd = OpenSerial(dev);for(;;){length = read(fd,buff,sizeof(buff));if(length > 0) {buff[length] = 0;printf("plain:%s\n",buff);}}close(fd);exit(0);
}


Select version

#include <sys/time.h>
#include <sys/types.h>
#include "serial/serial.h"int main() {int fd;fd_set rfds;struct timeval tv;char buff[512];ssize_t length;fd = OpenSerial("/dev/ttyAMA0");for(;;) {FD_ZERO(&rfds);FD_SET(fd, &rfds);//timeout = 5stv.tv_sec = 5;tv.tv_usec = 0;//Wait for 5 seconds, then goint n;n = select(fd + 1, &rfds, NULL, NULL, &tv);//choose the target from setif(n > 0) {if (FD_ISSET(fd, &rfds)) {length = read(fd, &buff, sizeof(buff));buff[length] = 0;printf("select:%s\n", buff);}} else {printf("No data within 5 seconds.\n");}}return 0;
}

Poll version

#include <sys/poll.h>
#include "serial/serial.h"
int main(void) {struct pollfd fds[1];ssize_t length;char buff[512];fds[0].fd = OpenSerial("/dev/ttyAMA0");fds[0].events = POLLIN ;for(;;) {int n;n = poll( fds, 1, 5000);//got data, and look up which fd has data, but we just have 1if(n > 0) {//if( fds[0].revents & POLLIN ) {length = read(fds[0].fd, buff, sizeof(buff) );buff[length] = 0;printf("poll:%s\n",buff);} else {printf("No data within 5 seconds.\n");}}
} 

Epoll version

#include <sys/epoll.h>
#include "serial/serial.h"#define MAXEVENTS 64int main(void){int fd;int efd;struct epoll_event event;struct epoll_event *events;int length;char buff[512];fd = OpenSerial("/dev/ttyAMA0");efd = epoll_create1 (0);//initial is 0event.data.fd = fd;event.events = EPOLLIN | EPOLLET;epoll_ctl (efd, EPOLL_CTL_ADD, fd, &event);/* Buffer where events are returned */events = calloc (MAXEVENTS, sizeof event);/* The event loop */for(;;) {int n;n = epoll_wait (efd, events, MAXEVENTS, 5000);if(n > 0) {length = read(events[0].data.fd, buff, sizeof(buff));if(length > 0) {buff[length] = 0;printf("epoll:%s\n", buff);}} else {printf("No data whthin 5 seconds.\n");}}free (events);close (fd);return 0;
}

[serial]基于select/poll/epoll的串口操作相关推荐

  1. python3 异步 非阻塞 IO多路复用 select poll epoll 使用

    有许多封装好的异步非阻塞IO多路复用框架,底层在linux基于最新的epoll实现,为了更好的使用,了解其底层原理还是有必要的. 下面记录下分别基于Select/Poll/Epoll的echo ser ...

  2. Python异步非阻塞IO多路复用Select/Poll/Epoll使用

    来源:http://www.haiyun.me/archives/1056.html 有许多封装好的异步非阻塞IO多路复用框架,底层在linux基于最新的epoll实现,为了更好的使用,了解其底层原理 ...

  3. select poll epoll IO操作多路复用及猴子补丁

    一:select(能监控数量有限,不能告诉用户程序具体那个连接有数据) select目前几乎所有的平台都支持,其良好的跨平台支持也是一个优点 select的缺点在于单个进程能够监控的文件描述的数量存在 ...

  4. select,poll,epoll的归纳总结区分

     Select.Poll与Epoll比较 以下资料都是来自网上搜集整理.引用源详见文章末尾. 1 Select.Poll与Epoll简介 Select select本质上是通过设置或者检查存放fd ...

  5. linux poll函数 实现,Linux select/poll/epoll 原理(一)实现基础

    本序列涉及的 Linux 源码都是基于 linux-4.14.143 . 1. 文件抽象 与 poll 操作 1.1 文件抽象 在 Linux 内核里,文件是一个抽象,设备是个文件,网络套接字也是个文 ...

  6. I/O多路复用之select,poll,epoll简介

    一.select 1.起源 select最早于1983年出现在4.2BSD中(BSD是早期的UNIX版本的分支). 它通过一个select()系统调用来监视多个文件描述符的数组,当select()返回 ...

  7. C++面试 select poll epoll之间的区别

    目录 摘要 场景描述 Select poll epoll 总结 摘要 先明确几个概念: 面试官问:给我讲讲什么事同步阻塞.异步阻塞.同步非阻塞.异步非阻塞. 我:????? 同步和异步的概念 同步是指 ...

  8. 《嵌入式系统 – 玩转ART-Pi开发板(基于RT-Thread系统)》第9章 基于Select/Poll实现并发服务器(二)

    基于Select/Poll实现并发服务器(一) 9.3 Select/Poll概述 在LWIP中,如果要实现并发服务器,可以基于Sequentaial API来实现,这种方式需要使用多线程,也就是为每 ...

  9. 【C/C++服务器开发】文件,文件描述符,I/O多路复用,select / poll / epoll 详解

    文章目录 一.前言 1.文件的概念 2.文件描述符和文件指针 文件描述符 文件描述符和文件指针的区别 文件描述符太多了怎么办 二.I/O多路复用 1.I/O多路复用的由来 不要打电话给我,有需要我会打 ...

最新文章

  1. 信息学奥赛一本通 1942:【08NOIP普及组】ISBN号码 | OpenJudge NOI 1.7 29:ISBN号码 | 洛谷 P1055 [NOIP2008 普及组] ISBN 号码
  2. c中获取python控制台输出_linux c程序中获取shell脚本输出的实现方法
  3. ~~高精度除以低精度
  4. 清华镜像源_Hyperledger Fabric2.x Docker镜像编译加速
  5. CodeBlocks常用操作快捷键
  6. Java项目案例-猜数小游戏和多级菜单系统.....
  7. Qt开发 之 Windows资源管理器模仿 并 小超越
  8. 毕业设计html5作品,基于HTML5的年货购物网站的设计与实现毕业论文+任务书+开题报告+设计源码...
  9. 数据分析软件哪个最好用?
  10. 梦行扫码付(收银台条码支付 微信钱包条码支付 支付宝二维码支付 手机APP钱包支付 PHP扫码支付 )
  11. 谷歌浏览器Chrome被hao123劫持怎么解决?---- 被hao123、2345、360等主页劫持和捆绑的解决方法
  12. 积木拼图游戏-儿童游戏免费拼图3-6岁
  13. Python爬取百度翻译及有道翻译
  14. LeetCode 2389. 和有限的最长子序列
  15. 计算机专业笔记本用i5还是i7,玩游戏笔记本i5和i7的区别_笔记本电脑游戏用i5还是i7...
  16. 【蓝桥杯】三羊献瑞-算法题JAVA解
  17. GitHub上搭建个人网站
  18. oracle ora-各种常见java.sql.SQLException归纳
  19. 特征函数篇2——与概率密度的函数
  20. Hadoop入门介绍

热门文章

  1. oracle 存储过程导出sql语句 导出为文件
  2. Intel FPGA 的时序约束
  3. linux 脚本启动oracle,linux自动启动 oracle脚本
  4. 10年老电脑如何提速_2020年10月和双十一轻薄本/轻薄型笔记本电脑如何挑选?内含轻薄本/轻薄型笔记本电脑推荐!...
  5. python内置模块在哪个文件夹_用 Python 内置模块处理 ini 配置文件
  6. ubuntu个人版和server_Ubuntu Desktop和Ubuntu Server有何区别? | MOS86
  7. php.ini 没有pdo,检查了启用的php.ini文件“ extension = php_pdo_mysql.dll”,但仍然出错...
  8. element元素 取属性_JS-DOM Element方法和属性
  9. bootice添加linux_如何使用老毛桃winpe的Bootice工具新建实模式启动项(Grub/Linux)?
  10. linux java 自启动_Linux设置开机启动脚本