歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux串口編程分析(三)

Linux串口編程分析(三)

日期:2017/2/25 10:38:39   编辑:Linux教程
 default:

  fprintf(stderr,"Unsupported parity\n");

  return (FALSE);

  }

  /* 設置停止位*/

  switch (stopbits) //這是設置停止位數,影響的標志是c_cflag

  {

  case 1:

  options.c_cflag &= ~CSTOPB; //不指明表示一位停止位

  break;

  case 2:

  options.c_cflag |= CSTOPB; //指明CSTOPB表示兩位,只有兩種可能

  break;

  default:

  fprintf(stderr,"Unsupported stop bits\n");

  return (FALSE);

  }

  /* Set input parity option */

  if (parity != 'n') //這是設置輸入是否進行校驗

  options.c_iflag |= INPCK;

  //這個地方是用來設置控制字符和超時參數的,一般默認即可。稍微要注意的是c_cc數組的VSTART 和 VSTOP 元素被設定成DC1 和 DC3,代表ASCII 標准的XON和XOFF字符。所以如果在傳輸這兩個字符的時候就傳不過去,這時需要把軟件流控制屏蔽 options.c_iflag &= ~(IXON | IXOFF | IXANY);

  options.c_cc[VTIME] = 150; // 15 seconds

  options.c_cc[VMIN] = 0;

  tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */ //刷新和立刻寫進去

  if (tcsetattr(fd,TCSANOW,&options) != 0)

  {

  perror("SetupSerial 3");

  return (FALSE);

  }

  return (TRUE);

  }

Copyright © Linux教程網 All Rights Reserved