Linux 串口
linux 操作系統(tǒng)從一開(kāi)始就對(duì)串行口提供了很好的支持,本文就 linux 下的串行口通訊編程進(jìn)行簡(jiǎn)單的介紹。 串口簡(jiǎn)介 串行口是計(jì)算機(jī)一種常用的接口,具有連接線少,通訊簡(jiǎn)單,得到廣泛的使用。常用的串口是 rs-232-c 接口(又稱 eia rs-232-c)它是在 1970 年由美國(guó)電子工業(yè)協(xié)會(huì)(eia)聯(lián)合貝爾系統(tǒng)、 調(diào)制解調(diào)器廠家及計(jì)算機(jī)終端生產(chǎn)廠家共同制定的用于串行通訊的標(biāo)準(zhǔn)。它的全名是"數(shù)據(jù)終端設(shè)備(dte)和數(shù)據(jù)通訊設(shè)備(dce)之間串行二進(jìn)制數(shù)據(jù)交換接口技術(shù)標(biāo)準(zhǔn)"該標(biāo)準(zhǔn)規(guī)定采用一個(gè) 25 個(gè)腳的 db25 連接器,對(duì)連接器的每個(gè)引腳的信號(hào)內(nèi)容加以規(guī)定,還對(duì)各種信號(hào)的電平加以規(guī)定。傳輸距離在碼元畸變小于 4 的情況下,傳輸電纜長(zhǎng)度應(yīng)為 50 英尺。 linux 操作系統(tǒng)從一開(kāi)始就對(duì)串行口提供了很好的支持,本文就 linux 下的串行口通訊編程進(jìn)行簡(jiǎn)單的介紹,如果要非常深入了解,建議看看本文所參考的《serial programming guide for posix operating systems》 計(jì)算機(jī)串口的引腳說(shuō)明 序號(hào) 信號(hào)名稱 符號(hào) 流向 功能 2 發(fā)送數(shù)據(jù) txd dte→dce dte發(fā)送串行數(shù)據(jù) 3 接收數(shù)據(jù) rxd dte←dce dte 接收串行數(shù)據(jù) 4 請(qǐng)求發(fā)送 rts dte→dce dte 請(qǐng)求 dce 將線路切換到發(fā)送方式 5 允許發(fā)送 cts dte←dce dce 告訴 dte 線路已接通可以發(fā)送數(shù)據(jù) 6 數(shù)據(jù)設(shè)備準(zhǔn)備好 dsr dte←dce dce 準(zhǔn)備好 7 信號(hào)地 信號(hào)公共地 8 載波檢測(cè) dcd dte←dce 表示 dce 接收到遠(yuǎn)程載波 20 數(shù)據(jù)終端準(zhǔn)備好 dtr dte→dce dte 準(zhǔn)備好 22 振鈴指示 ri dte←dce 表示 dce 與線路接通,出現(xiàn)振鈴 串口操作 串口操作需要的頭文件 #include <stdio.h> /*標(biāo)準(zhǔn)輸入輸出定義*/ #include <stdlib.h> /*標(biāo)準(zhǔn)函數(shù)庫(kù)定義*/ #include <unistd.h> /*unix 標(biāo)準(zhǔn)函數(shù)定義*/ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> /*文件控制定義*/ #include <termios.h> /*ppsix 終端控制定義*/ #include <errno.h> /*錯(cuò)誤號(hào)定義*/ 打開(kāi)串口 在 linux 下串口文件是位于 /dev 下的 串口一 為 /dev/ttys0 串口二 為 /dev/ttys1 打開(kāi)串口是通過(guò)使用標(biāo)準(zhǔn)的文件打開(kāi)函數(shù)操作: int fd; /*以讀寫(xiě)方式打開(kāi)串口*/ fd = open( "/dev/ttys0", o_rdwr); if (-1 == fd) 設(shè)置串口 最基本的設(shè)置串口包括波特率設(shè)置,效驗(yàn)位和停止位設(shè)置。 串口的設(shè)置主要是設(shè)置 struct termios 結(jié)構(gòu)體的各成員值。 struct termio ; 設(shè)置這個(gè)結(jié)構(gòu)體很復(fù)雜,我這里就只說(shuō)說(shuō)常見(jiàn)的一些設(shè)置: 波特率設(shè)置 下面是修改波特率的代碼: struct termios opt; tcgetattr(fd, &opt); cfsetispeed(&opt,b19200); /*設(shè)置為19200bps*/ cfsetospeed(&opt,b19200); tcsetattr(fd,tcanow,&opt); 設(shè)置波特率的例子函數(shù): /** *@brief 設(shè)置串口通信速率 *@param fd 類型 int 打開(kāi)串口的文件句柄 *@param speed 類型 int 串口速度 *@return void */ int speed_arr[] = ; int name_arr[] = ; void set_speed(int fd, int speed){ int i; int status; struct termios opt; tcgetattr(fd, &opt); for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i ) { if (speed == name_arr) { tcflush(fd, tcioflush); cfsetispeed(&opt, speed_arr); cfsetospeed(&opt, speed_arr); status = tcsetattr(fd1, tcsanow, &opt); if (status != 0) tcflush(fd,tcioflush); } } } 效驗(yàn)位和停止位的設(shè)置: 無(wú)效驗(yàn) 8位 option.c_cflag &= ~parenb; option.c_cflag &= ~cstopb; option.c_cflag &= ~csize; option.c_cflag |= ~cs8; 奇效驗(yàn)(odd) 7位 option.c_cflag |= ~parenb; option.c_cflag &= ~parodd; option.c_cflag &= ~cstopb; option.c_cflag &= ~csize; option.c_cflag |= ~cs7; 偶效驗(yàn)(even) 7位 option.c_cflag &= ~parenb; option.c_cflag |= ~parodd; option.c_cflag &= ~cstopb; option.c_cflag &= ~csize; option.c_cflag |= ~cs7; space效驗(yàn) 7位 option.c_cflag &= ~parenb; option.c_cflag &= ~cstopb; option.c_cflag &= &~csize; option.c_cflag |= cs8; 設(shè)置效驗(yàn)的函數(shù): /** *@brief 設(shè)置串口數(shù)據(jù)位,停止位和效驗(yàn)位 *@param fd 類型 int 打開(kāi)的串口文件句柄 *@param databits 類型 int 數(shù)據(jù)位 取值 為 7 或者8 *@param stopbits 類型 int 停止位 取值為 1 或者2 *@param parity 類型 int 效驗(yàn)類型 取值為n,e,o,,s */ int set_parity(int fd,int databits,int stopbits,int parity) options.c_cflag &= ~csize; switch (databits) /*設(shè)置數(shù)據(jù)位數(shù)*/ switch (parity) /* 設(shè)置停止位*/ switch (stopbits) /* set input parity option */ if (parity != "n") options.c_iflag |= inpck; tcflush(fd,tciflush); options.c_cc[vtime] = 150; /* 設(shè)置超時(shí)15 seconds*/ options.c_cc[vmin] = 0; /* up-date the options and do it now */ if (tcsetattr(fd,tcsanow,&options) != 0) return (true); } 需要注意的是: 如果不是開(kāi)發(fā)終端之類的,只是串口傳輸數(shù)據(jù),而不需要串口來(lái)處理,那么使用原始模式(raw mode)方式來(lái)通訊,設(shè)置方式如下: options.c_lflag &= ~(icanon | echo | echoe | isig); /*input*/ options.c_oflag &= ~opost; /*output*/ 讀寫(xiě)串口 設(shè)置好串口之后,讀寫(xiě)串口就很容易了,把串口當(dāng)作文件讀寫(xiě)就是。 發(fā)送數(shù)據(jù) char buffer[1024];int length;int nbyte;nbyte = write(fd, buffer ,length) 讀取串口數(shù)據(jù) 使用文件操作read函數(shù)讀取,如果設(shè)置為原始模式(raw mode)傳輸數(shù)據(jù),那么read函數(shù)返回的字符數(shù)是實(shí)際串口收到的字符數(shù)。 可以使用操作文件的函數(shù)來(lái)實(shí)現(xiàn)異步讀取,如fcntl,或者se-lect等來(lái)操作。 char buff[1024];int len;int readbyte = read(fd,buff,len); 關(guān)閉串口 關(guān)閉串口就是關(guān)閉文件。 close(fd); 例子 下面是一個(gè)簡(jiǎn)單的讀取串口數(shù)據(jù)的例子,使用了上面定義的一些函數(shù)和頭文件 /**********************************************************************代碼說(shuō)明:使用串口二測(cè)試的,發(fā)送的數(shù)據(jù)是字符, 但是沒(méi)有發(fā)送字符串結(jié)束符號(hào),所以接收到后,后面加上了結(jié)束符號(hào)。我測(cè)試使用的是單片機(jī)發(fā)送數(shù)據(jù)到第二個(gè)串口,測(cè)試通過(guò)。 **********************************************************************/ #define false -1 #define true 0 /*********************************************************************/ int opendev(char *dev) else return fd; } int main(int argc, char **argv) while (1) //循環(huán)讀取數(shù)據(jù) } //close(fd); // exit (0); } |