在Linux中使用Qt关闭()filedescriptor

时间:2011-04-07 13:45:56

标签: c++ linux qt serial-port arduino

背景资料:

我一直在编写代码来控制通过USB线连接的设备,但是模拟RS-232串口。

有问题的设备是Arduino微控制器控制的伺服摇摄和倾斜阶段(但这并不重要)。

我已设法使用C ++语言将字符写入USB模拟串行端口,并使用以下代码在NetBeans IDE中设置g ++编译器:

    #include <cstdlib>
    #include <stdio.h>   /* Standard input/output definitions */
    #include <string.h>  /* String function definitions */
    #include <unistd.h>  /* UNIX standard function definitions */
    #include <fcntl.h>   /* File control definitions */
    #include <errno.h>   /* Error number definitions */
    #include <termios.h> /* POSIX terminal control definitions */

    /*
     * 'open_port()' - Open serial port 1.
     *
     * Returns the file descriptor on success or -1 on error.
     */

    int
    open_port(void)
    {
        int fd; /* File descriptor for the port */

        fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
        if (fd == -1)
        {
            /*
            * Could not open the port.
            */

            perror("open_port: Unable to open /dev/ttyUSB0 - ");
        }
        else
            fcntl(fd, F_SETFL, 0);

        int pannumber = 90;
        char str[256];
        int tiltnumber = 90;
        char str2[256];
        char panchar = 0xA5;
        char tiltchar = 0xA5;

        while (tiltchar != 0x00) {
            printf ("Enter the pan number: ");
            gets ( str );
            printf ("\n");
            pannumber = atoi ( str );

            printf ("Enter the tilt number: ");
            gets ( str2 );
            printf ("\n");

            tiltnumber = atoi ( str2 );
            panchar = (char) pannumber;
            tiltchar = (char) tiltnumber;
            int n = 0;
            char mydata[] = { 'U' , 'U' , 'U' , 'U' , panchar , tiltchar };
            n = write(fd, mydata, 6);
            if (n < 0)
            fputs("write() of 6 bytes failed!\n", stderr);
        }
        close(fd);
        return (fd);
    }

这很好用:(“UUUU”字符用于与arduino上运行的草图握手,接下来的两个字符设置伺服角度。)

问题:

我尝试使用Qt Creator使用两个图形滑块执行相同操作,这些滑块的值介于0和180之间,这些滑块将转换为字符并按上述方式发送。

这也适用但在使用Qt creator IDE进行编译时,它不喜欢close(fd);命令。如果我评论这一行,程序可以正常工作,但最终会抱怨有太多文件被打开。

Qt代码:

void MainWindow::TiltValueChangedHandler() {
    tiltValue =   ui->verticalSlider->value();
    panValue = ui->horizontalSlider->value();
    ui->label_3->setText(QString::number(tiltValue));
    ui->label_4->setText(QString::number(panValue));

    panValue++; // To prevent the error when 0 is sent over serial to range is effectively 1 to 181.

    int fd; /* File descriptor for the port. */

    fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1)
    {
        /*
         * Could not open the port.
         */

        perror("open_port: Unable to open /dev/ttyUSB0 - ");
    }
    else
        fcntl(fd, F_SETFL, 0);

    char panchar = (char) panValue;
    char tiltchar = (char) tiltValue;

    int n = 0;
    char mydata[] = { 'U' , 'U' , 'U' , 'U' , panchar , tiltchar };
    n = write(fd, mydata, 6);
    if (n < 0)
        fputs("write() of 6 bytes failed!\n", stderr);

    // close(fd);
    // above line has to be commented out or will not compile but file not does not close!
    return;
 }

未注释掉(fd)时的Qt编译器错误是:

  

错误:没有匹配函数来调用'MainWindow :: close(int&amp;)'

2 个答案:

答案 0 :(得分:10)

使用:

::close(fd);

QWidget::close

使用全局关闭功能

答案 1 :(得分:0)

您可能会遗漏Qt Creator中的stdio.h。