Select/poll/epoll on serial port

Source: Internet
Author: User

In this article, I'll use three asynchronous Conferencing--select, poll and Epoll on serial port to transmit data betwee n 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 the 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 and the device is:

/dev/ttyusb0 #Ubuntu

/dev/ttyama0 #Debian Raspberry Pi

These the 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'll use our own method to use serial port.

So we should modify the 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>/* standard input/output definition */#include <string.h> #include <stdlib.h>/* Standard function library definition */#i Nclude <unistd.h>/*unix standard function definition */#include <sys/types.h>/**/#include <sys/stat.h>/**/      #include <fcntl.h>/* File control definition */#include <termios.h>/*ppsix terminal control definition */#include <errno.h>    /* ERROR Number Definition */#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_ndelay if ( -1 = = FD) {/* Set data bit number */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 = 5s        tv.tv_sec = 5;        tv.tv_usec = 0;        //Wait for 5 seconds, then go        int n;        n = select(fd + 1, &rfds, NULL, NULL, &tv);        //choose the target from set        if(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 1        if(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 0 event.data.fd = FD; event.events = Epollin |    Epollet;    Epoll_ctl (EFD, Epoll_ctl_add, FD, &event);    /* Buffer where events is 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;}  

Select/poll/epoll on serial port

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.