Results 1 to 14 of 14

Thread: read and write in serial port

  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 08:11.

  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: read and write in serial port

    Quote Originally Posted by neda View Post
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. MySerialPort();
    4. }
    To copy to clipboard, switch view to plain text mode 
    Creates and immediately destroys a temporay instance of MySerialPort

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    neda (10th February 2016)

  4. #3
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: read and write in serial port

    Quote Originally Posted by anda_skoa View Post
    Creates and immediately destroys a temporay instance of MySerialPort

    Cheers,
    _
    Thank you for your reply.

    I change my code:

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

    But now I have a error:
    Qt Code:
    1. Error code 6
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. Break condition detected while reading
    To copy to clipboard, switch view to plain text mode 
    Last edited by neda; 6th February 2016 at 11:08.

  5. #4
    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: read and write in serial port

    Did you remove the openSerialPort() call from the constructor or are you calling it twice now?

    Cheers,
    _

  6. #5
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: read and write in serial port

    I remove the openSerialPort() call from the constructor.

    MySerialPort::MySerialPort()
    {
    serial = new QSerialPort(this);


    connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
    connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this,
    SLOT(handleError(QSerialPort::SerialPortError)));

    //openSerialPort();
    }

  7. #6
    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: read and write in serial port

    If I understand the information on serial break correctly, you are getting a sort of data timeout on the line.
    This is a hardware thing: http://ltxfaq.custhelp.com/app/answe...erial-break%3F

    Maybe the device you are connected to can't keep up or is using different settings?

    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    neda (10th February 2016)

  9. #7
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: read and write in serial port

    Quote Originally Posted by anda_skoa View Post
    If I understand the information on serial break correctly, you are getting a sort of data timeout on the line.
    This is a hardware thing: http://ltxfaq.custhelp.com/app/answe...erial-break%3F

    Maybe the device you are connected to can't keep up or is using different settings?

    Cheers,
    _
    I read data with Hercules_3-2-6 Application (rs232 terminal software) http://www.qtcentre.org/threads/6510...nother-program

  10. #8
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: read and write in serial port

    My program worked today, and I have not any error.
    But I do not any change to program.

    When I run program,
    program connects to port successfully, but I receive just one data (space).

    But when I close this program and open Hercules_3-2-6 Application (rs232 terminal software), that application read data,
    and after close Hercules_3-2-6 application and open terminal example again, this program works and reads data until restarting computer.

    I repeat this process many times.

    But my project does not receive data after restarting system until port opens one time by Hercules_3-2-6 Application.

    Meanwhile I have virtual USB COM port and my hardware connected to computer with USB.
    Last edited by neda; 7th February 2016 at 07:19.

  11. #9
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: read and write in serial port

    I wrote a .net program.It worked very well likes to Hercules_3-2-6 Application and qt program worked after opening the program.
    Is it bug of Qt 5.5.1 ?

  12. #10
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: read and write in serial port

    I added this line for handle error port.

    voidMySerialPort::handleError(QSerialPort::SerialP ortErrorerror)
    {
    qDebug()<<"Error:"<<error;//I added this line
    if(error==QSerialPort::ResourceError)
    {
    showStatusMessage(serial->errorString());
    closeSerialPort();
    }
    }

    Result of run program:

    Error: 0
    "Connectedd"
    Error: 6
    "\x00"


    Just sometimes I have error 6.
    How do I fix break condition error?

  13. #11
    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: read and write in serial port

    > How do I fix break condition error?

    Nohow. It is HW/driver issue.

    UPD: Future Qt 5.6.0 won't be process the Parity/Frame and BreakCondition errors anymore. So, you can try QtSeriaslPort from 5.6.0 branch.

  14. The following user says thank you to kuzulis for this useful post:

    neda (10th February 2016)

  15. #12
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: read and write in serial port

    Quote Originally Posted by kuzulis View Post
    > How do I fix break condition error?

    Nohow. It is HW/driver issue.

    UPD: Future Qt 5.6.0 won't be process the Parity/Frame and BreakCondition errors anymore. So, you can try QtSeriaslPort from 5.6.0 branch.
    Of course,My program is problem because other programs (C# .net , Hercules) work very well.
    My problem is bug of QSerialPort?
    Last edited by neda; 10th February 2016 at 10:00.

  16. #13
    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: read and write in serial port

    > Of course,My program is problem because other programs (C# .net , Hercules) work very well.

    So what? Maybe they (those programs) do not trace the EV_ERR event flag. This flag coming from the device driver, and QtSerialPort < 5.6 handles this error when it is occurred.

    > My problem is bug of QSerialPort?

    You problem is bug of HW/driver of device.

  17. The following user says thank you to kuzulis for this useful post:

    neda (10th February 2016)

  18. #14
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: read and write in serial port

    Thanks you very much.
    Your answer was a big help for me.

    serial->setDataTerminalReady(false);

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, 09:49
  2. qext-serial-port write problem.
    By rex in forum Qt Programming
    Replies: 11
    Last Post: 9th December 2013, 08:18
  3. Replies: 4
    Last Post: 10th July 2010, 18:34
  4. Replies: 1
    Last Post: 16th June 2009, 10: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, 15: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.