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. How to send data in QserialPort thread
QtWS25 Last Chance

How to send data in QserialPort thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 5 Posters 7.9k Views
  • 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.
  • G Offline
    G Offline
    Gokhan
    wrote on last edited by
    #1

    Hi everyone,
    I'm using the QSerialPort with thread to read serialport, also have a mainwindow for GUI. I want to send the data from another class such as mainwindow through serial port. I should use the signal/slot method, qtconcurrent or to invoke the function directly to do it, which is better?

        for(;counterImageSend<4096*counterPartMemory;counterImageSend+=8){
            canbus.data[0]=static_cast<uint8_t>(flashMemory[counterImageSend]);
            canbus.data[1]=static_cast<uint8_t>(flashMemory[counterImageSend+1]);
            canbus.data[2]=static_cast<uint8_t>(flashMemory[counterImageSend+2]);
            canbus.data[3]=static_cast<uint8_t>(flashMemory[counterImageSend+3]);
            canbus.data[4]=static_cast<uint8_t>(flashMemory[counterImageSend+4]);
            canbus.data[5]=static_cast<uint8_t>(flashMemory[counterImageSend+5]);
            canbus.data[6]=static_cast<uint8_t>(flashMemory[counterImageSend+6]);
            canbus.data[7]=static_cast<uint8_t>(flashMemory[counterImageSend+7]);
           //send data...
        }
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mostefa
      wrote on last edited by
      #2

      Hi @Gokhan

      You can be interested with this link of qt doc

      Which Qt Thread Technology Should You Use?

      and

      Choosing an appropriate approach

      I hope this can help you !

      1 Reply Last reply
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        QSerialPort is already asynchronous so normally you wouldn't need a second thread to write to it.

        On a separate note, the code you posted is probably not the right way to handle communication over QSerialPort. you should check how to use QDataStream

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        3
        • G Offline
          G Offline
          Gokhan
          wrote on last edited by
          #4

          @mostefa hi, thank you for sharing.

          @VRonin thank you for your answer, I just tried to use the signal/slot method, although in main thread the signal is being emitted with Qt::BlockingQueuedConnection, all data can't be sent, so it can't be completed,

          VRoninV 1 Reply Last reply
          0
          • G Gokhan

            @mostefa hi, thank you for sharing.

            @VRonin thank you for your answer, I just tried to use the signal/slot method, although in main thread the signal is being emitted with Qt::BlockingQueuedConnection, all data can't be sent, so it can't be completed,

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            @Gokhan said in How to send data in QserialPort thread:

            all data can't be sent

            Can you elaborate this point?

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            G 1 Reply Last reply
            0
            • VRoninV VRonin

              @Gokhan said in How to send data in QserialPort thread:

              all data can't be sent

              Can you elaborate this point?

              G Offline
              G Offline
              Gokhan
              wrote on last edited by Gokhan
              #6

              @VRonin Serial port must not miss any data while reading, therefore I can use thread method. Sometimes, while reading big data which is sent immediately so byte-to-byte, unfortunately, have to send the big data. When the data is being sent, there must be a few delay between bytes, so I give a 1ms delay (QThread::msleep(1);). However, because reading and writing are at same thread, it can not read the buffer during writing and it gets an error that is bus-off. What should I do to read/write the serial port in a thread at the same time?

              1 Reply Last reply
              0
              • G Offline
                G Offline
                Gokhan
                wrote on last edited by
                #7

                I can't find any solution for this, please help me to solve. I have to set a delay is around 1ms while sending bytes. Moreover, at the same time, it has to read the buffer, but the thread is sleeping then and so it misses data.

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

                  Hi,

                  You seem to be blocking the communication, why not use Qt's asynchronous nature and buffer the data you receive until you have everything you need to go further ?

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

                  G 1 Reply Last reply
                  2
                  • AmoghA Offline
                    AmoghA Offline
                    Amogh
                    wrote on last edited by Amogh
                    #9

                    You can send it using byte array ,by serializing the data in an array and de-serializing it .

                    1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Hi,

                      You seem to be blocking the communication, why not use Qt's asynchronous nature and buffer the data you receive until you have everything you need to go further ?

                      G Offline
                      G Offline
                      Gokhan
                      wrote on last edited by Gokhan
                      #10

                      @SGaist Then it misses data while clicking the window bar, I have to use a thread for reading.

                      @Amogh I have already used the byte array for sending. While sending, it's spending lots of time because it's size is around 512byte and it sends byte-to-byte.

                      I'm using it like this link for reading and have tried to add the sending method. (qsp-no-freeze)
                      https://bugreports.qt.io/browse/QTBUG-61233

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

                        Then move the complete serial port handling to that thread and communicate information back to the main thread when appropriate but there shouldn't be any need to sleeping or blocking.

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

                        G 1 Reply Last reply
                        1
                        • SGaistS SGaist

                          Then move the complete serial port handling to that thread and communicate information back to the main thread when appropriate but there shouldn't be any need to sleeping or blocking.

                          G Offline
                          G Offline
                          Gokhan
                          wrote on last edited by
                          #12

                          @SGaist Well, how should I communicate with the serial port thread? Signals/Slots or Concurrency? Which better? You say to shouldn't block, but while writing the send buffer, have to wait in order to be writtenBytes. Then how can I check whether it was written a byte?

                          G 1 Reply Last reply
                          0
                          • G Gokhan

                            @SGaist Well, how should I communicate with the serial port thread? Signals/Slots or Concurrency? Which better? You say to shouldn't block, but while writing the send buffer, have to wait in order to be writtenBytes. Then how can I check whether it was written a byte?

                            G Offline
                            G Offline
                            Gokhan
                            wrote on last edited by
                            #13
                            This post is deleted!
                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              Please be a bit more patient before bumping your own thread. This is a community forum and people answering here may not live in the same timezone as you.

                              Signals/Slots, concurrency has nothing to do with inter thread communication.

                              Why do you have to wait when writing to your 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

                              G 1 Reply Last reply
                              1
                              • SGaistS SGaist

                                Please be a bit more patient before bumping your own thread. This is a community forum and people answering here may not live in the same timezone as you.

                                Signals/Slots, concurrency has nothing to do with inter thread communication.

                                Why do you have to wait when writing to your serial port ?

                                G Offline
                                G Offline
                                Gokhan
                                wrote on last edited by
                                #15

                                @SGaist Sorry for impatient, I am struggling with this problem for a week.
                                To make sure whether really it's written because the receiver has limited receive buffer, also these data will be parsed after that it will send through can bus.

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

                                  Are you mixing RS232 and CAN ?

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

                                  G 1 Reply Last reply
                                  0
                                  • SGaistS SGaist

                                    Are you mixing RS232 and CAN ?

                                    G Offline
                                    G Offline
                                    Gokhan
                                    wrote on last edited by Gokhan
                                    #17

                                    @SGaist It's USB-CAN module, so it converts the received data from USB to send can bus. At the same time, it might be data in can bus line and it has to read. It should never miss data even if the line is full(%100) while reading/writing.

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

                                      These are two different buffers so you should get data even if you are writing something to it.

                                      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
                                      • G Offline
                                        G Offline
                                        Gokhan
                                        wrote on last edited by Gokhan
                                        #19

                                        However, while it's writing, should read the buffer otherwise the line will be bus off and get an error. Unfortunately, because both are in the same thread and shouldn't wait to transfer the data.

                                        1 Reply Last reply
                                        0
                                        • G Offline
                                          G Offline
                                          Gokhan
                                          wrote on last edited by
                                          #20

                                          Well, how can I write data to the serial port when it's in another thread. Is it right method to access directly the writing register? Or Should I use signals/slots?

                                          serilPort *serial= new serialPort();
                                          
                                          serial->write(//data); it's right?
                                          
                                          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