Linux串口操作的一段代码


Linux串口操作的一段代码:

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

相关内容