Linux串口传输文件


需要从FPGAm上传输文件到PC机上,下面是串口传输文件的小程序,可以测试下串口buffer的大小,我电脑上大概4K多,也可以测试串口寄存器大小读入读出大小,我这里是32bytes。

write.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 FALSE -1   
  15. #define TRUE 1   
  16.   
  17. int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,  
  18.         B38400, B19200, B9600, B4800, B2400, B1200, B300, };  
  19. int name_arr[] = {38400,  19200,  9600,  4800,  2400,  1200,  300,  
  20.         38400,  19200,  9600, 4800, 2400, 1200,  300, };  
  21.   
  22. void set_speed(int fd, int speed);  
  23. int set_Parity(int fd,int databits,int stopbits,int parity);  
  24.   
  25. int main(int argc,char *argv[])  
  26. {  
  27.     char *interface = NULL,*filename = NULL;  
  28.   
  29.     if (argc < 2)  
  30.     {  
  31.         interface = "/dev/ttyUSB0";  
  32.         filename = "hello.txt"  ;  
  33.     }  
  34.     else   
  35.     {  
  36.         interface = argv[1];  
  37.         filename = argv[2];   
  38.     }  
  39.   
  40.     int fd=open(interface,O_RDWR|O_NOCTTY|O_NDELAY);     
  41.     if(fd == -1)     
  42.     {     
  43.         printf("%s Open   Error!\n",interface);     
  44.         return -1;     
  45.     }   
  46.     printf("open %s successfully !",interface);  
  47.       
  48.     set_speed(fd,19200);  
  49.         if (set_Parity(fd,8,1,'N')== FALSE)  
  50.     {  
  51.         printf("Set Parity Error\n");  
  52.         exit(1);  
  53.     }  
  54.   
  55.   
  56.     int fp = open(filename,O_RDWR);  
  57.     if ( fp == -1 )  
  58.     {  
  59.         printf("open %s failured\n",filename);  
  60.         return -1;  
  61.     }  
  62.     int nread = 1;  
  63.     char buffer[BUFFER_SIZE] = {'\0'};  
  64.     int sum = 0;  
  65.   
  66.     while(nread > 0)  
  67.     {  
  68.         int nwrite = 0;  
  69.         int tmp = 0,pos=0;    
  70.         nread = read(fp,buffer,BUFFER_SIZE);    //一次要写进去的字节数   
  71.         printf("读取字节数 : %d bytes \n",nread);  
  72.         int size_write = nread;         //size_write是要本次要写入的总字节数       
  73.         while (size_write > 0)  
  74.         {     
  75.     //      int pos = 0>=nwrite?0:nwrite ;           //偏移   
  76. //          printf("pos = %d\n",pos);   
  77.             tmp = (nwrite > tmp)? nwrite : tmp;          //   
  78.             pos = tmp ;  
  79.             //printf("tmp = %d,pos %d \n",tmp,pos);   
  80.                 nwrite = write(fd,buffer + pos,size_write); //nwrite一次写进去的字节数          
  81.             if (nwrite == -1)  
  82.             {  
  83.                 sleep (2);  
  84.                 continue;  
  85.             }  
  86.             sum = sum + nwrite;         //nwrite 8   
  87.             printf("写入字节数 : %d,剩余字节数 : %d \n",sum,size_write);    
  88.             size_write = size_write-nwrite;     //剩下要写进去的字节数                   
  89.         }  
  90.     }  
  91.     printf("sussfully  write %s\n",filename);  
  92.     close(fp);  
  93.     close(fd);  
  94.     return 0;   
  95. }    
  96.   
  97. void set_speed(int fd, int speed)  
  98. {  
  99.     int   i;  
  100.     int   status;  
  101.     struct termios   Opt;  
  102.     tcgetattr(fd, &Opt);  
  103.     for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++)  
  104.     {  
  105.         if  (speed == name_arr[i])  
  106.         {  
  107.             tcflush(fd, TCIOFLUSH);  
  108.             cfsetispeed(&Opt, speed_arr[i]);  
  109.             cfsetospeed(&Opt, speed_arr[i]);  
  110.             status = tcsetattr(fd, TCSANOW, &Opt);  
  111.             if (status != 0)  
  112.             {  
  113.                 perror("tcsetattr fd1");  
  114.                 return;  
  115.             }  
  116.         }  
  117.         tcflush(fd,TCIOFLUSH);  
  118.     }  
  119. }  
  120.   
  121. int set_Parity(int fd,int databits,int stopbits,int parity)  
  122. {  
  123.     struct termios options;  
  124.     if (tcgetattr( fd,&options)  !=  0)  
  125.     {  
  126.         perror("SetupSerial 1");  
  127.         return(FALSE);  
  128.     }  
  129.     options.c_cflag &= ~CSIZE;  
  130.     switch (databits) /*设置数据位数*/  
  131.     {  
  132.         case 7:  
  133.             options.c_cflag |= CS7;  
  134.             break;  
  135.         case 8:  
  136.             options.c_cflag |= CS8;  
  137.             break;  
  138.         default:  
  139.             fprintf(stderr,"Unsupported data size\n");  
  140.             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;  
  164.             break;  
  165.         default:  
  166.             fprintf(stderr,"Unsupported parity\n");  
  167.             return (FALSE);  
  168.     }  
  169.     /* 设置停止位*/     
  170.     switch (stopbits)  
  171.     {  
  172.         case 1:  
  173.             options.c_cflag &= ~CSTOPB;  
  174.             break;  
  175.         case 2:  
  176.             options.c_cflag |= CSTOPB;  
  177.             break;  
  178.         default:  
  179.             fprintf(stderr,"Unsupported stop bits\n");  
  180.             return (FALSE);  
  181.     }  
  182.     /* Set input parity option */  
  183.     if (parity != 'n')  
  184.         options.c_iflag |= INPCK;  
  185.     options.c_cc[VTIME] = 150; // 15 seconds   
  186.     options.c_cc[VMIN] = 0;  
  187.   
  188.     tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */  
  189.     if (tcsetattr(fd,TCSANOW,&options) != 0)  
  190.     {  
  191.         perror("SetupSerial 3");  
  192.         return (FALSE);  
  193.     }  
  194.     return (TRUE);  
  195.  }  

  • 1
  • 2
  • 下一页

相关内容