[SOLVED] Write to serial with Qt Gui Project



  • Hi!
    I've tried a loooot, but with no sucess write to serial using POSIX functions ( C ) on a Qt Gui Project (C++). Tried even using the /extern "C"/ thing (dunno if i did it right).
    I'm using:
    -Windows 7
    -Eclipse IDE for C/C++ Developers 1.4.2
    -MinGW 4.4
    -Qt Eclipse Integration 1.6.1
    -Qt by Nokia 4.8.2.

    (Everything from http://qt.nokia.com/downloads/windows-cpp)

    Im trying a simple task (write some bytes to the serial), as follows:

    -I created a "Qt Gui Project" project;
    -Added an QLabel to the QWidget on "gui.ui" (in design mode);
    -Added the following includes and code on the ""gui.cpp":

    @
    #include "gui.h"

    // Uses POSIX functions to send data to serial.
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>

    GUI::GUI(QWidget *parent)
    : QWidget(parent)
    {
    ui.setupUi(this);

    int fd = open("\\.\COM9", O_RDWR);
    if (fd != -1)
    {
    unsigned char command[] = {0xAA, 0x0C, 0x10, 0x00};
    if(write(fd, command, sizeof(command)) == -1)
    {
    ui.label->setText("Error writing");
    }
    close(fd);
    }
    }

    GUI::~GUI()
    {

    }
    @

    Well, it don't compile, giving me the following errors on "open", "write" and "close" commands:

    Function 'open' could not be resolved
    Function 'open' could not be resolved
    @
    gui.cpp:23: error: no matching function for call to 'GUI::close(int&)'
    ....\Qt\4.8.2\include/QtGui/../../src/gui/kernel/qwidget.h:506: note: candidates are: bool QWidget::close()
    @

    If i try the same code in a normal C/C++ project, everything goes fine (i've been using this code for a long time).

    It seems that eclipse is looking for Qt close() rather than POSIX close()...
    What am i doing wrong?
    Any help would be very appreciated!!



  • [quote author="juliannojungle" date="1339383135"]

    It seems that eclipse is looking for Qt close() rather than POSIX close()...
    What am i doing wrong?
    [/quote]

    One in global scope, one in class scope. So

    @
    ::close(fd)
    this->close();
    @



  • I got it, just cant make it access ::close(fd).
    Anyway, i found that using c++ fstream is a much easier approach.

    I got it working and writing to serial, the code follows:

    @
    #include "teste.h"

    #include <iostream>
    #include <fstream>
    using namespace std;

    teste::teste(QWidget *parent)
    : QWidget(parent)
    {
    ui.setupUi(this);
    fstream COM;
    COM.open("\\.\COM9");
    if (COM.fail())
    {
    ui.label->setText("Error writing");
    }
    char command[] = {0xAA, 0x0C, 0x10, 0x00};
    COM << command << endl;
    if (COM.is_open())
    {
    COM.close();
    }
    }

    teste::~teste()
    {

    }
    @

    Thanks a lot, everyone!! xD



  • Yes, Windows provides very poor support for Posix.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.