手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux串口传输文件

[日期:2012-06-18] 来源:Linux社区  作者:firefoxbug [字体:大 中 小]

read.c

 
  1. #include   <stdlib.h>              
  2. #include   <stdio.h>   
  3. #include   <unistd.h>      
  4. #include  <string.h>         
  5. #include   <sys/types.h>    
  6. #include   <sys/stat.h>    
  7. #include   <fcntl.h>                
  8. #include   <termios.h>            
  9. #include   <errno.h>                
  10. #include   <sys/time.h>    
  11. #include   <time.h>    
  12.   
  13. #define BUFFER_SIZE 16   
  14. #define MAX 1024   
  15. #define FALSE -1   
  16. #define TRUE 1   
  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,  
  21.         38400,  19200,  9600, 4800, 2400, 1200,  300, };  
  22.   
  23. void set_speed(int fd, int speed);  
  24. int set_Parity(int fd,int databits,int stopbits,int parity);  
  25.   
  26. int main(int argc,char *argv[])  
  27. {  
  28.     char *interface = NULL,*filename = NULL;  
  29.   
  30.     if (argc < 2)  
  31.     {  
  32.         interface = "/dev/ttyACM0";  
  33.         filename = "hello.txt"  ;  
  34.     }  
  35.     else   
  36.     {  
  37.         interface = argv[1];  
  38.         filename = argv[2];   
  39.     }  
  40.    
  41.     int fd=open(interface,O_RDWR|O_NOCTTY|O_NDELAY);     
  42.     if(fd == -1)     
  43.     {     
  44.         printf("%s Open   Error!\n",interface);     
  45.         return -1;     
  46.     }   
  47.     printf("open %s successfully !",interface);  
  48.       
  49.     set_speed(fd,19200);  
  50.     if (set_Parity(fd,8,1,'N')== FALSE)  
  51.     {  
  52.         printf("Set Parity Error\n");  
  53.         exit(1);  
  54.     }  
  55.   
  56.     int fp = open(filename,O_RDWR|O_CREAT);  
  57.     if ( fp == -1 )  
  58.     {  
  59.         printf("open %s failured\n",filename);  
  60.         return -1;  
  61.     }  
  62.   
  63.     int nread = 1,nwrite = 0;  
  64.     char buffer[BUFFER_SIZE] = {'\0'};  
  65.     while(1)  
  66.     {  
  67.         nread = read(fd,buffer,BUFFER_SIZE);     
  68.         if (nread > 0)  
  69.         {  
  70. //          printf("读取 %d bytes \n",nread);   
  71.             nwrite = write(fp,buffer,nread);      
  72. //          printf("写入 %d bytes \n",nwrite);   
  73.             if (nwrite <= 0)  
  74.             {         
  75.                 printf("write into file finished n");  
  76.                 break;  
  77.             }  
  78.         }  
  79.     }  
  80.   
  81.     printf("sussfully get %s\n",filename);  
  82.     return 0;   
  83. }    
  84.   
  85. void set_speed(int fd, int speed)  
  86. {  
  87.     int   i;  
  88.     int   status;  
  89.     struct termios   Opt;  
  90.     tcgetattr(fd, &Opt);  
  91.     for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++)  
  92.     {  
  93.         if  (speed == name_arr[i])  
  94.         {  
  95.             tcflush(fd, TCIOFLUSH);  
  96.             cfsetispeed(&Opt, speed_arr[i]);  
  97.             cfsetospeed(&Opt, speed_arr[i]);  
  98.             status = tcsetattr(fd, TCSANOW, &Opt);  
  99.             if (status != 0)  
  100.             {  
  101.                 perror("tcsetattr fd1");  
  102.                 return;  
  103.             }  
  104.         }  
  105.         tcflush(fd,TCIOFLUSH);  
  106.     }  
  107. }  
  108.   
  109. int set_Parity(int fd,int databits,int stopbits,int parity)  
  110. {  
  111.     struct termios options;  
  112.     if (tcgetattr( fd,&options)  !=  0)  
  113.     {  
  114.         perror("SetupSerial 1");  
  115.         return(FALSE);  
  116.     }  
  117.     options.c_cflag &= ~CSIZE;  
  118.     switch (databits) /*设置数据位数*/  
  119.     {  
  120.         case 7:  
  121.             options.c_cflag |= CS7;  
  122.             break;  
  123.         case 8:  
  124.             options.c_cflag |= CS8;  
  125.             break;  
  126.         default:  
  127.             fprintf(stderr,"Unsupported data size\n");  
  128.             return (FALSE);  
  129.     }  
  130.     switch (parity)  
  131.     {  
  132.         case 'n':  
  133.         case 'N':  
  134.             options.c_cflag &= ~PARENB;   /* Clear parity enable */  
  135.             options.c_iflag &= ~INPCK;     /* Enable parity checking */  
  136.             break;  
  137.         case 'o':  
  138.         case 'O':  
  139.             options.c_cflag |= (PARODD | PARENB);  /* 设置为奇效验*/   
  140.             options.c_iflag |= INPCK;             /* Disnable parity checking */  
  141.             break;  
  142.         case 'e':  
  143.         case 'E':  
  144.             options.c_cflag |= PARENB;     /* Enable parity */  
  145.             options.c_cflag &= ~PARODD;   /* 转换为偶效验*/    
  146.             options.c_iflag |= INPCK;       /* Disnable parity checking */  
  147.             break;  
  148.         case 'S':  
  149.         case 's':  /*as no parity*/  
  150.             options.c_cflag &= ~PARENB;  
  151.             options.c_cflag &= ~CSTOPB;  
  152.             break;  
  153.         default:  
  154.             fprintf(stderr,"Unsupported parity\n");  
  155.             return (FALSE);  
  156.     }  
  157.     /* 设置停止位*/     
  158.     switch (stopbits)  
  159.     {  
  160.         case 1:  
  161.             options.c_cflag &= ~CSTOPB;  
  162.             break;  
  163.         case 2:  
  164.             options.c_cflag |= CSTOPB;  
  165.             break;  
  166.         default:  
  167.             fprintf(stderr,"Unsupported stop bits\n");  
  168.             return (FALSE);  
  169.     }  
  170.     /* Set input parity option */  
  171.     if (parity != 'n')  
  172.         options.c_iflag |= INPCK;  
  173.     options.c_cc[VTIME] = 150; // 15 seconds   
  174.     options.c_cc[VMIN] = 0;  
  175.   
  176.     tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */  
  177.     if (tcsetattr(fd,TCSANOW,&options) != 0)  
  178.     {  
  179.         perror("SetupSerial 3");  
  180.         return (FALSE);  
  181.     }  
  182.     return (TRUE);  
  183.  }  

linux
相关资讯       Linux编程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款