歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux串口傳輸文件

Linux串口傳輸文件

日期:2017/3/1 10:19:23   编辑: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. #define BUFFER_SIZE 16
  13. #define FALSE -1
  14. #define TRUE 1
  15. int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
  16. B38400, B19200, B9600, B4800, B2400, B1200, B300, };
  17. int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300,
  18. 38400, 19200, 9600, 4800, 2400, 1200, 300, };
  19. void set_speed(int fd, int speed);
  20. int set_Parity(int fd,int databits,int stopbits,int parity);
  21. int main(int argc,char *argv[])
  22. {
  23. char *interface = NULL,*filename = NULL;
  24. if (argc < 2)
  25. {
  26. interface = "/dev/ttyUSB0";
  27. filename = "hello.txt" ;
  28. }
  29. else
  30. {
  31. interface = argv[1];
  32. filename = argv[2];
  33. }
  34. int fd=open(interface,O_RDWR|O_NOCTTY|O_NDELAY);
  35. if(fd == -1)
  36. {
  37. printf("%s Open Error!\n",interface);
  38. return -1;
  39. }
  40. printf("open %s successfully !",interface);
  41. set_speed(fd,19200);
  42. if (set_Parity(fd,8,1,'N')== FALSE)
  43. {
  44. printf("Set Parity Error\n");
  45. exit(1);
  46. }
  47. int fp = open(filename,O_RDWR);
  48. if ( fp == -1 )
  49. {
  50. printf("open %s failured\n",filename);
  51. return -1;
  52. }
  53. int nread = 1;
  54. char buffer[BUFFER_SIZE] = {'\0'};
  55. int sum = 0;
  56. while(nread > 0)
  57. {
  58. int nwrite = 0;
  59. int tmp = 0,pos=0;
  60. nread = read(fp,buffer,BUFFER_SIZE); //一次要寫進去的字節數
  61. printf("讀取字節數 : %d bytes \n",nread);
  62. int size_write = nread; //size_write是要本次要寫入的總字節數
  63. while (size_write > 0)
  64. {
  65. // int pos = 0>=nwrite?0:nwrite ; //偏移
  66. // printf("pos = %d\n",pos);
  67. tmp = (nwrite > tmp)? nwrite : tmp; //
  68. pos = tmp ;
  69. //printf("tmp = %d,pos %d \n",tmp,pos);
  70. nwrite = write(fd,buffer + pos,size_write); //nwrite一次寫進去的字節數
  71. if (nwrite == -1)
  72. {
  73. sleep (2);
  74. continue;
  75. }
  76. sum = sum + nwrite; //nwrite 8
  77. printf("寫入字節數 : %d,剩余字節數 : %d \n",sum,size_write);
  78. size_write = size_write-nwrite; //剩下要寫進去的字節數
  79. }
  80. }
  81. printf("sussfully write %s\n",filename);
  82. close(fp);
  83. close(fd);
  84. return 0;
  85. }
  86. void set_speed(int fd, int speed)
  87. {
  88. int i;
  89. int status;
  90. struct termios Opt;
  91. tcgetattr(fd, &Opt);
  92. for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
  93. {
  94. if (speed == name_arr[i])
  95. {
  96. tcflush(fd, TCIOFLUSH);
  97. cfsetispeed(&Opt, speed_arr[i]);
  98. cfsetospeed(&Opt, speed_arr[i]);
  99. status = tcsetattr(fd, TCSANOW, &Opt);
  100. if (status != 0)
  101. {
  102. perror("tcsetattr fd1");
  103. return;
  104. }
  105. }
  106. tcflush(fd,TCIOFLUSH);
  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. tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
  176. if (tcsetattr(fd,TCSANOW,&options) != 0)
  177. {
  178. perror("SetupSerial 3");
  179. return (FALSE);
  180. }
  181. return (TRUE);
  182. }

Copyright © Linux教程網 All Rights Reserved