linux 下串行口超时设置

这是我的实现代码,关键是fcntl(fd, F_SETFL, O_NDELAY) 这一行不能要,有了这一行,一切都会搞乱。


    struct termios options;


    fd = open("/devttyS0", O_RDWR | O_NOCTTY );

    if (fd == -1)
    {
        //Could not open the port.
        return FAIL;
    }

    //fcntl(fd, F_SETFL, O_NDELAY);

    //Get the current options for the port...
    tcgetattr(fd, &options);

    //Set the baud rates
    cfsetispeed(&options, B115200);
    cfsetospeed(&options, B115200);

    //Enable the receiver and set local mode...
    options.c_cflag |= (CLOCAL | CREAD);

    //8 data bits
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;

    //CSTOPB -- 2 stop bits (1 otherwise)
    //1 stop bit
    options.c_cflag &= ~CSTOPB;

    //no parity
    options.c_cflag &= ~PARENB;

    //use rts/cts flow control    
    if(flowControl)
    {
        options.c_cflag |= CRTSCTS;
    }
    else
    {
        options.c_cflag &= (~CRTSCTS);
    }

    //disable software flow control
    options.c_iflag &= ~(IXON | IXOFF | IXANY);

    //raw type input
    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    //raw type output
    options.c_oflag &= ~OPOST;

    //disable 0d->0xa mapping
    options.c_iflag &= ~ (INLCR | ICRNL | IGNCR);
    options.c_oflag &= ~(ONLCR | OCRNL);

    options.c_cc[VTIME] = 40; /* Set timeout of 4.0 seconds */
    options.c_cc[VMIN] = 0;
    //Set the new options for the port...
    //tcsetattr(fd, TCSANOW, &options);
    tcsetattr(fd, TCSAFLUSH, &options);

    tcflush(fd, TCIOFLUSH);





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值