Results 1 to 14 of 14

Thread: read and write in serial port

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default read and write in serial port

    Hi
    I'm using Qt version 5.5.1 from windows 8.1.
    I create a Qt Quick Controls Application.
    Port opens, but I can not read data.

    main.cpp:
    Qt Code:
    1. #include <QApplication>
    2. #include <QQmlApplicationEngine>
    3. #include <QtSerialPort/QtSerialPort>
    4. #include <myserialport.h>
    5. #include <mythread.h>
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication app(argc, argv);
    10.  
    11. QQmlApplicationEngine engine;
    12. engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    13.  
    14. MySerialPort();
    15.  
    16. return app.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 

    myserialport.cpp:
    Qt Code:
    1. #include "myserialport.h"
    2. #include <QtSerialPort/QSerialPort>
    3. #include <QMessageBox>
    4. #include <QObject>
    5.  
    6. MySerialPort::MySerialPort()
    7. {
    8. serial = new QSerialPort(this);
    9. connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
    10. openSerialPort();
    11. }
    12.  
    13.  
    14. void MySerialPort::openSerialPort()
    15. {
    16. serial->setPortName("COM3");
    17. serial->setBaudRate(QSerialPort::Baud9600);
    18. serial->setDataBits(QSerialPort::Data8);
    19. serial->setParity(QSerialPort::NoParity);
    20. serial->setStopBits(QSerialPort::OneStop);
    21. serial->setFlowControl(QSerialPort::NoFlowControl);
    22. if (serial->open(QIODevice::ReadWrite)) {
    23.  
    24. showStatusMessage("Connectedd");
    25.  
    26. } else {
    27.  
    28. showStatusMessage(tr("Open error"));
    29. }
    30. }
    31.  
    32. void MySerialPort::closeSerialPort()
    33. {
    34. if (serial->isOpen())
    35. serial->close();
    36.  
    37. showStatusMessage(tr("Disconnected"));
    38. }
    39.  
    40. void MySerialPort::writeData(const QByteArray &data)
    41. {
    42. serial->write(data);
    43. }
    44.  
    45. void MySerialPort::readData()
    46. {
    47. QByteArray data = serial->readAll();
    48.  
    49. qDebug() << data;
    50.  
    51. }
    52.  
    53. void MySerialPort::handleError(QSerialPort::SerialPortError error)
    54. {
    55. if (error == QSerialPort::ResourceError) {
    56. closeSerialPort();
    57. }
    58. }
    59.  
    60.  
    61. void MySerialPort::showStatusMessage(const QString &message)
    62. {
    63. qDebug() << message;
    64. }
    To copy to clipboard, switch view to plain text mode 

    myserialport.h:
    Qt Code:
    1. #include "myserialport.h"
    2. #include <QtSerialPort/QSerialPort>
    3. #include <QMessageBox>
    4. #include <QObject>
    5.  
    6. MySerialPort::MySerialPort()
    7. {
    8. serial = new QSerialPort(this);
    9. connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
    10. openSerialPort();
    11. }
    12.  
    13.  
    14. void MySerialPort::openSerialPort()
    15. {
    16. serial->setPortName("COM3");
    17. serial->setBaudRate(QSerialPort::Baud9600);
    18. serial->setDataBits(QSerialPort::Data8);
    19. serial->setParity(QSerialPort::NoParity);
    20. serial->setStopBits(QSerialPort::OneStop);
    21. serial->setFlowControl(QSerialPort::NoFlowControl);
    22. if (serial->open(QIODevice::ReadWrite)) {
    23.  
    24. showStatusMessage("Connectedd");
    25.  
    26. } else {
    27.  
    28. showStatusMessage(tr("Open error"));
    29. }
    30. }
    31.  
    32. void MySerialPort::closeSerialPort()
    33. {
    34. if (serial->isOpen())
    35. serial->close();
    36.  
    37. showStatusMessage(tr("Disconnected"));
    38. }
    39.  
    40. void MySerialPort::writeData(const QByteArray &data)
    41. {
    42. serial->write(data);
    43. }
    44.  
    45. void MySerialPort::readData()
    46. {
    47. QByteArray data = serial->readAll();
    48.  
    49. qDebug() << data;
    50.  
    51. }
    52.  
    53. void MySerialPort::handleError(QSerialPort::SerialPortError error)
    54. {
    55. if (error == QSerialPort::ResourceError) {
    56. closeSerialPort();
    57. }
    58. }
    59.  
    60.  
    61. void MySerialPort::showStatusMessage(const QString &message)
    62. {
    63. qDebug() << message;
    64. }
    To copy to clipboard, switch view to plain text mode 

    myproject.pro:
    Qt Code:
    1. TEMPLATE = app
    2.  
    3. QT += qml quick widgets
    4. QT += serialport
    5.  
    6. SOURCES += main.cpp \
    7. myserialport.cpp \
    8. mythread.cpp
    9.  
    10. RESOURCES += qml.qrc
    11.  
    12. # Additional import path used to resolve QML modules in Qt Creator's code model
    13. QML_IMPORT_PATH =
    14.  
    15. # Default rules for deployment.
    16. include(deployment.pri)
    17.  
    18. HEADERS += \
    19. myserialport.h \
    20. mythread.h
    To copy to clipboard, switch view to plain text mode 
    Last edited by neda; 6th February 2016 at 07:11.

Similar Threads

  1. Serial read misses to read data from the serial port
    By mania in forum Qt for Embedded and Mobile
    Replies: 11
    Last Post: 18th August 2014, 08:49
  2. qext-serial-port write problem.
    By rex in forum Qt Programming
    Replies: 11
    Last Post: 9th December 2013, 07:18
  3. Replies: 4
    Last Post: 10th July 2010, 17:34
  4. Replies: 1
    Last Post: 16th June 2009, 09:09
  5. How to write bytes read from serial port to a QFile
    By shamik in forum Qt Programming
    Replies: 19
    Last Post: 25th June 2007, 14:12

Tags for this Thread

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.