直播推薦
企業(yè)動態(tài)
- 紛享銷客發(fā)布首個企業(yè)級智能CRM平臺ShareAI
- 揭秘西企業(yè)數(shù)字化+低碳化轉(zhuǎn)型“工具箱”:西門子Xcelerator
- 企業(yè)AI賦能數(shù)智制造,用友U9 cloud世界級云ERP煥新升級
- 《“智“領(lǐng)石化,“質(zhì)“造未來——威圖石化行業(yè)數(shù)智化實踐白皮書》隆重發(fā)布
- 攜手共贏!德國Agfa搭載瑞典IPCO鋼帶,實現(xiàn)印刷設(shè)備振動銳減6倍,提升印刷速度與精度
- 創(chuàng)四方集團(tuán)榮獲“知名商標(biāo)品牌閃亮”證書,助力品牌戰(zhàn)略升級
- 皇冠CAD(CrownCAD)2025 R3版本來了,率先開啟C“Ai”D時代!
- 電費砍半!中國制冷展:海爾發(fā)布AI建筑最新成果
推薦展會
串口簡介
串行口是計算機(jī)一種常用的接口,具有連接線少,通訊簡單,得到廣泛的使用。常用的串口是 rs-232-c 接口(又稱 eia rs-232-c)它是在 1970 年由美國電子工業(yè)協(xié)會(eia)聯(lián)合貝爾系統(tǒng)、 調(diào)制解調(diào)器廠家及計算機(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ī)定采用一個 25 個腳的 db25 連接器,對連接器的每個引腳的信號內(nèi)容加以規(guī)定,還對各種信號的電平加以規(guī)定。傳輸距離在碼元畸變小于 4 的情況下,傳輸電纜長度應(yīng)為 50 英尺。
linux 操作系統(tǒng)從一開始就對串行口提供了很好的支持,本文就 linux 下的串行口通訊編程進(jìn)行簡單的介紹,如果要非常深入了解,建議看看本文所參考的《serial programming guide for posix operating systems》
計算機(jī)串口的引腳說明
序號 信號名稱 符號 流向 功能
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 接收到遠(yuǎn)程載波
20 數(shù)據(jù)終端準(zhǔn)備好 dtr dte→dce dte 準(zhǔn)備好
22 振鈴指示 ri dte←dce 表示 dce 與線路接通,出現(xiàn)振鈴
串口操作
串口操作需要的頭文件
#Include
#Include
#Include
#Include
#Include
#Include
#Include
#Include
打開串口
在 linux 下串口文件是位于 /dev 下的
串口一 為 /dev/ttys0
串口二 為 /dev/ttys1
打開串口是通過使用標(biāo)準(zhǔn)的文件打開函數(shù)操作:
int fd;
/*以讀寫方式打開串口*/
fd = open( "/dev/ttys0", o_rdwr);
if (-1 == fd){
/* 不能打開串口一*/
perror(" 提示錯誤!");
}
設(shè)置串口
zui基本的設(shè)置串口包括波特率設(shè)置,效驗位和停止位設(shè)置。
串口的設(shè)置主要是設(shè)置 struct termios 結(jié)構(gòu)體的各成員值。
struct termio
{ unsigned short c_iflag; /* 輸入模式標(biāo)志 */
unsigned short c_oflag; /* 輸出模式標(biāo)志 */
unsigned short c_cflag; /* 控制模式標(biāo)志*/
unsigned short c_lflag; /* local mode flags */
unsigned char c_line; /* line discipline */
unsigned char c_cc[ncc]; /* control characters */
};
設(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[] = { b38400, b19200, b9600, b4800, b2400, b1200, b300,
b38400, b19200, b9600, b4800, b2400, b1200, b300, };
int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,
19200, 9600, 4800, 2400, 1200, 300, };
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) {
perror("tcsetattr fd1");
return;
}
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)
{
struct termios options;
if ( tcgetattr( fd,&options) != 0) {
perror("setupserial 1");
return(false);
}
options.c_cflag &= ~csize;
switch (databits) /*設(shè)置數(shù)據(jù)位數(shù)*/
{
case 7:
options.c_cflag |= cs7;
break;
case 8:
options.c_cflag |= cs8;
break;
default:
fprintf(stderr,"unsupported data size\n"); return (false);
}
switch (parity)
{
case "n":
case "n":
options.c_cflag &= ~parenb; /* clear parity enable */
options.c_iflag &= ~inpck; /* enable parity checking */
break;
case "o":
case "o":
options.c_cflag |= (parodd | parenb); /* 設(shè)置為奇效驗*/
options.c_iflag |= inpck; /* disnable parity checking */
break;
case "e":
case "e":
options.c_cflag |= parenb; /* enable parity */
options.c_cflag &= ~parodd; /* 轉(zhuǎn)換為偶效驗*/
options.c_iflag |= inpck; /* disnable parity checking */
break;
case "s":
case "s": /*as no parity*/
options.c_cflag &= ~parenb;
options.c_cflag &= ~cstopb;break;
default:
fprintf(stderr,"unsupported parity\n");
return (false);
}
/* 設(shè)置停止位*/
switch (stopbits)
{
case 1:
options.c_cflag &= ~cstopb;
break;
case 2:
options.c_cflag |= cstopb;
break;
default:
fprintf(stderr,"unsupported stop bits\n");
return (false);
}
/* 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)
{
perror("setupserial 3");
return (false);
}
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é)束符號。我測試使用的是單片機(jī)發(fā)送數(shù)據(jù)到第二個串口,測試通過。
**********************************************************************/
#define false -1
#define true 0
/*********************************************************************/
int opendev(char *dev)
{
int fd = open( dev, o_rdwr ); //| o_noctty | o_ndelay
if (-1 == fd)
{
perror("can"t open serial port");
return -1;
}
else
return fd;
}
int main(int argc, char **argv){
int fd;
int nread;
char buff[512];
char *dev = "/dev/ttys1"; //串口二
fd = opendev(dev);
set_speed(fd,19200);
if (set_parity(fd,8,1,"n") == false) {
printf("set parity error\n");
exit (0);
}
while (1) //循環(huán)讀取數(shù)據(jù)
{
while((nread = read(fd, buff, 512))>0)
{
printf("\nlen d\n",nread);
buff[nread 1] = "\0";
printf( "\ns", buff);
}
}
//close(fd);
// exit (0);
}
免責(zé)聲明
- 凡本網(wǎng)注明"來源:智能制造網(wǎng)"的所有作品,版權(quán)均屬于智能制造網(wǎng),轉(zhuǎn)載請必須注明智能制造網(wǎng),http://towegas.com。違反者本網(wǎng)將追究相關(guān)法律責(zé)任。
- 企業(yè)發(fā)布的公司新聞、技術(shù)文章、資料下載等內(nèi)容,如涉及侵權(quán)、違規(guī)遭投訴的,一律由發(fā)布企業(yè)自行承擔(dān)責(zé)任,本網(wǎng)有權(quán)刪除內(nèi)容并追溯責(zé)任。
- 本網(wǎng)轉(zhuǎn)載并注明自其它來源的作品,目的在于傳遞更多信息,并不代表本網(wǎng)贊同其觀點或證實其內(nèi)容的真實性,不承擔(dān)此類作品侵權(quán)行為的直接責(zé)任及連帶責(zé)任。其他媒體、網(wǎng)站或個人從本網(wǎng)轉(zhuǎn)載時,必須保留本網(wǎng)注明的作品來源,并自負(fù)版權(quán)等法律責(zé)任。
- 如涉及作品內(nèi)容、版權(quán)等問題,請在作品發(fā)表之日起一周內(nèi)與本網(wǎng)聯(lián)系,否則視為放棄相關(guān)權(quán)利。
2025第十一屆中國國際機(jī)電產(chǎn)品交易會 暨先進(jìn)制造業(yè)博覽會
展會城市:合肥市展會時間:2025-09-20