在 Linux 中使用 Qt Close() 文件描述符
背景信息:
我一直在编写代码来控制通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用途:
对
QWidget::close
使用全局关闭函数Use:
to use global close function against
QWidget::close
您可能缺少 Qt Creator 中的 stdio.h。
You may be missing stdio.h from Qt Creator.