学而不思则罔,思而不学则殆

有其事必有其理, 有其理必有其事

  IT博客 :: 首页 :: 联系 :: 聚合  :: 管理
  85 Posts :: 12 Stories :: 47 Comments :: 0 Trackbacks
  1#include     <stdio.h>      /*标准输入输出定义*/
  2#include     <stdlib.h>     /*标准函数库定义*/
  3#include     <unistd.h>     /*Unix标准函数定义*/
  4#include     <sys/types.h>  /**/
  5#include     <sys/stat.h>   /**/
  6#include     <fcntl.h>      /*文件控制定义*/
  7#include     <termios.h>    /*PPSIX终端控制定义*/
  8#include     <errno.h>      /*错误号定义*/
  9
 10
 11
 12/***@brief  设置串口通信速率
 13*@param  fd     类型 int  打开串口的文件句柄
 14*@param  speed  类型 int  串口速度
 15*@return  void
 16*/

 17int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
 18      B38400, B19200, B9600, B4800, B2400, B1200, B300, }
;
 19int name_arr[] = {38400,  19200,  9600,  4800,  2400,  1200,  300,
 20      38400,  19200,  9600480024001200,  300, }
;
 21
 22void CES_OpenDoor::set_speed(int fd, int speed)
 23{
 24    int   i;
 25    int   status;
 26    struct termios   Opt;
 27    tcgetattr(fd, &Opt);
 28    for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++)
 29    {
 30        if  (speed == name_arr[i])
 31        {
 32            tcflush(fd, TCIOFLUSH);
 33            cfsetispeed(&Opt, speed_arr[i]);
 34            cfsetospeed(&Opt, speed_arr[i]);
 35            status = tcsetattr(fd, TCSANOW, &Opt);
 36            if  (status != 0)
 37                ESLOG_debug("[CES_OpenDoor::set_speed] tcsetattr fd1");
 38            return;
 39        }

 40        tcflush(fd,TCIOFLUSH);
 41    }

 42}

 43
 44/**
 45*@brief   设置串口数据位,停止位和效验位
 46*@param  fd     类型  int  打开的串口文件句柄*
 47*@param  databits 类型  int 数据位   取值 为 7 或者8*
 48*@param  stopbits 类型  int 停止位   取值为 1 或者2*
 49*@param  parity  类型  int  效验类型 取值为N,E,O,,S
 50*/

 51int CES_OpenDoor::set_Parity(int fd,int databits,int stopbits,int parity)
 52{
 53    struct termios options;
 54    if  ( tcgetattr( fd,&options)  !=  0)
 55    {
 56        ESLOG_debug("[CES_OpenDoor::set_Parity] tcgetattr() 1");
 57        return(FALSE);
 58    }

 59    options.c_cflag &= ~CSIZE;
 60    switch (databits) /*设置数据位数*/
 61    {
 62        case 7:
 63            options.c_cflag |= CS7;
 64            break;
 65        case 8:
 66            options.c_cflag |= CS8;
 67            break;
 68        default:
 69            ESLOG_debug("[CES_OpenDoor::set_Parity]  Unsupported data size\n");
 70            return (FALSE);
 71    }

 72
 73    switch (parity)
 74    {
 75        case 'n':
 76        case 'N':
 77            options.c_cflag &= ~PARENB;   /* Clear parity enable */
 78            options.c_iflag &= ~INPCK;     /* Enable parity checking */
 79            break;
 80        case 'o':
 81        case 'O':
 82            options.c_cflag |= (PARODD | PARENB);  /* 设置为奇效验*/ 
 83            options.c_iflag |= INPCK;             /* Disnable parity checking */
 84            break;
 85        case 'e':
 86        case 'E':
 87            options.c_cflag |= PARENB;     /* Enable parity */
 88            options.c_cflag &= ~PARODD;   /* 转换为偶效验*/  
 89            options.c_iflag |= INPCK;       /* Disnable parity checking */
 90            break;
 91        case 'S':
 92        case 's':  /*as no parity*/
 93            options.c_cflag &= ~PARENB;
 94            options.c_cflag &= ~CSTOPB;
 95            break;
 96        default:
 97            ESLOG_debug("[CES_OpenDoor::set_Parity] Unsupported parity\n");
 98            return (FALSE);
 99    }

100    /* 设置停止位*/   
101    switch (stopbits)
102    {
103    case 1:
104        options.c_cflag &= ~CSTOPB;
105        break;
106    case 2:
107        options.c_cflag |= CSTOPB;
108        break;
109    default:
110        ESLOG_debug("[CES_OpenDoor::set_Parity] Unsupported stop bits\n");
111        return (FALSE);
112    }

113
114    /* Set input parity option */
115    if (parity != 'n')
116        options.c_iflag |= INPCK;
117
118    options.c_cc[VTIME] = 150// 15 seconds
119    options.c_cc[VMIN] = 0;
120    tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
121    if (tcsetattr(fd,TCSANOW,&options) != 0)
122    {
123        ESLOG_debug("[CES_OpenDoor::set_Parity] tcsetattr) ;
124        return (FALSE);
125    }

126    return (TRUE);
127}

128/**
129*@breif 打开串口
130*/

131int CES_OpenDoor::OpenDev(char *Dev)
132{
133    n_fd_com = open( Dev, O_RDWR );         //| O_NOCTTY | O_NDELAY
134    if (-1 == n_fd_com)
135    /*设置数据位数*/
136        ESLOG_debug("[CES_OpenDoor::OpenDev] Can't Open Serial Port");
137        return n_fd_com;
138    }
    
139    else
140    {
141        set_speed(n_fd_com,19200);
142        if (set_Parity(n_fd_com,8,1,'N')== FALSE)
143        {
144            ESLOG_debug("[CES_OpenDoor::OpenDev] Set Parity Error\n");
145        }

146        return n_fd_com;
147    }

148}
posted on 2013-03-22 11:57 易道 阅读(567) 评论(0)  编辑 收藏 引用 所属分类: linux 编程
只有注册用户登录后才能发表评论。