亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1910 | 回復(fù): 1
打印 上一主題 下一主題

Linux下串口編程入門(二) [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2005-02-14 22:24 |只看該作者 |倒序瀏覽

設(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
效驗位和停止位的設(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 sizen"); 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 parityn");   
                return (FALSE);  
        }  
/* 設(shè)置停止位*/  
switch (stopbits)
{   
        case 1:   
                options.c_cflag &= ~CSTOPB;  
                break;  
        case 2:   
                options.c_cflag |= CSTOPB;  
           break;
        default:   
                 fprintf(stderr,"Unsupported stop bitsn");  
                 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; /* Update 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*/


本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u/3166/showart_12108.html

論壇徽章:
0
2 [報告]
發(fā)表于 2014-09-28 14:33 |只看該作者
樓主講得聽詳細。。OK
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP