Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] How to work with the Serial Port in Qt

[SOLVED] How to work with the Serial Port in Qt

Scheduled Pinned Locked Moved General and Desktop
11 Posts 2 Posters 4.8k 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.
  • 8Observer88 Offline
    8Observer88 Offline
    8Observer8
    wrote on last edited by
    #2

    I delete the m_sender object here:
    [CODE]
    MainWindow::~MainWindow()
    {
    delete ui;
    delete m_sender;
    }
    [/CODE]

    1 Reply Last reply
    0
    • 8Observer88 Offline
      8Observer88 Offline
      8Observer8
      wrote on last edited by
      #3

      The main problem: my application crash when I call .isOpen() Method. Why? Who can say the reason?

      [CODE]
      void Sender::send( const QByteArray &data ) throw( PortError )
      {
      if ( !m_port.isOpen() ) {
      throw PortError( m_port.errorString().toStdString() );
      }

      // Write data to the port
      if ( m_port.write( data ) == -1 ) {
          throw PortError( m_port.errorString().toStdString() );
      }
      

      }[/CODE]

      P.S. You see that it is very simple application

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

        Hi,

        Please run your application using a debugger, it will tell you exactly where the code is crashing

        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
        0
        • 8Observer88 Offline
          8Observer88 Offline
          8Observer8
          wrote on last edited by
          #5

          I've written. Here: "if ( !m_port.isOpen() )" in my last post. And I see this message:
          http://i6.pixs.ru/storage/4/6/9/288png_9391611_13540469.png

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

            Indeed, but running with the debugger, it will show you the stack trace and that would be more useful to help finding where the problem is.

            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
            0
            • 8Observer88 Offline
              8Observer88 Offline
              8Observer8
              wrote on last edited by
              #7

              I was helped here: [url]http://www.prog.org.ru/index.php?topic=27520[/url]

              The error on this line "m_port( portName ),"
              [CODE]
              Sender::Sender( const QString &portName,
              QSerialPort::BaudRate baudRate,
              QSerialPort::DataBits dataBits,
              QSerialPort::Parity parity,
              QSerialPort::StopBits stopBits,
              QSerialPort::FlowControl flowControl ) :
              m_port( portName ),
              m_baudRate( baudRate ),
              m_dataBits( dataBits ),
              m_parity( parity ),
              m_stopBits( stopBits ),
              m_flowControl( flowControl )
              {
              // Set the port name
              m_port.setPortName( m_portName );[/CODE]

              I should have written m_portName instead m_port.

              I replaced "QSerialPort m_port;" on "QSerialPort m_serialPort;" And I delete the m_portName because I can to write m_serialPort( portName ) in the "Sender" constructor.

              It is my project: [url]https://github.com/8Observer8/ComPort[/url]

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

                Since you are using exceptions, please take a look at the corresponding chapter in Qt's documentation.

                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
                0
                • 8Observer88 Offline
                  8Observer88 Offline
                  8Observer8
                  wrote on last edited by
                  #9

                  Please, show me what do you mean? Cannot I use exceptions?

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

                    Yes, you can, but there are some rules. Read "this":http://qt-project.org/doc/qt-5/exceptionsafety.html

                    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
                    0
                    • 8Observer88 Offline
                      8Observer88 Offline
                      8Observer8
                      wrote on last edited by
                      #11

                      Thank you very much! :)

                      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