Results 1 to 12 of 12

Thread: COM-port in Qt

  1. #1
    Join Date
    Mar 2013
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default COM-port in Qt

    Hello! I have several functions for working with COM-port. Here is a small listing, that I used in C++ Builder:

    Qt Code:
    1. //function for opening COM-port
    2. void COMOpen(portname)
    3. {
    4. DCB dcb;
    5. COMMTIMEOUTS timeouts; //structure for timeouts
    6.  
    7. //open COM-port!
    8. COMport = CreateFile(portname.c_str(),GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
    9.  
    10. if(COMport == INVALID_HANDLE_VALUE) { //if error
    11. QMessageBox::warning(this, "Error", "No such COM!");
    12. return;
    13. }
    14. }
    15.  
    16. //function for closing COM-port
    17. void COMClose()
    18. {
    19. CloseHandle(COMport);
    20. COMport=0;
    21. handle=0;
    22. }
    23.  
    24.  
    25. on_pushButton_clicked() // function send pulse on button click
    26. {
    27. COMOpen(portname); //initialized and open port
    28.  
    29. int i;
    30. EscapeCommFunction(COMport, SETDTR); //set hight level DTR
    31. ui->label->setText("Sending pulse to com-port..."); //sign about sending pulse
    32. i=0;
    33. while (i<3000000) { // wait a little, pulse should have a duration
    34. i++;
    35. Application->ProcessMessages();
    36. }
    37. EscapeCommFunction(COMport, CLRDTR); // free DTR pin
    38. ui->label->setText(""); //pulse finished, so remove label
    39. COMClose(portname); //close COM-port
    40.  
    41. }
    To copy to clipboard, switch view to plain text mode 

    When I include the code to Qt creator I get errors. I'm novice in Qt. Could someone make simple example, which open COM1, for instance, with this code? Thanks a lot!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: COM-port in Qt

    Hi,
    I am novice in mind reading. Could you probably tell us which errors you are getting?

    Cheers,
    _

  3. #3
    Join Date
    Mar 2013
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: COM-port in Qt

    Sorry, it's too many errors. I should replace this Application->CreateForm in WinApi to something like a->Initialize(), but now it's just conguring. Sorry for low skill. I think, it's better to attache my project.
    Attached Files Attached Files

  4. #4
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: COM-port in Qt

    You can try the ready module: QtSerialPort

    Online documentation: http://qt-project.org/doc/qt-5.1/qts...erialport.html

    Then your solution was like:

    Qt Code:
    1. class MyClass : public QObject
    2. {
    3. public:
    4. explicit MyClass() {
    5. port = new QSerialPort(this);
    6. port->setPortName("COM1");
    7. connect(port, SIGNAL(dataTerminalReadyChanged(bool)), this, SLOT(onDtr(bool)));
    8.  
    9. timer = new QTimer(this);
    10. timer->setSingleShot();
    11. connect(timer, SIGNAL(timeout()), this, SLOT(onTimer()));
    12. }
    13.  
    14. void startPulse() {
    15. if (!port->open(QIODevice::ReadWrite))
    16. return;
    17.  
    18. port->setDataTerminalReady(true);
    19. }
    20.  
    21. private slots:
    22. void onTimer() {
    23. port->setDataTerminalReady(false);
    24. }
    25.  
    26. void onDtr(bool set) {
    27. if (set) {
    28. timer->start(50); // ~50msec :)
    29. } else {
    30. port->close();
    31. }
    32. }
    33.  
    34. private:
    35. QSerialPort *port;
    36. QTimer *timer;
    37. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by kuzulis; 22nd July 2013 at 19:38.

  5. #5
    Join Date
    Mar 2013
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: COM-port in Qt

    Thank you, but it doesn't work on my computer. Fist problem is that my qt creator doesn't know qextserialport.h. How can I add it or what #include do I need if not that?
    And why do you use dataTerminalReadyChanged(bool) signal? I need only set DTR on push button, so should I just call onDtr() function in push_button case, right? I don't need connect(port, SIGNAL(dataTerminalReadyChanged(bool)), this, SLOT(onDtr(bool))), do I?
    Attached Files Attached Files

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: COM-port in Qt

    I don't see how this:
    Fist problem is that my qt creator doesn't know qextserialport.h.
    is related to this:
    You can try the ready module: QtSerialPort
    They are two different components. QtSerialPort is a part of Qt 5.1 out-of-the-box, QExtSerialPort is external.

    You use external libraries by including the relevant INCLUDEPATH and LIBS entries in your PRO file.
    Declaring Other Libraries

    And why do you use dataTerminalReadyChanged(bool) signal? I need only set DTR on push button, so should I just call onDtr() function in push_button case, right? I don't need connect(port, SIGNAL(dataTerminalReadyChanged(bool)), this, SLOT(onDtr(bool))), do I?
    The signal dataTerminalReadyChanged() signal connection ensures that regardless of how DTR is turned on the timer is started. The connection to the timer signal ensures that DTR is turned off approximately 50 mSec later. You can arrange it differently if you wish.

  7. #7
    Join Date
    Mar 2013
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: COM-port in Qt

    Thanks again. Unfortunately, I have Qt 4.8.3 and need solution directly for this version. I've read the documentation and obtained, that I should include this line to my pro file: CONFIG += serialport After all, I still have too many mistakes.
    Attached Files Attached Files

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: COM-port in Qt

    I've read the documentation and obtained, that I should include this line to my pro file: CONFIG += serialport
    That's the documentation for QtSerialPort.
    The instructions for QExtSerialPort are different.
    You can build either for Qt 4 but you implied you were trying to use QExtSerialPort.

  9. #9
    Join Date
    Mar 2013
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: COM-port in Qt

    Thank you! Now I'm much more close to the solution. I use 3rd way from wiki, include to my .pro:

    INCLUDEPATH += C:/qextserialport-1.2/src
    LIBS += -L C:/qextserialport-1.2/lib -lqextsrialport-1.2
    DEFINES += QEXTSERIALPORT_USING_SHARED
    I've truncated my code and left just the open/close procedure by press Button. Now I have only unique error:

    :-1: error: LNK1146: no argument specified with option '/LIBPATH:'

    And I really do not see any lib path in the proper directory. But I used instructions and download the directory from one issue that you wrote (instructions for QExtSerialPort)

  10. #10
    Join Date
    Mar 2013
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: COM-port in Qt

    I also tried first way with including qextserialport.pri in .pro file, but now I have other errors:
    moc_widget.obj:-1: error: LNK2019: unresolved external symbol "private: void __cdecl Widget:: onPortAddedOrRemoved(void)" (?onPortAddedOrRemoved@Widget@@AEAAXXZ) referenced in function "private: static void __cdecl Widget::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void (?qt_static_metacall@Widget@@CAXPEAVQObject@@W4Cal l@QMetaObject@@HPEAPEAX@Z)
    Shit! What can be wrong?! I use so minimal code:
    Qt Code:
    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. #include "qextserialport.h"
    4. #include <QtCore>
    5.  
    6. Widget::Widget(QWidget *parent) :
    7. QWidget(parent),
    8. ui(new Ui::Widget)
    9. {
    10. ui->setupUi(this);
    11. PortSettings settings = {BAUD9600, DATA_8, PAR_NONE, STOP_1, FLOW_OFF, 10};
    12. port = new QextSerialPort("COM1", settings, QextSerialPort::Polling);
    13. }
    14.  
    15. Widget::~Widget()
    16. {
    17. delete ui;
    18. }
    19.  
    20. void Widget::on_pushButton_clicked()
    21. {
    22. if (!port->isOpen()) {
    23. port->setPortName("COM1");
    24. port->open(QIODevice::ReadWrite);
    25. }
    26. else {
    27. port->close();
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: COM-port in Qt

    The linker says it can't find Widget:nPortAddedOrRemoved()

    Maybe you only have it in the header and not in the cpp file?

    Cheers,
    _

  12. #12
    Join Date
    Mar 2013
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: COM-port in Qt

    Oh, y! Thank you very much, it works)

Similar Threads

  1. how to port from qt3 to qt4
    By narlapavan in forum Qt Tools
    Replies: 3
    Last Post: 5th July 2013, 05:34
  2. how to port from qt3 to qt4
    By narlapavan in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2013, 08:17
  3. Replies: 1
    Last Post: 13th March 2013, 08:44
  4. Com Port
    By bismitapadhy in forum Qt Programming
    Replies: 4
    Last Post: 6th June 2009, 12:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.