Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. read and write in serial port
Forum Updated to NodeBB v4.3 + New Features

read and write in serial port

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlserial port
13 Posts 4 Posters 25.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    neda
    wrote on last edited by neda
    #1

    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.
    I have a error:
    "Break condition detected while reading" (Error code 6)
    Please guide me.

    main.cpp:

    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include <QtSerialPort/QtSerialPort>
    #include <myserialport.h>
    #include <mythread.h>
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
        //MySerialPort();
        MySerialPort iSerialPort;
        iSerialPort.openSerialPort();
    
        return app.exec();
    }
    

    myserialport.cpp:

    #include "myserialport.h"
    #include <QtSerialPort/QSerialPort>
    #include <QMessageBox>
    #include <QObject>
    
    MySerialPort::MySerialPort()
    {
        serial = new QSerialPort(this);
        connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
        //openSerialPort();
    }
    
    
    void MySerialPort::openSerialPort()
    {
        serial->setPortName("COM3");
        serial->setBaudRate(QSerialPort::Baud9600);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        if (serial->open(QIODevice::ReadWrite)) {
    
            showStatusMessage("Connectedd");
    
        } else {
    
            showStatusMessage(tr("Open error"));
        }
    }
    
    void MySerialPort::closeSerialPort()
    {
        if (serial->isOpen())
            serial->close();
    
        showStatusMessage(tr("Disconnected"));
    }
    
    void MySerialPort::writeData(const QByteArray &data)
    {
        serial->write(data);
    }
    
    void MySerialPort::readData()
    {
       QByteArray data = serial->readAll();
    
       qDebug() << data;
    
    }
    
    void MySerialPort::handleError(QSerialPort::SerialPortError error)
    {
        if (error == QSerialPort::ResourceError) {
            closeSerialPort();
        }
    }
    
    
    void MySerialPort::showStatusMessage(const QString &message)
    {
        qDebug() << message;
    }
    

    myserialport.h:

    #ifndef MYSERIALPORT_H
    #define MYSERIALPORT_H
    #include <QtSerialPort/QtSerialPort>
    #include <QObject>
    
    class MySerialPort: public QSerialPort
    {
         Q_OBJECT
    public:
        MySerialPort();
    
    public slots:
        void openSerialPort();
        void closeSerialPort();
    
        void writeData(const QByteArray &data);
        void readData();
    
        void handleError(QSerialPort::SerialPortError error);
    
    private:
        void showStatusMessage(const QString &message);
    
        QSerialPort *serial;
    
    };
    
    #endif // MYSERIALPORT_H
    
    

    myproject.pro:

    TEMPLATE = app
    
    QT += qml quick widgets
    QT += serialport
    
    SOURCES += main.cpp \
        myserialport.cpp \
        mythread.cpp
    
    RESOURCES += qml.qrc
    
    # Additional import path used to resolve QML modules in Qt Creator's code model
    QML_IMPORT_PATH =
    
    # Default rules for deployment.
    include(deployment.pri)
    
    HEADERS += \
        myserialport.h \
        mythread.h
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Do you have any other application also accessing that serial port ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      N 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Do you have any other application also accessing that serial port ?

        N Offline
        N Offline
        neda
        wrote on last edited by
        #3

        @SGaist

        Hi,
        I read data with Hercules_3-2-6 Application (rs232 terminal software), but before opening my program, I closed Hercules_3-2-6 Application

        1 Reply Last reply
        0
        • N Offline
          N Offline
          neda
          wrote on last edited by neda
          #4

          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 my application 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.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Are you using the same serial port setup in both your application and Hercules_3-2-6 ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            N 1 Reply Last reply
            0
            • SGaistS SGaist

              Are you using the same serial port setup in both your application and Hercules_3-2-6 ?

              N Offline
              N Offline
              neda
              wrote on last edited by neda
              #6

              @SGaist

              yes, I using the same serial port setup in both my application and Hercules_3-2-6 .

              Specification of port: Name: COM3, Baud Rate: 9600, Data bits: 8, Parity: None, Stop bits: 1, Flow control: None

              Hercules_3-2-6

              My program:

              void MySerialPort::openSerialPort()
              {
                  serial->setPortName("COM3");
              
              
                  if (serial->open(QIODevice::ReadWrite)) {
                      serial->setParity(QSerialPort::NoParity);
                      serial->setBaudRate(QSerialPort::Baud9600);
                      serial->setDataBits(QSerialPort::Data8);
                      serial->setStopBits(QSerialPort::OneStop);
                      serial->setFlowControl(QSerialPort::NoFlowControl);
                      showStatusMessage("Connectedd");
              
                  } else {
              
                      showStatusMessage(tr("Open error"));
                  }
              }
              

              Meanwhile I have the same problem in qtserialport terminal example.
              When I run qtserialport terminal example, program connects to port successfully, but does not receive any data.
              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.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Does the device need DTR ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • N Offline
                  N Offline
                  neda
                  wrote on last edited by neda
                  #8

                  No,
                  I wrote a C# .net program. It worked very well likes to Hercules_3-2-6 Application and qt program worked after opening the program.
                  I added this line for handle error port.

                  void MySerialPort::handleError(QSerialPort::SerialPortError error)
                  {
                      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?

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @kuzulis might have an idea about that one

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    N 2 Replies Last reply
                    0
                    • SGaistS SGaist

                      @kuzulis might have an idea about that one

                      N Offline
                      N Offline
                      neda
                      wrote on last edited by
                      #10
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • SGaistS SGaist

                        @kuzulis might have an idea about that one

                        N Offline
                        N Offline
                        neda
                        wrote on last edited by neda
                        #11

                        @SGaist
                        Thank you very much my dear.
                        Your answer was a big help for me.

                        My problem was solved by set "setDataTerminalReady" function to false.
                        I did not have information about the signal DTR, by pointing you I read about that and find my problem.

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          TobyBar
                          wrote on last edited by
                          #12
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            asterixxx
                            wrote on last edited by
                            #13

                            https://www.youtube.com/watch?v=7ny6Hrdc_2M&t=47s

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved