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. Send a serial message before closing the serial port in Qt
Forum Updated to NodeBB v4.3 + New Features

Send a serial message before closing the serial port in Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 6 Posters 1.5k Views 4 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.
  • C Offline
    C Offline
    Chicowolf
    wrote on last edited by Chicowolf
    #4

    I tryed to use the function waitForBytesWritten() like this:

            device->write(QString("DISCONCT").toStdString().c_str());
            if(device->waitForBytesWritten(30000))
            {
                displayMessage("Disconnected");
                deviceIsConnected = false;
                device->close();
                ui->PortComboBox->setEnabled(true);
                ui->tabWidget->setEnabled(false);
                ui->ConnectButton->setText("Connect");
                ui->ConnectionStatus->setText("<span style=\" color:#e1d41f;\">DISCONNECTED</span>");
            }
    

    but the issue persists. The port is well configured because the program sends other messages over serial port, and the messages are well received, except for the "DISCONCT" message.

    EDIT:
    I also tested the write line through qDebug and it returns 8

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

      Hi,

      Does that message get an acknowledge from the device ?

      On a side note, there's no need for these multiple conversions you are doing in your write call. Just call it with the string you want to send.

      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
      2
      • C Offline
        C Offline
        Chicowolf
        wrote on last edited by
        #6

        No the message is not received: the device receives 4 random character/ascii codes instead of the word "DISCONCT"

        Thanks for the suggestion :)

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #7

          Did you try QIODevice::flush()?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • C Offline
            C Offline
            Chicowolf
            wrote on last edited by
            #8

            @Christian-Ehrlicher nope, how should i use it?

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #9

              @Chicowolf said in Send a serial message before closing the serial port in Qt:

              how should i use it?

              I don't understand - simply call it? QSerialPort is derived from QIODevice...

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              C 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @Chicowolf said in Send a serial message before closing the serial port in Qt:

                how should i use it?

                I don't understand - simply call it? QSerialPort is derived from QIODevice...

                C Offline
                C Offline
                Chicowolf
                wrote on last edited by Chicowolf
                #10

                @Christian-Ehrlicher ok, i tryed to call it before and after the write call, but the issue persists

                Image
                this is the message received by the device: as you can see, it's a random message of 4 bytes instead of the 8 byte message i sent

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #11

                  Are you using windows? Which Qt version exactly? waitForBytesWritten() + flush() should work for sure.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  1
                  • C Offline
                    C Offline
                    Chicowolf
                    wrote on last edited by
                    #12

                    Yes, i'm using windows and Qt 5.14.1
                    This is my code now

                            device->flush();
                            device->write(QString("DISCONCT").toStdString().c_str());
                            device->flush();
                            if(device->waitForBytesWritten(30000))
                            {
                                displayMessage("Disconnected");
                                deviceIsConnected = false;
                                device->close();
                                ui->PortComboBox->setEnabled(true);
                                ui->tabWidget->setEnabled(false);
                                ui->ConnectButton->setText("Connect");
                                ui->ConnectionStatus->setText("<span style=\" color:#e1d41f;\">DISCONNECTED</span>");
                            }
                    
                    1 Reply Last reply
                    0
                    • Kent-DorfmanK Offline
                      Kent-DorfmanK Offline
                      Kent-Dorfman
                      wrote on last edited by Kent-Dorfman
                      #13

                      What is your state machine for the microcontroller? What is it suppose to do when it receives "DISCONCT"? Does it expect that the command be newline terminated? Can you sample a GPIO on the microcontroller when it receives the DISCONCT? Your problem may be on the embedded component and not in the Qt logic of "send and wait"....but without seeing the controller program I don't know how you're parsing the incoming command either.

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        Chicowolf
                        wrote on last edited by Chicowolf
                        #14

                        The state of the microcontroller is "CONNECTED" since the program sends the message "HELLO___" to the micro. When it receives "DISCONCT" it goes to "IDLE" state, ready to reconnect again if needed. It doesn't expect any '\n' character. I could use an oscilloscope, but it doesn't have the UART decoder fuction built-in; i'm monitoring the internal variables though, thanks to the debug mode in the micro, and i'm pretty sure that the issue is related to the close() call, since if i comment that call, the message is received.
                        As you can see in the image i posted in the previous answer, the micro's variable "RxData" it's not filled with the "DISCONCT" word, but with 4 random character.

                        To be fair, with the waitForBytesWritten(30000) call, the program doesn't seem that it waits for 30 second, it seems pretty responsive instead. If necessary i could post the entire code.

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #15

                          If you're already Debugging the Qt code then make some breakpoints inside qserialport_win.cpp in the close() and waitForBytesWritten methods.

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          C 1 Reply Last reply
                          1
                          • Christian EhrlicherC Christian Ehrlicher

                            If you're already Debugging the Qt code then make some breakpoints inside qserialport_win.cpp in the close() and waitForBytesWritten methods.

                            C Offline
                            C Offline
                            Chicowolf
                            wrote on last edited by
                            #16

                            @Christian-Ehrlicher thank for the suggestion :)
                            I made 3 breakpoints in the write(), waitForBytesWritten() and close() calls. The message is sent without problems, but after the flush() call, not after the write() call; the waitForBytesWritten(30000) call, doesn't wait for 30 seconds, but the close() call works as expectet.
                            Now that i put these three breakpoints the program seems to work fine, even in non-debug mode. I don't understand what is going on :/

                            1 Reply Last reply
                            0
                            • K Offline
                              K Offline
                              kuzulis
                              Qt Champions 2020
                              wrote on last edited by kuzulis
                              #17

                              There are no any way to know that a data has been written to the physical line.

                              A best way is wait for a device response (e.g. ACK) and only then close the port. Other way is just use QSP::bytesWritten() signal, and after this signal received wait a bit using QTimer with some empiric timeout (e.g. with some milliseconds), and only then close the port.

                              1 Reply Last reply
                              2
                              • Kent-DorfmanK Offline
                                Kent-DorfmanK Offline
                                Kent-Dorfman
                                wrote on last edited by
                                #18

                                I'm sorry to say it, but I'm leaning toward a questionable protocol on the embedded board. You are trying to parse block commands from a stream data feed. There are oh so many potential problems by not using either 1) all fixed length commands, or 2) choosing and consistently using an end of message indicator like \n...and consistently sending a status indicator upon receipt and processing of EVERY COMMAND.

                                1 Reply Last reply
                                1

                                • Login

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