Results 1 to 7 of 7

Thread: serial programming

  1. #1
    Join Date
    Dec 2013
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default serial programming

    I am trying to write to serial port and read as well. Below is my code for it.
    //mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <qextserialport.h>
    4. #include <QDebug>
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. }
    12.  
    13. MainWindow::~MainWindow()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void MainWindow::on_pushButton_clicked()
    19. {
    20. PortSettings portSettings;
    21. portSettings.BaudRate = BAUD9600;
    22. portSettings.DataBits = DATA_8;
    23. portSettings.Parity = PAR_EVEN;
    24. portSettings.StopBits = STOP_1;
    25. portSettings.FlowControl = FLOW_OFF;
    26. //portSettings.Timeout_Millisec=1000;
    27.  
    28. port= new QextSerialPort("COM3",portSettings,QextSerialPort::EventDriven);
    29. QObject::connect(port,SIGNAL(readyRead()),this,SLOT(readData()));
    30. if(port->isOpen())
    31. {
    32. port->flush();
    33. port->close();
    34. }
    35. port->open(QextSerialPort::ReadWrite);
    36. if (port->isOpen()) {
    37. qDebug("connected!!");
    38. //delay->setInterval(2000);
    39.  
    40. }
    41. else qDebug("not connected");
    42. writeData(":0B1000408000102FA00DC\r\n");//this is the data that i need to send
    43. }
    44. void MainWindow::readData()
    45. {
    46. port->flush();
    47. QByteArray response=port->readAll();
    48. ui->textBrowser->append(response);
    49. }
    50. void MainWindow::writeData(char* msg)
    51. {
    52. port->write(msg);
    53. ui->textBrowser->append("writing data");
    54. ui->textBrowser->append(msg);
    55. port->flush();
    56. }
    To copy to clipboard, switch view to plain text mode 

    this code writes to virtual serial(realterm) and even reads the response. But when i use hardware to write serial data to an atmega, no response is obtained. But i received response when i sent this data to atmega using python. Is there any fault in here?
    Last edited by anda_skoa; 27th January 2014 at 08:20. Reason: add code tags

  2. #2
    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: serial programming

    You ca use QtSerialPort instead of QextSerialPort, because the QtSerialPort now is an part of Qt:

    http://qt-project.org/wiki/QtSerialPort
    http://qt-project.org/doc/qt-5.1/qts...ort-index.html

  3. #3
    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: serial programming

    Quote Originally Posted by kuzulis View Post
    You ca use QtSerialPort instead of QextSerialPort, because the QtSerialPort now is an part of Qt:

    http://qt-project.org/wiki/QtSerialPort
    http://qt-project.org/doc/qt-5.1/qts...ort-index.html
    Isn't that only in Qt5?

    According to the profile, pooch is using Qt4

    Cheers,
    _

  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: serial programming

    Isn't that only in Qt5?
    No, not only. QtSerialPort support Qt4 and/or Qt5. In case of Qt4 - user need to build it manually. Please read WIKI (the url above).

  5. #5
    Join Date
    Dec 2013
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: serial programming

    I installed qt5 from its site. But when i built the project, it gave me error like this:
    :-1: error: Qt Creator needs a compiler set up to build. Configure a compiler in the kit options.
    What do i have to do?

  6. #6
    Join Date
    Dec 2013
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: serial programming

    I installed qt5 from its site. But when i built the project, it gave me error like this:
    :-1: error: Qt Creator needs a compiler set up to build. Configure a compiler in the kit options.
    What do i have to do?
    I solves this problem myself.

    But according to QtSerialPort documentation, Configuring timeouts and delays while reading is not supported. What does that mean?
    I actually need it to implement modbusmaster protocol and to timeout when no data is recived within 1 second as soon as i complete my write to serial port.
    Also when i use qextserialport with qt5, when i try to open port, the application output says:
    Starting E:\qt stuffs try only\qt5\build-untitled-Desktop_Qt_5_2_0_MinGW_32bit-Debug\debug\untitled.exe...
    The program has unexpectedly finished.
    E:\qt stuffs try only\qt5\build-untitled-Desktop_Qt_5_2_0_MinGW_32bit-Debug\debug\untitled.exe crashed.
    what does this mean?
    What do i do with it

  7. #7
    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: serial programming

    But according to QtSerialPort documentation, Configuring timeouts and delays while reading is not supported. What does that mean?
    This means that I/O is non-blocking and all of read/write operations returns immediately, without waiting.
    It is necessary in order that not to lock of Qt event-loop, because it lead to freeze.

    I actually need it to implement modbusmaster protocol and to timeout when no data is recived within 1 second as soon as i complete my write to serial port.
    No problem:

    Qt Code:
    1. ...
    2. ...
    3. QTimer *timer = new QTimer(this);
    4. QSerialPort *port = new QSerialPort(this);
    5. ...
    6. timer->setSingleShot(true);
    7. ...
    8. connect(timer, SIGNAL(timeout()), this, SLOT(handleTimeout()));
    9. connect(port, SIGNAL(readyRead()), this, SLOT(handleReadyRead()));
    10. ...
    11. ...
    12. Foo::handleTimeout()
    13. {
    14. // process of response timeout from the slave device
    15. }
    16. ...
    17. Foo::handleReadyRead()
    18. {
    19. if (port->bytesAvailable() < expectedPacketLength)
    20. return;
    21.  
    22. timer->stop();
    23. QByteArray receivedPacket = port->readAll();
    24.  
    25. // do parse response from the slave device
    26. }
    27.  
    28. Foo::write()
    29. {
    30. port->write(packetToSend);
    31. timer->start(1000); // run timer for the 1 sec timeout
    32. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Serial Programming
    By pavan in forum Qt Programming
    Replies: 3
    Last Post: 10th August 2010, 11:49
  2. serial programming help
    By eva2002 in forum Qt Programming
    Replies: 2
    Last Post: 22nd January 2010, 11:45
  3. Serial Programming with QT
    By ape in forum Newbie
    Replies: 3
    Last Post: 24th December 2007, 20:33
  4. serial port programming in qt
    By sar_van81 in forum Qt Programming
    Replies: 46
    Last Post: 13th June 2007, 12:27
  5. Replies: 12
    Last Post: 23rd March 2007, 09:23

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.