Linux 串口
linux 操作系統(tǒng)從一開始就對串行口提供了很好的支持,本文就 linux 下的串行口通訊編程進行簡單的介紹。 串口簡介 串行口是計算機一種常用的接口,具有連接線少,通訊簡單,得到廣泛的使用。常用的串口是 rs-232-c 接口(又稱 eia rs-232-c)它是在 1970 年由美國電子工業(yè)協(xié)會(eia)聯(lián)合貝爾系統(tǒng)、 調(diào)制解調(diào)器廠家及計算機終端生產(chǎn)廠家共同制定的用于串行通訊的標(biāo)準(zhǔn)。它的全名是"數(shù)據(jù)終端設(shè)備(dte)和數(shù)據(jù)通訊設(shè)備(dce)之間串行二進制數(shù)據(jù)交換接口技術(shù)標(biāo)準(zhǔn)"該標(biāo)準(zhǔn)規(guī)定采用一個 25 個腳的 db25 連接器,對連接器的每個引腳的信號內(nèi)容加以規(guī)定,還對各種信號的電平加以規(guī)定。傳輸距離在碼元畸變小于 4 的情況下,傳輸電纜長度應(yīng)為 50 英尺。 linux 操作系統(tǒng)從一開始就對串行口提供了很好的支持,本文就 linux 下的串行口通訊編程進行簡單的介紹,如果要非常深入了解,建議看看本文所參考的《serial programming guide for posix operating systems》 計算機串口的引腳說明 序號 信號名稱 符號 流向 功能 2 發(fā)送數(shù)據(jù) txd dte→dce dte發(fā)送串行數(shù)據(jù) 3 接收數(shù)據(jù) rxd dte←dce dte 接收串行數(shù)據(jù) 4 請求發(fā)送 rts dte→dce dte 請求 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 信號地 信號公共地 8 載波檢測 dcd dte←dce 表示 dce 接收到遠程載波 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ù)庫定義*/ #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> /*錯誤號定義*/ 打開串口 在 linux 下串口文件是位于 /dev 下的 串口一 為 /dev/ttys0 串口二 為 /dev/ttys1 打開串口是通過使用標(biāo)準(zhǔn)的文件打開函數(shù)操作: int fd; /*以讀寫方式打開串口*/ fd = open( "/dev/ttys0", o_rdwr); if (-1 == fd) 設(shè)置串口 最基本的設(shè)置串口包括波特率設(shè)置,效驗位和停止位設(shè)置。 串口的設(shè)置主要是設(shè)置 struct termios 結(jié)構(gòu)體的各成員值。 struct termio ; 設(shè)置這個結(jié)構(gòu)體很復(fù)雜,我這里就只說說常見的一些設(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 打開串口的文件句柄 *@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); } } } 效驗位和停止位的設(shè)置: 無效驗 8位 option.c_cflag &= ~parenb; option.c_cflag &= ~cstopb; option.c_cflag &= ~csize; option.c_cflag |= ~cs8; 奇效驗(odd) 7位 option.c_cflag |= ~parenb; option.c_cflag &= ~parodd; option.c_cflag &= ~cstopb; option.c_cflag &= ~csize; option.c_cflag |= ~cs7; 偶效驗(even) 7位 option.c_cflag &= ~parenb; option.c_cflag |= ~parodd; option.c_cflag &= ~cstopb; option.c_cflag &= ~csize; option.c_cflag |= ~cs7; space效驗 7位 option.c_cflag &= ~parenb; option.c_cflag &= ~cstopb; option.c_cflag &= &~csize; option.c_cflag |= cs8; 設(shè)置效驗的函數(shù): /** *@brief 設(shè)置串口數(shù)據(jù)位,停止位和效驗位 *@param fd 類型 int 打開的串口文件句柄 *@param databits 類型 int 數(shù)據(jù)位 取值 為 7 或者8 *@param stopbits 類型 int 停止位 取值為 1 或者2 *@param parity 類型 int 效驗類型 取值為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è)置超時15 seconds*/ options.c_cc[vmin] = 0; /* up-date the options and do it now */ if (tcsetattr(fd,tcsanow,&options) != 0) return (true); } 需要注意的是: 如果不是開發(fā)終端之類的,只是串口傳輸數(shù)據(jù),而不需要串口來處理,那么使用原始模式(raw mode)方式來通訊,設(shè)置方式如下: options.c_lflag &= ~(icanon | echo | echoe | isig); /*input*/ options.c_oflag &= ~opost; /*output*/ 讀寫串口 設(shè)置好串口之后,讀寫串口就很容易了,把串口當(dāng)作文件讀寫就是。 發(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ù)來實現(xiàn)異步讀取,如fcntl,或者se-lect等來操作。 char buff[1024];int len;int readbyte = read(fd,buff,len); 關(guān)閉串口 關(guān)閉串口就是關(guān)閉文件。 close(fd); 例子 下面是一個簡單的讀取串口數(shù)據(jù)的例子,使用了上面定義的一些函數(shù)和頭文件 /**********************************************************************代碼說明:使用串口二測試的,發(fā)送的數(shù)據(jù)是字符, 但是沒有發(fā)送字符串結(jié)束符號,所以接收到后,后面加上了結(jié)束符號。我測試使用的是單片機發(fā)送數(shù)據(jù)到第二個串口,測試通過。 **********************************************************************/ #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); } |