linux 串口編程記錄(一)串口參數設置


    一些常用串口屬性的設置方法。

  • 設置流控制
  • termios_new.c_cflag &= ~CRTSCTS; //不使用流控制
  • termios_new.c_cflag |= CRTSCTS; //使用硬件流控制
  • termios_new.c_iflag |= IXON|IXOFF|IXANY; //使用軟件流控制

 

  • 屏蔽字符大小位
  • termios_new.c_cflag &= ~CSIZE;

 

  • 設置數據位大小
  • termios_new.c_cflag |= CS8; //使用8位數據位
  • termios_new.c_cflag |= CS7; //使用7位數據位
  • termios_new.c_cflag |= CS6; //使用6位數據位
  • termios_new.c_cflag |= CS5; //使用5位數據位

 

  • 設置奇偶校驗方式
  • termios_new.c_cflag &= ~PARENB; //無奇偶校驗
  •  
  • termios_new.c_cflag |= PARENB; //奇校驗
  • termios_new.c_cflag &= ~PARODD;
  •  
  • termios_new.c_cflag |= PARENB; //偶校驗
  • termios_new.c_cflag &= ~PARODD;

 

  • 停止位
  • termios_new.c_cflag |= CSTOPB; //2位停止位
  • termios_new.c_cflag &= ~CSTOPB; //1位停止位

 

  • 輸出模式
  • termios_new.c_cflag &= ~OPOST; //原始數據(RAW)輸出

 

  • 控制字符
  • termios_new.c_cc[VMIN] = 1; //讀取字符的最小數量
  • termios_new.c_cc[VTIME] = 1; //讀取第一個字符的等待時間

 

  • 關閉終端回顯,鍵盤輸入的字符不會在終端窗口顯示。
  • #include <stdio.h>
  • #include <stdlib.h>
  • #include <termios.h>
  • #include <unistd.h>
  •  
  • int main(void)
  • {
  • struct termios ts,ots;
  • char passbuf[1024];
  •  
  • tcgetattr(STDIN_FILENO,&ts); /* STDIN_FILENO的值是1,表示標准輸入的文件描述符 */
  • ots = ts;
  •  
  • ts.c_lflag &= ~ECHO; /* 關閉回終端回顯功能*/
  • ts.c_lflag |= ECHONL;
  • tcsetattr(STDIN_FILENO,TCSAFLUSH,&ts); /* 應用新終端設置 */
  •  
  • fgets(passbuf,1024,stdin); /* 輸入字符不會在終端顯示 */
  • printf("you input character = %s/n",passbuf);
  •  
  • tcsetattr(STDIN_FILENO,TCSANOW,&ots); /* 恢復舊的終端設備 */

}

 

例子:串口參數設置函數

int Serial::SetPara(int serialfd,int speed,int databits , int stopbits ,int parity )

{    

  struct termios termios_new;

     bzero( &termios_new, sizeof(termios_new));//等價於memset(&termios_new,sizeof(termios_new));

    cfmakeraw(&termios_new);//就是將終端設置為原始模式

    termios_new.c_cflag=BaudRate(speed);

    termios_new.c_cflag |= CLOCAL | CREAD;   //  termios_new.c_iflag = IGNPAR | IGNBRK;

    termios_new.c_cflag &= ~CSIZE;

    switch (databits)

    {

    case 0:

        termios_new.c_cflag |= CS5;

        break;

    case 1:

        termios_new.c_cflag |= CS6;

        break;

    case 2:

        termios_new.c_cflag |= CS7;

        break;

    case 3:

        termios_new.c_cflag |= CS8;

        break;

    default:

        termios_new.c_cflag |= CS8;

        break;

    }

    switch (parity)

    {

    case 0:      //as no parity

        termios_new.c_cflag &= ~PARENB;    //Clear parity enable

        break;

    case 1:

        termios_new.c_cflag |= PARENB;     // Enable parity

        termios_new.c_cflag &= ~PARODD;

        break;

    case 2:

        termios_new.c_cflag |= PARENB;

        termios_new.c_cflag |= ~PARODD;

        break;

    default:

        termios_new.c_cflag &= ~PARENB;   // Clear parity enable

        break;

    }

    switch (stopbits)// set Stop Bit

    {

    case 1:

        termios_new.c_cflag &= ~CSTOPB;

        break;

    case 2:

        termios_new.c_cflag |= CSTOPB;

        break;

    default:  

       termios_new.c_cflag &= ~CSTOPB;

        break;

    }

    tcflush(serialfd,TCIFLUSH); // 清除輸入緩存

   tcflush(serialfd,TCOFLUSH); // 清除輸出緩存

    termios_new.c_cc[VTIME] = 1;   // MIN與 TIME組合有以下四種:1.MIN = 0 , TIME =0  有READ立即回傳 否則傳回 0 ,不讀取任何字元

    termios_new.c_cc[VMIN] = 1;  //    2、 MIN = 0 , TIME >0  READ 傳回讀到的字元,或在十分之一秒后傳回TIME 若來不及讀到任何字元,則傳回0

    tcflush (serialfd, TCIFLUSH);  //    3、 MIN > 0 , TIME =0  READ 會等待,直到MIN字元可讀

    return tcsetattr(serialfd,TCSANOW,&termios_new);  //    4、 MIN > 0 , TIME > 0 每一格字元之間計時器即會被啟動 READ 會在讀到MIN字元,傳回值或

}

 

 

int Serial::BaudRate( int baudrate)
{
    switch(baudrate)
    {
    case 0:
        return (B2400);
    case 1:
        return (B4800);
    case 2:
        return (B9600);
    case 3:
        return (B19200);
    case 4:
        return (B38400);
    case 5:
        return (B57600);
    case 6:
        return (B115200);
    default:
        return (B9600);
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM