Close() filedescriptor using Qt in Linux
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 s开发者_开发知识库tr[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&)’
Use:
::close(fd);
to use global close function against QWidget::close
You may be missing stdio.h from Qt Creator.
精彩评论