serialport控件的詳細用法,[serial]基于select/poll/epoll的串口操作

 2023-12-06 阅读 21 评论 0

摘要:轉自: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 Character device file

轉自: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;
}

?

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/189743.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息