<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>

新聞中心

EEPW首頁(yè) > 嵌入式系統 > 設計應用 > Linux串口上網(wǎng)的程序實(shí)現方法

Linux串口上網(wǎng)的程序實(shí)現方法

作者: 時(shí)間:2011-05-10 來(lái)源:網(wǎng)絡(luò ) 收藏
如果打包前的數據中有END這個(gè)字符,那么使用ESC_END代替,如果發(fā)現有ESC這個(gè)字符,那么使用ESC_ESC字符替換。在環(huán)境下,名從ttyS0開(kāi)始依次是ttyS1、ttyS2等。在本中,使用ttyS0作為通信。在打開(kāi)ttyS0的時(shí)候,選項O_NOCTTY 表示不能把本當成控制終端,否則用戶(hù)的鍵盤(pán)輸入信息將影響的執行; O_NDELAY表示打開(kāi)串口的時(shí)候,并不關(guān)心另一端的串口是否在使用中。在中,打開(kāi)串口設備和打開(kāi)普通文件一樣,使用的是open()系統調用。比如我么打開(kāi)串口設備1也就是COM1,只需要:

本文引用地址:http://dyxdggzs.com/article/150775.htm

fd = open(/dev/ttyS0, O_RDWR | O_NOCTTY | O_NDELAY );

打開(kāi)的串口設備有很多設置選項。本文中使用int setup_com(int fd)設置。在系統頭文件termios.h>中定義了終端控制結構struct termios,tcgetattr()和tcsetattr()兩個(gè)系統函數獲得和設置這些屬性。結構struct termios中的域描述的主要屬性包括:

c_cflag : 控制選項

c_lflag : 線(xiàn)選項

c_iflag : 輸入選項

c_oflag :輸出選項

c_cc :控制字符

c_ispeed :輸入數據波特率

c_ospeed :輸出數據波特率

如果要設置某個(gè)選項,那么就使用|=運算,如果關(guān)閉某個(gè)選項就使用=和~運算。本文使用的各個(gè)選項的意義定義如下:

c_cflag: CLOCAL 本地模式,不改變端口的所有者

CREAD 表示使能數據接收器

PARENB 表示偶校驗

PARODD 表示奇校驗

CSTOPB 使用兩個(gè)停止位

CSIZE 對數據的bit使用掩碼

CS8 數據寬度是8bit

c_lflag: ICANON 使能規范輸入,否則使用原始數據(本文使用)

ECHO 回送(echo)輸入數據

ECHOE 回送擦除字符

ISIG 使能SIGINTR,SIGSUSP, SIGDSUSP和 SIGQUIT 信號

c_iflag: IXON 使能輸出軟件控制

IXOFF 使能輸入軟件控制

IXANY 允許任何字符再次開(kāi)啟數據流

INLCR 把字符NL(0A)映射到CR(0D)

IGNCR 忽略字符CR(0D)

ICRNL 把CR(0D)映射成字符NR(0A)

c_oflag: OPOST 輸出后處理,如果不設置表示原始數據(本文使用原始數據)

c_cc[VMIN]: 最少可讀數據

c_cc[VTIME]: 等待數據時(shí)間(10秒的倍數)

根據以上設置的定義,串口端口設置函數setup_com()定義如下:

int setup_com(int fd){

struct termios options;

tcgetattr(fd, options);

/* Set the baud rates to 38400...*/

cfsetispeed(options, B38400);

cfsetospeed(options, B38400);

/* Enable the receiver and set local mode...*/

options.c_cflag |= (CLOCAL | CREAD);

/* Set c_cflag options.*/

options.c_cflag |= PARENB;

options.c_cflag = ~PARODD;

options.c_cflag = ~CSTOPB;

options.c_cflag = ~CSIZE;

options.c_cflag |= CS8;

/* Set c_iflag input options */

options.c_iflag =~(IXON | IXOFF | IXANY);

options.c_iflag =~(INLCR | IGNCR | ICRNL);

options.c_lflag = ~(ICANON | ECHO | ECHOE | ISIG);

/* Set c_oflag output options */

options.c_oflag = ~OPOST;

/* Set the timeout options */

options.c_cc[VMIN] = 0;

options.c_cc[VTIME] = 10;

tcsetattr(fd, TCSANOW, options);

return 1;

}

兩個(gè)打包和拆包函數和SLIP協(xié)議定義的一樣,拆包函數和打包相反,這里不列舉了。

小結

本文描述的是一個(gè)非常簡(jiǎn)單的串口程序,如果需要可靠的通信,增加吞吐量,可在用戶(hù)空間添加適當的網(wǎng)絡(luò )控制協(xié)議,也可增加數據壓縮算法。

linux操作系統文章專(zhuān)題:linux操作系統詳解(linux不再難懂)

linux相關(guān)文章:linux教程



上一頁(yè) 1 2 3 4 5 下一頁(yè)

評論


相關(guān)推薦

技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>