转自: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

  1. #include <stdio.h> /*标准输入输出定义*/
  2. #include <string.h>
  3. #include <stdlib.h> /*标准函数库定义*/
  4. #include <unistd.h> /*Unix标准函数定义*/
  5. #include <sys/types.h> /**/
  6. #include <sys/stat.h> /**/
  7. #include <fcntl.h> /*文件控制定义*/
  8. #include <termios.h> /*PPSIX终端控制定义*/
  9. #include <errno.h> /*错误号定义*/
  10. #define FALSE 0
  11. #define TRUE 1
  12.  
  13. void set_speed(int fd) {
  14. struct termios Opt;
  15. tcgetattr(fd, &Opt);
  16. cfsetispeed(&Opt,B115200);
  17. cfsetospeed(&Opt,B115200);
  18. tcsetattr(fd,TCSANOW,&Opt);
  19. return;
  20. }
  21.  
  22. void set_Parity(int fd) {
  23. struct termios options;
  24. tcgetattr(fd, &options);
  25. options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
  26. options.c_oflag &= ~OPOST; /*Output*/
  27. tcsetattr(fd,TCSANOW,&options);
  28. return;
  29. }
  30.  
  31. int OpenSerial(char *Dev) {
  32. int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY
  33. if (- == fd) { /*设置数据位数*/
  34. perror("Can't Open Serial Port");
  35. return -;
  36. }
  37. else {
  38. set_speed(fd);
  39. set_Parity(fd);
  40. return fd;
  41. }
  42.  
  43. }
  44.  
  45. int main(){
  46. int fd;
  47. ssize_t length;
  48. char buff[];
  49. char *dev ="/dev/ttyAMA0";
  50. fd = OpenSerial(dev);
  51. for(;;){
  52. length = read(fd,buff,sizeof(buff));
  53. if(length > ) {
  54. buff[length] = ;
  55. printf("plain:%s\n",buff);
  56. }
  57. }
  58. close(fd);
  59. exit();
  60. }

Select version

  1. #include <sys/time.h>
  2. #include <sys/types.h>
  3. #include "serial/serial.h"
  4.  
  5. int main() {
  6. int fd;
  7. fd_set rfds;
  8. struct timeval tv;
  9. char buff[];
  10. ssize_t length;
  11. fd = OpenSerial("/dev/ttyAMA0");
  12.  
  13. for(;;) {
  14. FD_ZERO(&rfds);
  15. FD_SET(fd, &rfds);
  16.  
  17. //timeout = 5s
  18. tv.tv_sec = ;
  19. tv.tv_usec = ;
  20. //Wait for 5 seconds, then go
  21. int n;
  22. n = select(fd + , &rfds, NULL, NULL, &tv);
  23. //choose the target from set
  24. if(n > ) {
  25. if (FD_ISSET(fd, &rfds)) {
  26. length = read(fd, &buff, sizeof(buff));
  27. buff[length] = ;
  28. printf("select:%s\n", buff);
  29. }
  30. } else {
  31. printf("No data within 5 seconds.\n");
  32. }
  33. }
  34. return ;
  35. }

Poll version

  1. #include <sys/poll.h>
  2. #include "serial/serial.h"
  3. int main(void) {
  4. struct pollfd fds[];
  5. ssize_t length;
  6. char buff[];
  7. fds[].fd = OpenSerial("/dev/ttyAMA0");
  8. fds[].events = POLLIN ;
  9. for(;;) {
  10. int n;
  11. n = poll( fds, , );
  12. //got data, and look up which fd has data, but we just have 1
  13. if(n > ) {
  14. //if( fds[0].revents & POLLIN ) {
  15. length = read(fds[].fd, buff, sizeof(buff) );
  16. buff[length] = ;
  17. printf("poll:%s\n",buff);
  18.  
  19. } else {
  20. printf("No data within 5 seconds.\n");
  21. }
  22. }
  23. }

Epoll version

  1. #include <sys/epoll.h>
  2. #include "serial/serial.h"
  3.  
  4. #define MAXEVENTS 64
  5.  
  6. int main(void){
  7. int fd;
  8. int efd;
  9. struct epoll_event event;
  10. struct epoll_event *events;
  11. int length;
  12. char buff[];
  13. fd = OpenSerial("/dev/ttyAMA0");
  14. efd = epoll_create1 ();//initial is 0
  15.  
  16. event.data.fd = fd;
  17. event.events = EPOLLIN | EPOLLET;
  18.  
  19. epoll_ctl (efd, EPOLL_CTL_ADD, fd, &event);
  20. /* Buffer where events are returned */
  21. events = calloc (MAXEVENTS, sizeof event);
  22.  
  23. /* The event loop */
  24. for(;;) {
  25. int n;
  26. n = epoll_wait (efd, events, MAXEVENTS, );
  27. if(n > ) {
  28. length = read(events[].data.fd, buff, sizeof(buff));
  29.  
  30. if(length > ) {
  31. buff[length] = ;
  32. printf("epoll:%s\n", buff);
  33. }
  34. } else {
  35. printf("No data whthin 5 seconds.\n");
  36. }
  37. }
  38. free (events);
  39. close (fd);
  40. return ;
  41. }

[serial]基于select/poll/epoll的串口操作的更多相关文章

  1. Python异步非阻塞IO多路复用Select/Poll/Epoll使用,线程,进程,协程

    1.使用select模拟socketserver伪并发处理客户端请求,代码如下: import socket import select sk = socket.socket() sk.bind((' ...

  2. 转一贴,今天实在写累了,也看累了--【Python异步非阻塞IO多路复用Select/Poll/Epoll使用】

    下面这篇,原理理解了, 再结合 这一周来的心得体会,整个框架就差不多了... http://www.haiyun.me/archives/1056.html 有许多封装好的异步非阻塞IO多路复用框架, ...

  3. select/poll/epoll on serial port

    In this article, I will use three asynchronous conferencing--select, poll and epoll on serial port t ...

  4. 多进程、协程、事件驱动及select poll epoll

    目录 -多线程使用场景 -多进程 --简单的一个多进程例子 --进程间数据的交互实现方法 ---通过Queues和Pipe可以实现进程间数据的传递,但是不能实现数据的共享 ---Queues ---P ...

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

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

  6. Select\Poll\Epoll异步IO与事件驱动

    事件驱动与异步IO 事件驱动编程是一种编程规范,这里程序的执行流由外部事件来规定.它的特点是包含一个事件循环,但外部事件发生时使用回调机制来触发响应的处理.另外两种常见的编程规范是(单线程)同步以及多 ...

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

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

  8. Linux 网络编程的5种IO模型:多路复用(select/poll/epoll)

    Linux 网络编程的5种IO模型:多路复用(select/poll/epoll) 背景 我们在上一讲 Linux 网络编程的5种IO模型:阻塞IO与非阻塞IO中,对于其中的 阻塞/非阻塞IO 进行了 ...

  9. 哪5种IO模型?什么是select/poll/epoll?同步异步阻塞非阻塞有啥区别?全在这讲明白了!

    系统中有哪5种IO模型?什么是 select/poll/epoll?同步异步阻塞非阻塞有啥区别? 本文地址http://yangjianyong.cn/?p=84转载无需经过作者本人授权 先解开第一个 ...

随机推荐

  1. “The operation cannot be completed because the DbContext has been disposed” exception with lazy load disabled

    http://stackoverflow.com/questions/18261732/the-operation-cannot-be-completed-because-the-dbcontext- ...

  2. 跟我学SharePoint 2013视频培训课程——理解SharePoint网站的体系结构(3)

    课程简介 第三天,理解SharePoint 2013 网站的体系结构 视频 SharePoint 2013 交流群 41032413

  3. 迁移TFS,批量将文档导入SharePoint 2013 文档库

    一.需求分析 公司需要将存在于旧系统(TFS)所有的文档迁移至新系统(SharePoint 2013).现已经将50G以上的文档拷贝到SharePoint 2013 Server上.这些文档是一些不规 ...

  4. CUDA 中的计时方法

    问题描述:一般利用CUDA进行加速处理时,都需要测试CUDA程序的运行时间,来对比得到的加速效果. 解决方法: 1).GPU端计时,即设备端计时. 2).CPU端计时,即主机端计时. 设备端计时有两种 ...

  5. Vue 点击波浪特效指令

  6. 微信小程序 confirm(删除提示)提示框,询问框,小程序操作成功提示后跳转

    微信小程序删除处理 没有 confrim 那怎么实现这个效果呢 可以使用小程序里的模态框 代码: wx.showModal({ title: '提示', content: '确定要删除吗?', suc ...

  7. 自动化无线网破解工具wifite2

    自动化无线网破解工具wifite2 wifite是一款自动化wifi密码破解工具,特点是支持多个wep.wpa加密的wifi网络,不支持windows和osx. wifite的特点是可以同时攻击多个采 ...

  8. 定时删除elasticsearch的index

    #!/bin/bashfind /data/elasticsearch/data/kz-log/nodes/0/indices/ -type d -mtime +5 |  awk -F"/& ...

  9. 如何使Android应用支持多种屏幕分辨率

    原文:http://android.eoe.cn/topic/android_sdk 描述: 让您指定您的应用支持的屏幕的大小并且可以通过屏幕兼容模式来支持比您应用所支持更大的屏幕.所以这对于您需要在 ...

  10. ios 关于屏幕旋转和屏幕晃动

    内置加速计是智能手机最酷的特性之一,ios可以通过这个小设备知道用户握持手机的方式,以及用户是否移动了手机,ios使用加速计处理自动旋转,并且许多游戏都是用它作为控制机制,它还可以用于检测摇动和其他突 ...