Chinaunix首页 | 论坛 | 博客
  • 博客访问: 60379
  • 博文数量: 15
  • 博客积分: 517
  • 博客等级: 一等列兵
  • 技术积分: 230
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-14 09:54
文章分类

全部博文(15)

文章存档

2012年(15)

分类:

2012-04-17 15:41:33

原文地址:linux 下简单的minicom 作者:zrx171407518

  1. /*
  2.  ============================================================================
  3.  Name : testio.c
  4.  Author :
  5.  Version :
  6.  Copyright : Your copyright notice
  7.  Description : Hello World in C, Ansi-style
  8.  ============================================================================
  9.  */

  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <fcntl.h>
  16. #include <termios.h>
  17. #include <errno.h>
  18. #include <string.h>

  19. #define BUFF_SIZE 1024

  20. int set_option(int fd,int baudrate,int bits,char event,int stops);

  21. int main(void) {
  22.     unsigned char buff[BUFF_SIZE];
  23.     ssize_t rd;
  24.     fd_set rdset;
  25.     int fd = open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
  26.     if(fd < 0){
  27.         //printf("open file test.txt fail!\n");
  28.         perror("open file");
  29.         return 1;
  30.     }
  31.     else
  32.     {
  33.         printf("opened serial port id %d\n",fd);
  34.     }

  35.     

  36.     int fcr = fcntl(fd,F_SETLK,0);
  37.     if(fcr < 0)
  38.     {
  39.         perror("ftcnl:");
  40.     }
  41.     else
  42.     {
  43.         printf("ftcnl %d\n",fcr);
  44.     }

  45.     if(isatty(STDIN_FILENO)==0)
  46.     {
  47.         printf("standard input is not a termial device");
  48.     }
  49.     else
  50.     {
  51.         printf("isatty\n");
  52.     }

  53.     set_option(fd,115200,8,'N',1);

  54.     memset(buff,0,BUFF_SIZE);

  55.     while(1)
  56.     {
  57.         FD_ZERO(&rdset);
  58.         FD_SET(fd,&rdset);
  59.         FD_SET(0,&rdset);
  60.         
  61.         if(select(fd+1,&rdset,NULL,NULL,NULL) < 0)
  62.         {
  63.             perror("select");
  64.             break;
  65.         }
  66.     
  67.         if(FD_ISSET(0,&rdset))
  68.         {
  69.             rd = read(fd,buff,BUFF_SIZE);
  70.             if(rd > 0){
  71.                 buff[rd] = 0;
  72.                 //printf("after seek just read data %d bytes\n",rd);
  73.                 //printf("%s",buff);
  74.                 write(1,buff,rd);
  75.             }
  76.         }

  77.         if(FD_ISSET(fd,&rdset))
  78.         {
  79.             rd = read(0,buff,BUFF_SIZE);
  80.             if(rd > 0){
  81.                 buff[rd] = 0;
  82.                 //printf("after seek just read data %d bytes\n",rd);
  83.                 //printf("%s",buff);
  84.                 write(fd,buff,rd);
  85.             }
  86.         }
  87.     }

  88.     close(fd);

  89.     return EXIT_SUCCESS;
  90. }

  91. int set_option(int fd,int baudrate,int bits,char event,int stops)
  92. {
  93.     struct termios newtio,oldtio;
  94.     if(tcgetattr(fd,&oldtio) != 0)
  95.     {
  96.         perror("get attr");
  97.         return -1;
  98.     }

  99.     bzero(&newtio,sizeof(newtio));
  100.     newtio.c_cflag |= CLOCAL | CREAD;
  101.     newtio.c_cflag &= ~CSIZE;
  102.     switch(bits)
  103.     {
  104.         case 7: newtio.c_cflag |= CS7;
  105.             break;
  106.         case 8: newtio.c_cflag |= CS8;
  107.             break;
  108.     }

  109.     switch(event)
  110.     {
  111.         case 'N': newtio.c_cflag &= ~PARENB;
  112.             break;
  113.         case 'O': newtio.c_cflag |= PARENB;
  114.             newtio.c_cflag |= PARODD;
  115.             newtio.c_cflag |= (INPCK | ISTRIP);
  116.             break;
  117.         case 'E': newtio.c_cflag |= PARENB;
  118.              newtio.c_cflag |= ~PARODD;
  119.             newtio.c_cflag |= (INPCK | ISTRIP);
  120.             break;
  121.         
  122.     }

  123.     switch(baudrate)
  124.     {
  125.         case 9600:
  126.             cfsetispeed(&newtio,B9600);
  127.             cfsetospeed(&newtio,B9600);
  128.             break;
  129.         case 115200:
  130.         default:
  131.             cfsetispeed(&newtio,B115200);
  132.             cfsetospeed(&newtio,B115200);
  133.             break;
  134.     
  135.     }    

  136.      if(stops == 1)
  137.     {
  138.         newtio.c_cflag &= ~CSTOPB;
  139.     }
  140.     else if(stops == 2)
  141.     {
  142.         newtio.c_cflag |= CSTOPB;
  143.     }

  144.     newtio.c_cc[VTIME] = 0;
  145.     newtio.c_cc[VMIN] = 0;
  146.        tcflush(fd,TCIFLUSH);

  147.     if(tcsetattr(fd,TCSANOW,&newtio) != 0)
  148.     {
  149.         perror("set attr");
  150.         return -1;
  151.     }

  152.     printf("set done!\n");
  153.     return 0;

  154. }
在ubuntu10.4下测试可用
阅读(548) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~