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;
}

tty linux 打开和设置范例的更多相关文章

  1. Linux打开文件设置

    在某些情况下会要求增加Linux的文件打开数,以增加服务器到处理效率,在Linux下查看最多能打开的文件数量为: cat /proc/sys/fs/file-max 然后设置ulimit -n 文件数 ...

  2. Linux环境变量设置

    修改环境变量PATH 最近为root添加一个环境变量发现sudo su进去没有变化所以总结了一下所有设置环境变量的方法: 查看PATH:echo $PATH 直接在命令行修改,就可以使用,但是只有在当 ...

  3. 教你如何在Kali Linux 环境下设置蜜罐?

    导读 Pentbox是一个包含了许多可以使渗透测试工作变得简单流程化的工具的安全套件.它是用Ruby编写并且面向GNU/Linux,同时也支持Windows.MacOS和其它任何安装有Ruby的系统. ...

  4. linux打开80端口及80端口占用解决办法

    linux打开80端口天客户那边有台服务器同一个局域网中都无法访问,排除lamp环境问题,发现时服务器中的防火墙没有开启80端口. 代码如下 复制代码vi /etc/sysconfig/iptable ...

  5. linux中怎样设置dhcpd

    linux中怎样设置DHCP  在 linux 以下设置 DHCP一点也不复杂﹐您全部要做的仅仅有一个文件﹕/etc/dhcpd.conf . 以下﹐我用我自己的设置文件来说说怎么改动这个文件﹕  d ...

  6. Linux 打开句柄限制的调整

    Linux 打开句柄限制的调整 参考文章: Linux---进程句柄限制总结(http://blog.csdn.net/jhcsdb/article/details/32338953) !! 本文内容 ...

  7. linux linux系统常用设置

    linux  linux系统常用设置 一.设置开机时开启数字键 修改rc.local文件 命令:vi  /etc/rc.local rc.local文件中增加如下代码: INITTY=/dev/tty ...

  8. Linux打开防火墙telnet端口

    检查端口情况:netstat -an | grep 22 关闭端口号:iptables -A INPUT -p tcp --drop 端口号-j DROP                    ipt ...

  9. linux core文件设置

     http://blog.csdn.net/ctthuangcheng/article/details/8963551 linux core文件设置 分类: Linux OS Debugging Te ...

随机推荐

  1. 微擎框架小程序 uitl

    获取用户信息 util.getUserInfo(callback) 获取成功后会将用户信息写入到缓存中,如果指定了回调函数,则会调用回调函数 callback 获取成功后的回调函数 示例 var ap ...

  2. docker容器中搭建kafka集群环境

    Kafka集群管理.状态保存是通过zookeeper实现,所以先要搭建zookeeper集群 zookeeper集群搭建 一.软件环境: zookeeper集群需要超过半数的的node存活才能对外服务 ...

  3. NavitForMySql 破解工具使用

    Navicat 11.0注册机使用教程: 1.右键-管理员权限运行注册机2.选择对应的产品3.点击“补丁”按钮,选择文件4.点击“生成”按钮,生成序列号,并保存下授权文件5.复制序列号,打开软件,在弹 ...

  4. J2SE 8的Lambda --- functions

    functions //1. Runnable 输入参数:无 返回类型void new Thread(() -> System.out.println("In Java8!" ...

  5. 使用curl发起https请求

    "SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:S ...

  6. Cookie的过期时间设置

    https://pan.baidu.com/s/1ibUQhLt6ZgVyhVM6mnrtHg 密码:9psc

  7. 傅里叶变换--MP3、JPEG和Siri背后的数学

    http://blog.jobbole.com/51301/ 九年前,当我还坐在学校的物理数学课的课堂里时,我的老师为我们讲授了一种新方法,给我留下了深刻映像. 我认为,毫不夸张地说,这是对数学理论发 ...

  8. InitComponent的使用

    网页中的数据,有些是不在网页上改变的,像一些个人信息,比如:头像,当前用户名,友情链接等等,每次请求该页面都要重新加载,这样很消耗服务器资源,会降低服务器的性能,这个时候我们可以把这些不变的信息,统一 ...

  9. 复制CentOS虚拟机网络配置

    复制出来的CentOS虚拟机,网络需要重新配置.   卸载原来的VMware网卡,重新启用一块新的网卡,网卡网段要匹配.   ifconfig -a 查看当前启用网卡的mac地址 编辑/etc/ude ...

  10. mongodb first

    use [database] 使用数据库,新增文档后,数据库被自动创建 show dbs 显示所有数据库 db.[document].insert() 插入数据库 例:db.persons.inser ...