使用Qt在Linux中关闭文件描述符Close()

6

背景信息:

我一直在编写控制通过USB电缆连接但模拟RS-232串行端口的设备的代码。

所涉及的设备是Arduino微控制器控制的伺服平移舞台(但这并不重要)。

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

    #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上运行的sketch握手,接下来的两个字符设置舵机角度)。
问题:
我试图使用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;
 }

当没有注释掉 close(fd) 时,Qt 编译器会报错:
错误:没有与 'MainWindow::close(int&)' 匹配的函数

1
听起来像是命名空间冲突。尝试使用 ::close(fd)。 - Duck
1
还有一件事:通常我们以这种形式称呼Qt。QT通常指的是QuickTime。 - Naszta
2个回答

14

使用:

::close(fd);

如何针对 QWidget::close 使用全局关闭函数?


谢谢,这是正确的,现在可以编译了!但是我不确定为什么在 ::close(fd); 前面有一个空格,这不是表示 close(fd) 属于当前对象作用域吗? - Zac
1
没有 :: 时,它尝试使用 QWidget 的 bool close(void); 方法。添加 :: 后,您声明使用来自全局名称空间的 close - Naszta

0

你可能会从Qt Creator中缺失stdio.h头文件。


没有关系,我在这个函数上面有一整个导入系列。 - Zac

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接