歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux串口操作的一段代碼

Linux串口操作的一段代碼

日期:2017/3/1 10:08:29   编辑:Linux編程

Linux串口操作的一段代碼:

  1. /*
  2. * termio.c
  3. *
  4. * Created on: 2011-11-2
  5. * Author: jieen
  6. */
  7. #include <stdio.h> /*標准輸入輸出定義*/
  8. #include <stdlib.h> /*標准函數庫定義*/
  9. #include <unistd.h> /*Unix 標准函數定義*/
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <fcntl.h> /*文件控制定義*/
  13. #include <termios.h> /*PPSIX 終端控制定義*/
  14. #include <errno.h> /*錯誤號定義*/
  15. #if 0
  16. int main(void)
  17. {
  18. int fd;
  19. /*以讀寫方式打開串口*/
  20. fd = open( "/dev/ttyS0", O_RDWR);
  21. if (-1 == fd)
  22. {
  23. /* 不能打開串口一*/
  24. perror(" 提示錯誤!");
  25. }
  26. struct termios Opt;
  27. tcgetattr(fd, &Opt);
  28. printf("speed:%x\n",Opt.c_ispeed);
  29. cfsetispeed(&Opt,B115200); /*設置為115200Bps*/
  30. cfsetospeed(&Opt,B115200);
  31. tcsetattr(fd,TCANOW,&Opt);
  32. }
  33. #endif
  34. /**********************************************************************
  35. 代碼說明:使用串口二測試的,發送的數據是字符,
  36. 但是沒有發送字符串結束符號,所以接收到後,後面加上了結束符號。
  37. 我測試使用的是單片機發送數據到第二個串口,測試通過。
  38. **********************************************************************/
  39. #define FALSE -1
  40. #define TRUE 0
  41. /*********************************************************************/
  42. int OpenDev(char *Dev)
  43. {
  44. int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY
  45. if (-1 == fd)
  46. {
  47. perror("Can't Open Serial Port");
  48. return -1;
  49. }
  50. else
  51. return fd;
  52. }
  53. int main(int argc, char **argv)
  54. {
  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. /**
  88. *@brief 設置串口通信速率
  89. *@param fd 類型 int 打開串口的文件句柄
  90. *@param speed 類型 int 串口速度
  91. *@return void
  92. */
  93. void set_speed(int fd, int speed){
  94. int i;
  95. int status;
  96. struct termios Opt;
  97. int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
  98. B38400, B19200, B9600, B4800, B2400, B1200, B300, };
  99. int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,
  100. 19200, 9600, 4800, 2400, 1200, 300, };
  101. tcgetattr(fd, &Opt);
  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. {
  118. struct termios options;
  119. if ( tcgetattr( fd,&options) != 0) {
  120. perror("SetupSerial 1");
  121. return(FALSE);
  122. }
  123. options.c_cflag &= ~CSIZE;
  124. switch (databits) /*設置數據位數*/
  125. {
  126. case 7:
  127. options.c_cflag |= CS7;
  128. break;
  129. case 8:
  130. options.c_cflag |= CS8;
  131. break;
  132. default:
  133. fprintf(stderr,"Unsupported data size\n"); return (FALSE);
  134. }
  135. switch (parity)
  136. {
  137. case 'n':
  138. case 'N':
  139. options.c_cflag &= ~PARENB; /* Clear parity enable */
  140. options.c_iflag &= ~INPCK; /* Enable parity checking */
  141. break;
  142. case 'o':
  143. case 'O':
  144. options.c_cflag |= (PARODD | PARENB); /* 設置為奇效驗*/
  145. options.c_iflag |= INPCK; /* Disnable parity checking */
  146. break;
  147. case 'e':
  148. case 'E':
  149. options.c_cflag |= PARENB; /* Enable parity */
  150. options.c_cflag &= ~PARODD; /* 轉換為偶效驗*/
  151. options.c_iflag |= INPCK; /* Disnable parity checking */
  152. break;
  153. case 'S':
  154. case 's': /*as no parity*/
  155. options.c_cflag &= ~PARENB;
  156. options.c_cflag &= ~CSTOPB;break;
  157. default:
  158. fprintf(stderr,"Unsupported parity\n");
  159. return (FALSE);
  160. }
  161. /* 設置停止位*/
  162. switch (stopbits)
  163. {
  164. case 1:
  165. options.c_cflag &= ~CSTOPB;
  166. break;
  167. case 2:
  168. options.c_cflag |= CSTOPB;
  169. break;
  170. default:
  171. fprintf(stderr,"Unsupported stop bits\n");
  172. return (FALSE);
  173. }
  174. /* Set input parity option */
  175. if (parity != 'n')
  176. options.c_iflag |= INPCK;
  177. tcflush(fd,TCIFLUSH);
  178. options.c_cc[VTIME] = 150; /* 設置超時15 seconds*/
  179. options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
  180. if (tcsetattr(fd,TCSANOW,&options) != 0)
  181. {
  182. perror("SetupSerial 3");
  183. return (FALSE);
  184. }
  185. return (TRUE);
  186. }
Copyright © Linux教程網 All Rights Reserved