歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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. { 
  22.     int fd; 
  23.     /*以讀寫方式打開串口*/ 
  24.     fd = open( "/dev/ttyS0", O_RDWR); 
  25.     if (-1 == fd) 
  26.     { 
  27.         /* 不能打開串口一*/ 
  28.         perror(" 提示錯誤!"); 
  29.     } 
  30.     struct  termios Opt; 
  31.     tcgetattr(fd, &Opt); 
  32.     printf("speed:%x\n",Opt.c_ispeed); 
  33.     cfsetispeed(&Opt,B115200);     /*設置為115200Bps*/ 
  34.     cfsetospeed(&Opt,B115200); 
  35.     tcsetattr(fd,TCANOW,&Opt); 
  36.  
  37. } 
  38. #endif  
  39. /********************************************************************** 
  40. 代碼說明:使用串口二測試的,發送的數據是字符, 
  41. 但是沒有發送字符串結束符號,所以接收到後,後面加上了結束符號。 
  42. 我測試使用的是單片機發送數據到第二個串口,測試通過。 
  43. **********************************************************************/ 
  44. #define FALSE  -1  
  45. #define TRUE   0  
  46. /*********************************************************************/ 
  47. int OpenDev(char *Dev) 
  48. { 
  49.         int     fd = open( Dev, O_RDWR );         //| O_NOCTTY | O_NDELAY  
  50.         if (-1 == fd) 
  51.         { 
  52.                 perror("Can't Open Serial Port"); 
  53.                 return -1; 
  54.         } 
  55.         else 
  56.                 return fd; 
  57. } 
  58. int main(int argc, char **argv) 
  59. { 
  60.         int fd; 
  61.         int nread; 
  62.         char *dev; 
  63.         char buff[512]; 
  64.         dev = (char *)malloc(15); 
  65.         if(argc < 2) 
  66.         { 
  67.             strcpy(dev,"/dev/ttyS0"); //串口一  
  68.         }else 
  69.           { 
  70.             strncpy(dev,argv[1],strlen(argv[1])); 
  71.           } 
  72.         fd = OpenDev(dev); 
  73.         if(fd == -1) 
  74.           exit(EXIT_FAILURE); 
  75.         set_speed(fd,115200); 
  76.         if (set_Parity(fd,8,1,'N') == FALSE)  { 
  77.                 printf("Set Parity Error\n"); 
  78.                 exit (0); 
  79.         } 
  80.         while (1) //循環讀取數據  
  81.         { 
  82.           while((nread = read(fd, buff, 512))>0) 
  83.           { 
  84.                   printf("\nLen %d\n",nread); 
  85.                   buff[nread+1] = '\0'; 
  86.                   printf( "\n%s", buff); 
  87.           } 
  88.         } 
  89.         //close(fd);  
  90.         // exit (0);  
  91. } 
  92. /** 
  93. *@brief  設置串口通信速率 
  94. *@param  fd     類型 int  打開串口的文件句柄 
  95. *@param  speed  類型 int  串口速度 
  96. *@return  void 
  97. */ 
  98. void set_speed(int fd, int speed){ 
  99.   int   i; 
  100.   int   status; 
  101.   struct termios   Opt; 
  102.   int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300, 
  103.             B38400, B19200, B9600, B4800, B2400, B1200, B300, }; 
  104.   int name_arr[] = {38400,  19200,  9600,  4800,  2400,  1200,  300, 38400, 
  105.             19200,  9600, 4800, 2400, 1200,  300, }; 
  106.   tcgetattr(fd, &Opt); 
  107.  
  108.   for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++) { 
  109.     if  (speed == name_arr[i]) { 
  110.       tcflush(fd, TCIOFLUSH); 
  111.       cfsetispeed(&Opt, speed_arr[i]); 
  112.       cfsetospeed(&Opt, speed_arr[i]); 
  113.       status = tcsetattr(fd, TCSANOW, &Opt); 
  114.       if  (status != 0) { 
  115.         perror("tcsetattr fd"); 
  116.         return; 
  117.       } 
  118.       tcflush(fd,TCIOFLUSH); 
  119.     } 
  120.   } 
  121. } 
  122.  
  123. int set_Parity(int fd,int databits,int stopbits,int parity) 
  124. { 
  125.         struct termios options; 
  126.         if  ( tcgetattr( fd,&options)  !=  0) { 
  127.                 perror("SetupSerial 1"); 
  128.                 return(FALSE); 
  129.         } 
  130.         options.c_cflag &= ~CSIZE; 
  131.         switch (databits) /*設置數據位數*/ 
  132.         { 
  133.         case 7: 
  134.                 options.c_cflag |= CS7; 
  135.                 break; 
  136.         case 8: 
  137.                 options.c_cflag |= CS8; 
  138.                 break; 
  139.         default: 
  140.                 fprintf(stderr,"Unsupported data size\n"); return (FALSE); 
  141.         } 
  142.   switch (parity) 
  143.   { 
  144.           case 'n': 
  145.           case 'N': 
  146.                   options.c_cflag &= ~PARENB;   /* Clear parity enable */ 
  147.                   options.c_iflag &= ~INPCK;     /* Enable parity checking */ 
  148.                   break; 
  149.           case 'o': 
  150.           case 'O': 
  151.                   options.c_cflag |= (PARODD | PARENB); /* 設置為奇效驗*/ 
  152.                   options.c_iflag |= INPCK;             /* Disnable parity checking */ 
  153.                   break; 
  154.           case 'e': 
  155.           case 'E': 
  156.                   options.c_cflag |= PARENB;     /* Enable parity */ 
  157.                   options.c_cflag &= ~PARODD;   /* 轉換為偶效驗*/ 
  158.                   options.c_iflag |= INPCK;       /* Disnable parity checking */ 
  159.                   break; 
  160.           case 'S': 
  161.           case 's':  /*as no parity*/ 
  162.               options.c_cflag &= ~PARENB; 
  163.                   options.c_cflag &= ~CSTOPB;break; 
  164.           default: 
  165.                   fprintf(stderr,"Unsupported parity\n"); 
  166.                   return (FALSE); 
  167.           } 
  168.   /* 設置停止位*/ 
  169.   switch (stopbits) 
  170.   { 
  171.           case 1: 
  172.                   options.c_cflag &= ~CSTOPB; 
  173.                   break; 
  174.           case 2: 
  175.                   options.c_cflag |= CSTOPB; 
  176.              break; 
  177.           default: 
  178.                    fprintf(stderr,"Unsupported stop bits\n"); 
  179.                    return (FALSE); 
  180.   } 
  181.   /* Set input parity option */ 
  182.   if (parity != 'n') 
  183.           options.c_iflag |= INPCK; 
  184.   tcflush(fd,TCIFLUSH); 
  185.   options.c_cc[VTIME] = 150; /* 設置超時15 seconds*/ 
  186.   options.c_cc[VMIN] = 0; /* Update the options and do it NOW */ 
  187.   if (tcsetattr(fd,TCSANOW,&options) != 0) 
  188.   { 
  189.           perror("SetupSerial 3"); 
  190.           return (FALSE); 
  191.   } 
  192.   return (TRUE); 
  193. } 
Copyright © Linux教程網 All Rights Reserved