http://bbs.csdn.net/topics/340184140

/************************************************************************************
 * tty_open_port() open the tty port
 ************************************************************************************/
 
int  tty_open_port(const char *dev_name)
{
 
        int fd; /* File descriptor for the port */
        fd = open(dev_name, O_RDWR | O_NOCTTY | O_NDELAY);
        if (-1 == fd)
        {
          perror("open_port: Unable to open tty " );
          exit(1);
        }
        else
        {
 
            if(DEBUG)
                     printf("The %s is opened \n",dev_name);
        }
/*
          if( (val=fcntl(fd, F_SETFL, 0))< 0)
          perror("fcntl failed");
*/
    if ( isatty(fd) == 0 )
            perror("This is not a tty device ");
 
        return (fd);
}
/************************************************************************************
 * tty_set_port() set the attributes of the tty
 ************************************************************************************/
 int  tty_set_port (int  fd , int nSpeed , int  nBits , char nEvent , int  nStop )
{
    struct  termios new_ios,old_ios;
 
    if ( tcgetattr ( fd , &new_ios ) !=0 )
        perror("Save the terminal error");
 
    bzero( &old_ios , sizeofstruct termios ));
    old_ios=new_ios;
 
    tcflush(fd,TCIOFLUSH) ;
    new_ios.c_cflag |= CLOCAL |CREAD ;
    new_ios.c_cflag &= ~CSIZE ;
 
    switch (nBits)
    {
        case 5:
            new_ios.c_cflag |=CS5 ;
            break ;
        case 6:
            new_ios.c_cflag |=CS6 ;
            break ;
        case 7:
            new_ios.c_cflag |=CS7 ;
            break ;
        case 8:
            new_ios.c_cflag |=CS8 ;
            break ;
        default:
            perror("Wrong  nBits");
            break ;
    }
    switch (nSpeed )
    {
        case 2400:
            cfsetispeed(&new_ios , B2400);
            cfsetospeed(&new_ios , B2400);
            break;
        case 4800:
            cfsetispeed(&new_ios , B4800);
            cfsetospeed(&new_ios , B4800);
            break;
        case 9600:
            cfsetispeed(&new_ios , B9600);
            cfsetospeed(&new_ios , B9600);
            break;
        case 19200:
            cfsetispeed(&new_ios , B19200);
            cfsetospeed(&new_ios , B19200);
            break;
        case 115200:
            cfsetispeed(&new_ios , B115200);
            cfsetospeed(&new_ios , B115200);
            break;
        case 460800:
            cfsetispeed(&new_ios , B460800);
            cfsetospeed(&new_ios , B460800);
            break;
        default:
            perror("Wrong  nSpeed");
            break;
    }
    switch (nEvent )
    {
        case 'o':
        case 'O':
            new_ios.c_cflag |= PARENB ;
            new_ios.c_cflag |= PARODD ;
            new_ios.c_iflag |= (INPCK | ISTRIP);
            break ;
        case 'e':
        case 'E':
            new_ios.c_iflag |= (INPCK | ISTRIP);
            new_ios.c_cflag |= PARENB ;
            new_ios.c_cflag &= ~PARODD ;
            break ;
        case 'n':
        case 'N':
            new_ios.c_cflag &= ~PARENB ;
            new_ios.c_iflag &= ~INPCK  ;
            break ;
        default:
            perror("Wrong nEvent");
            break ;
    }
    if ( nStop == 1 )
        new_ios.c_cflag &= ~CSTOPB ;
    else if ( nStop == 2 )
        new_ios.c_cflag |= CSTOPB ;
 
    /*No hardware control*/
    new_ios.c_cflag &= ~CRTSCTS;
    /*No software control*/
    new_ios.c_iflag &= ~(IXON | IXOFF | IXANY);
    /*delay time set */
    new_ios.c_cc[VTIME] = 0 ;
    new_ios.c_cc[VMIN] = 0 ;
 
    /*raw model*/
    new_ios.c_lflag  &= ~(ICANON | ECHO | ECHOE | ISIG);
    new_ios.c_oflag  &= ~OPOST;
 
    new_ios.c_iflag &= ~(INLCR|IGNCR|ICRNL);
    new_ios.c_iflag &= ~(ONLCR|OCRNL);
 
    new_ios.c_oflag &= ~(INLCR|IGNCR|ICRNL);
    new_ios.c_oflag &= ~(ONLCR|OCRNL);
 
 
    tcflush(fd,TCIOFLUSH) ;
    if (tcsetattr(fd,TCSANOW,&new_ios) != 0 )
    {
        perror("Set the terminal error");
        tcsetattr(fd,TCSANOW,&old_ios);
        return -1 ;
    }
 
    return  0;
}

最新文章

  1. 初步了解JPA
  2. 【OPENGL】第三篇 着色器基础(一)
  3. Atitit. 类与对象的存储实现
  4. http://blog.csdn.net/a491057947/article/details/46724707
  5. mysql启动关闭
  6. 《day12---异常》
  7. 【JavaScript】javascript常用的东西
  8. IIS支持PHP
  9. WinForm调试输出数据
  10. Linux查找yum安装软件在系统中路径
  11. 权限的分类(shiro项目中来的五)
  12. 简单用数组模拟顺序栈(c++)
  13. Reids 持久化AOF 重写实现原理
  14. PhoenixFD插件流体模拟——UI布局【Output】详解
  15. SQL2008无法附加数据库,提示“无法显示请求的对话框”(nColIndex实际值是-1)图文解决方法
  16. [洛谷P1886]滑动窗口 (单调队列)(线段树)
  17. 卷积的三种模式:full, same, valid
  18. 笔记-返回到前一个页面时显示前一个页面中ajax获取的数据
  19. js切换背景颜色
  20. css居中问题:水平居中、垂直居中

热门文章

  1. PHP判断客户端是PCweb端还是移动手机端方法
  2. style css
  3. 基于OpenGL编写一个简易的2D渲染框架-06 编写一个粒子系统
  4. C++ 实现的netstat -an 的功能&lt;转&gt;-目的为获取rtmp推流地址如果是域名的话查看1935的ip
  5. UI5-文档-4.13-Margins and Paddings
  6. UGUI RectTransform
  7. 更新日志(建议升级到2017.1.18a) &amp;&amp; 更新程序的方法
  8. redmine邮件配置
  9. pyplot绘图区域
  10. 首届阿里巴巴在线技术峰会,9位大V演讲整理!