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

发布于 2024-10-31 03:53:43 字数 3587 浏览 0 评论 0原文

背景信息:

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

该设备是一个 Arduino 微控制器控制的伺服平移和倾斜平台(但这并不重要)。

我已成功使用 C++ 语言和 NetBeans 使用以下代码的 IDE:

    #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 创建者 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&)”

Background information:

I have been writing code to control a device attached by a USB cable but emulating an RS-232 serial port.

The device in question is an Arduino microcontroller controlled servo pan and tilt stage (but that's not important).

I have managed to write characters to the USB emulated serial port using the C++ language with the g++ compiler set in NetBeans IDE using the following code:

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

This works fine : (The "UUUU" chars are used to handshake with the sketch running on the arduino and the next two chars set the servo angle).

Question:

I have tried to do the same thing using Qt Creator with two graphical sliders which have values between 0 and 180 which are converted to chars and sent as above.

This also works BUT when compiling with Qt creator IDE it doesn't like the close(fd); command. If I comment this line out the program works but eventually it complains about too many files being open.

Qt code:

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

The Qt compiler error when close(fd) isn't commented out is:

error: no matching function for call to ‘MainWindow::close(int&)’

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤芳又自赏 2024-11-07 03:53:44

用途:

::close(fd);

QWidget::close 使用全局关闭函数

Use:

::close(fd);

to use global close function against QWidget::close

静谧 2024-11-07 03:53:44

您可能缺少 Qt Creator 中的 stdio.h。

You may be missing stdio.h from Qt Creator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文