Chinaunix首页 | 论坛 | 博客
  • 博客访问: 354715
  • 博文数量: 79
  • 博客积分: 1270
  • 博客等级: 中尉
  • 技术积分: 1370
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-12 08:48
个人简介

freedom~~~~~~~~~~

文章分类

全部博文(79)

文章存档

2014年(10)

2013年(2)

2012年(13)

2011年(54)

分类: LINUX

2011-04-25 15:09:34

    1. #include<stdio.h>
    2. #include<stdlib.h>
    3. #include<unistd.h>
    4. #include<sys/types.h>
    5. #include<sys/stat.h>
    6. #include<fcntl.h>
    7. #include<termios.h>
    8. #include<errno.h>

    9. int main()
    10. {
    11.     //打开uart口
    12.     int fd=open("/dev/ttyS0",O_RDWR);
    13.     if(fd==-1)
    14.     {
    15.         printf("can't open the uart");
    16.     }
    17.     
    18.     //修改串口波特率
    19.     struct termios Opt;
    20.     tcgetattr(fd,&Opt);
    21.     cfsetispeed(&Opt,B115200);
    22.     cfsetospeed(&Opt,B115200);
    23.     tcsetattr(fd,ICANON,&Opt);
    24.     Opt.c_cflag &=~PARENB;
    25.     Opt.c_cflag &=~CSTOPB;
    26.     Opt.c_cflag &=~CSIZE;
    27.     Opt.c_cflag |=~CS8;
    28.     
    29.     //读写串口
    30.     T:
    31.     while(1)
    32.     {
    33.         char buffer;
    34.         if(scanf("%c",&buffer)!=0)
    35.         {
    36.             printf("received from the keyboard %c",buffer);
    37.             write(fd,&buffer,1);
    38.             break;
    39.         }
    40.     }

    41.     int i;
    42.     for(i=1;i<=100;i++)
    43.     {
    44.         char buff[1024];
    45.         int readByte=read(fd,buff,sizeof(buff));
    46.         buff[readByte]='\0';
    47.         printf("%s",buff);
    48.     }

    49.     goto T;
    50.     close(fd);    
    51. }

    1. 使用了goto语句,进入下一个输入读写。

  1. #include          /*标准输入输出定义*/
  2. #include         /*标准函数库定义*/
  3. #include         /*Unix 标准函数定义*/
  4. #include      
  5. #include      
  6. #include          /*文件控制定义*/
  7. #include        /*PPSIX 终端控制定义*/
  8. #include          /*错误号定义*/
  9.  
  10. #define FALSE  -1
  11. #define TRUE   0
  12. /**
  13. *@brief  设置串口通信速率
  14. *@param  fd     类型 int  打开串口的文件句柄
  15. *@param  speed  类型 int  串口速度
  16. *@return  void
  17. */
  18. int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
  19.                    B38400, B19200, B9600, B4800, B2400, B1200, B300, };
  20. int name_arr[] = {38400,  19200,  9600,  4800,  2400,  1200,  300, 38400, 
  21.                             19200,  9600, 4800, 2400, 1200,  300, };
  22. void set_speed(int fd, int speed){
  23.          int   i;
  24.          int   status;
  25.          struct termios   Opt;
  26.          tcgetattr(fd, &Opt);
  27.          for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++) {
  28.                    if  (speed == name_arr[i]) {    
  29.                             tcflush(fd, TCIOFLUSH);    
  30.                             cfsetispeed(&Opt, speed_arr[i]); 
  31.                             cfsetospeed(&Opt, speed_arr[i]);  
  32.                             status = tcsetattr(fd, TCSANOW, &Opt); 
  33.                             if  (status != 0) {       
  34.                                      perror("tcsetattr fd"); 
  35.                                      return;    
  36.                             }   
  37.                             tcflush(fd,TCIOFLUSH);  
  38.                    } 
  39.          }
  40. }
  41. /**
  42. *@brief   设置串口数据位,停止位和效验位
  43. *@param  fd     类型  int  打开的串口文件句柄
  44. *@param  databits 类型  int 数据位   取值 为 7 或者8
  45. *@param  stopbits 类型  int 停止位   取值为 1 或者2
  46. *@param  parity  类型  int  效验类型 取值为N,E,O,,S
  47. */
  48. int set_Parity(int fd,int databits,int stopbits,int parity)
  49. {
  50.          struct termios options;
  51.          if  ( tcgetattr( fd,&options)  !=  0) {
  52.                    perror("SetupSerial 1");    
  53.                    return(FALSE); 
  54.          }
  55.          options.c_cflag &= ~CSIZE;
  56.          options.c_lflag  &= ~(ICANON | ECHO | ECHOE | ISIG);  /*Input*/
  57.          options.c_oflag  &= ~OPOST;   /*Output*/
  58.  
  59.          switch (databits) /*设置数据位数*/
  60.          {   
  61.          case 7:                
  62.                    options.c_cflag |= CS7;
  63.                    break;
  64.          case 8:    
  65.                    options.c_cflag |= CS8;
  66.                    break;  
  67.          default:   
  68.                    fprintf(stderr,"Unsupported data size\n"); return (FALSE); 
  69.          }
  70. switch (parity)
  71. {  
  72.          case 'n':
  73.          case 'N':   
  74.                    options.c_cflag &= ~PARENB;   /* Clear parity enable */
  75.                    options.c_iflag &= ~INPCK;     /* Enable parity checking */
  76.                    break; 
  77.          case 'o':  
  78.          case 'O':    
  79.                    options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/ 
  80.                    options.c_iflag |= INPCK;             /* Disnable parity checking */
  81.                    break; 
  82.          case 'e': 
  83.          case 'E':  
  84.                    options.c_cflag |= PARENB;     /* Enable parity */   
  85.                    options.c_cflag &= ~PARODD;   /* 转换为偶效验*/    
  86.                    options.c_iflag |= INPCK;       /* Disnable parity checking */
  87.                    break;
  88.          case 'S':
  89.          case 's':  /*as no parity*/  
  90.              options.c_cflag &= ~PARENB;
  91.                    options.c_cflag &= ~CSTOPB;break; 
  92.          default:  
  93.                    fprintf(stderr,"Unsupported parity\n");   
  94.                    return (FALSE); 
  95.          } 
  96. /* 设置停止位*/ 
  97. switch (stopbits)
  98. {  
  99.          case 1:   
  100.                    options.c_cflag &= ~CSTOPB; 
  101.                    break; 
  102.          case 2:   
  103.                    options.c_cflag |= CSTOPB; 
  104.             break;
  105.          default:   
  106.                     fprintf(stderr,"Unsupported stop bits\n"); 
  107.                     return (FALSE);
  108. }
  109. /* Set input parity option */
  110. if (parity != 'n')  
  111.          options.c_iflag |= INPCK;
  112. tcflush(fd,TCIFLUSH);
  113. options.c_cc[VTIME] = 0; /* 设置超时15 seconds*/  
  114. options.c_cc[VMIN] = 13; /* define the minimum bytes data to be readed*/
  115. if (tcsetattr(fd,TCSANOW,&options) != 0)  
  116. {
  117.          perror("SetupSerial 3");  
  118.          return (FALSE); 
  119. }
  120. return (TRUE); 
  121. }
  122. /**********************************************************************
  123. 代码说明:使用串口一测试的,发送的数据是字符,
  124. 但是没有发送字符串结束符号,所以接收到后,后面加上了结束符号
  125. **********************************************************************/
  126.  
  127. /*********************************************************************/
  128. int OpenDev(char *Dev)
  129. {
  130.          int     fd = open( Dev, O_RDWR );
  131.         //| O_NOCTTY | O_NDELAY         
  132.          if (-1 == fd)        
  133.          {                        
  134.                    perror("Can't Open Serial Port");
  135.                    return -1;            
  136.          }      
  137.          else  
  138.                    return fd;
  139. }
  140. void getcardinfo(char *buff){
  141.          int fd;
  142.          int nread,count=0;
  143.          char tempbuff[13];
  144.          char *dev  = "/dev/ttyS0"; //串口1
  145.          fd = OpenDev(dev);
  146.          set_speed(fd,9600);
  147.          if (set_Parity(fd,8,1,'N') == FALSE)  {
  148.                    printf("Set Parity Error\n");
  149.                    //return -1;
  150.          }
  151.          while (1) //循环读取数据
  152.          {  
  153.                    count=0;
  154.                    //sleep(5000);
  155.                    while(1)
  156.                    {
  157.                             if((nread = read(fd, tempbuff, 13))>0)
  158.                             {
  159.                             //printf("\nLen %d\n",nread);
  160.                                      memcpy(&buff[count],tempbuff,nread);
  161.                                      count+=nread;
  162.                             }
  163.                             if(count==13)
  164.                             {
  165.                                      buff[count+1] = '\0';  
  166.                             //printf( "\n%s", buff);
  167.                                      break;
  168.                             }
  169.                    }
  170.                    //break;
  171.          }
  172.          //return buff;
  173.          close(fd);
  174.          pthread_exit(NULL);
  175.          //close(fd); 
  176.          // exit (0);
  177. }
阅读(747) | 评论(0) | 转发(0) |
0

上一篇:socket

下一篇:多重继承

给主人留下些什么吧!~~