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. Serial port expanding enumerator example

Serial port expanding enumerator example

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 3.1k Views 2 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.
  • S Offline
    S Offline
    SweetOrange
    wrote on last edited by SweetOrange
    #1

    Hi!
    I am trying to expand the enumerator example and create a Serial Port object. I try then to invoke some QSerialPort member-functions using that object but I get some errors. Eventually I would like to send some data via COM4. See image: http://s29.postimg.org/yq4mgwe2f/Build_Errors_1_1_16.jpg

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      You need to include
      #include <QtSerialPort>
      also
      You have only QSerialPortInfo so It dont know serial port
      in your code and that it was it means with incomplete type. :)

      1 Reply Last reply
      1
      • S Offline
        S Offline
        SweetOrange
        wrote on last edited by
        #3

        I have added some lines to the code and got some errors. See image
        http://s22.postimg.org/g6isurxbl/Build_Errors_1_6_16.jpg

        Is the following sequence correct? 1)set port name 2)open the port 3) write data 4) close the port

        serialPort.setPortName(COM4);
        serialPort.open(QIODevice::WriteOnly);
        serialPort.writeData(0xAA, 6);
        serialPort.close();

        thanks

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

          Hi,

          1. COM4 is not a string, put it between quotes
          2. use write and with a QByteArray

          Also, add a check that open is successful. That way you are sure the port is open and the rest of the code should work.

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

          S 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            1. COM4 is not a string, put it between quotes
            2. use write and with a QByteArray

            Also, add a check that open is successful. That way you are sure the port is open and the rest of the code should work.

            S Offline
            S Offline
            SweetOrange
            wrote on last edited by
            #5

            @SGaist Thanks
            I added the following lines of code and now runs successfully. see lines 34-55
            http://s8.postimg.org/j8fvvd3hx/Build_Errors_1_7_16.jpg

            The check I have to verify that port COM4 is open gives false so the program stacks at while loop.
            Do I need to use the setPort() as well?
            What does the setPortName() does?

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

              That's not a check, it's just a blocking while loop. If it can't be opened the first time, the chances are unlikely that it will become later.

              Just use an if and print the error string so you will know what is happening.

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

              S 1 Reply Last reply
              0
              • SGaistS SGaist

                That's not a check, it's just a blocking while loop. If it can't be opened the first time, the chances are unlikely that it will become later.

                Just use an if and print the error string so you will know what is happening.

                S Offline
                S Offline
                SweetOrange
                wrote on last edited by SweetOrange
                #7

                @SGaist
                http://s12.postimg.org/gvao8d6st/Build_Errors_1_8_16.jpg

                I use a usb to serial TTL FTDI cable (TTL-232RG-VIP-WE) connected to COM4.
                It seems that the port doesn't open.
                How to print the error string?

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

                  qDebug() << serialPort.errorString();

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

                  S 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    qDebug() << serialPort.errorString();

                    S Offline
                    S Offline
                    SweetOrange
                    wrote on last edited by
                    #9

                    @SGaist
                    http://s10.postimg.org/8u1d8066x/Build_Errors_1_8_16_b.jpg
                    http://s7.postimg.org/gveeczka3/Build_Errors_1_8_16_c.jpg

                    It doesn't open COM4.

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

                      You're opening it twice

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

                      S 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        You're opening it twice

                        S Offline
                        S Offline
                        SweetOrange
                        wrote on last edited by
                        #11

                        @SGaist
                        http://s22.postimg.org/n3cplzer5/Build_Errors_1_8_16_d.jpg
                        I commented line 49 and now it opens the port and transmits the data.(data need to be captured in scope)
                        I will mark this as an answer.
                        I will be creating another question in order to design a minimum gui that selects baud rate, stop bits and data bits and then transmits data that have been placed to a text box. Probably I will be needing your help. I have been reading for the last year C++ and recently I have ordered book by Jasmin Blanchette.
                        thanks again.

                        1 Reply Last reply
                        0
                        • mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          hi
                          The Terminal example that comes with Qt has dialog for selecting
                          comport settings.
                          Its minimal sample and sounds a lot like what u need.
                          http://doc.qt.io/qt-5/qtserialport-terminal-example.html

                          S 1 Reply Last reply
                          0
                          • mrjjM mrjj

                            hi
                            The Terminal example that comes with Qt has dialog for selecting
                            comport settings.
                            Its minimal sample and sounds a lot like what u need.
                            http://doc.qt.io/qt-5/qtserialport-terminal-example.html

                            S Offline
                            S Offline
                            SweetOrange
                            wrote on last edited by
                            #13

                            @mrjj
                            hi will check

                            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