Results 1 to 3 of 3

Thread: Using QtSerialPort in a console application

  1. #1
    Join Date
    Mar 2014
    Posts
    18
    Qt products
    Qt5

    Question Using QtSerialPort in a console application

    Hello,
    first I am sorry for my english. I am not a native speaker.

    I am a beginner in Qt and I try to use QtSerialPort in a console application.

    I tried to use the following code:

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QSerialPort>
    3. #include <QDebug>
    4. QSerialPort serial;
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QCoreApplication a(argc, argv);
    9.  
    10. serial.setPortName("/dev/ttyUSB0");
    11. serial.open(QIODevice::ReadWrite);
    12. serial.setBaudRate(QSerialPort::Baud115200);
    13. serial.setDataBits(QSerialPort::Data8);
    14. serial.setParity(QSerialPort::NoParity);
    15. serial.setStopBits(QSerialPort::OneStop);
    16. serial.setFlowControl(QSerialPort::NoFlowControl);
    17.  
    18. serial.write("-helloworld-");
    19. serial.close();
    20.  
    21. while (1)
    22. {
    23. if (serial.bytesAvailable()>0||serial.waitForReadyRead(10))
    24. {
    25. ba=serial.readAll();
    26. serial.write(ba);
    27. qDebug()<<ba;
    28. }
    29. }
    30. serial.close();
    31. return a.exec();
    32. }
    To copy to clipboard, switch view to plain text mode 

    In the open console window is written "Press <Return> to close this window..." and nothing more.
    The application output window in Qt tells me that the programm crashed but I dont know why and I also get no errors or warnings!

    If I try to use the Gui example of QtSerialPort named "terminal" everything woks fine.
    Therefore I can be sure that the access rights are ok and the hardware is working also with Qt.

    I also have testet another Gui application using the QtSerialPort and everything worked fine.

    But why is it not working in my console application?


    Can anybody help me?

    Thank you very much :-)

  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: Using QtSerialPort in a console application

    One thing that looks weird is that you close() after writing but then try to read again from the same object.

    Cheers,
    _

  3. #3
    Join Date
    Mar 2014
    Posts
    18
    Qt products
    Qt5

    Default Re: Using QtSerialPort in a console application

    Thank you very much.

    How embarrassing...

    I deleted the upper "serial.close" and now everything works fine

Similar Threads

  1. Replies: 8
    Last Post: 23rd August 2012, 19:28
  2. about console application
    By jirach_gag in forum Qt Programming
    Replies: 2
    Last Post: 5th January 2012, 11:39
  3. How to get keyevent in qt console application
    By zuoshaobo in forum Qt Programming
    Replies: 4
    Last Post: 7th December 2011, 06:55
  4. QT console application and CTRL+C
    By dawwin in forum Qt Programming
    Replies: 3
    Last Post: 18th March 2011, 16:35
  5. Replies: 2
    Last Post: 21st November 2010, 18:03

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.