Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [Solved]Connecting to a serial device

    General and Desktop
    2
    5
    1090
    Loading More Posts
    • 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.
    • A
      Alfredo Palhares last edited by Alfredo Palhares

      Hello everyone,

      I am new to Qt and C++ and programming in general. I am trying to connect to serial device, here is the code:

      #include <QApplication>
      #include <QPushButton>
      #include <QtSerialPort/QtSerialPort>
      
      int main(int argc, char **argv)
      {
        QApplication app (argc, argv);
      
        //QPushButton button("Hello world !");
        //button.show();
      
      
        std::string portName = "/dev/ttyUSB0";
        int baudRate = 115200;
      
        bool parity = true;
      /*
       * We're trying to send this
       * 0000 0000 0000 0000 0000 0000 0000 00aa
       * 0000 0202 cc00 0000 0000 0000 0000 0000
       * 0a                                       .
       */
      
        QSerialPort serial;
      
        serial->setPortName(portName);
        serial->setBaudRate(baudRate);
        serial->setParity(parity);
      
      
        return app.exec();
      }
      

      And while building I get this:

      INFO: There is a new version of biicode. Download it at https://www.biicode.com/downloads
      INFO: Processing changes...
      Building: "cmake" --build . 
      Scanning dependencies of target user_yats_main
      [100%] Building CXX object user_yats/CMakeFiles/user_yats_main.dir/main.cpp.o
      /path/to/my/repomain.cpp: In function ‘int main(int, char**)’:
      /path/to/my/repomain.cpp:26:9: error: base operand of ‘->’ has non-pointer type ‘QSerialPort’
         serial->setPortName(portName);
               ^
      /path/to/my/repomain.cpp:27:9: error: base operand of ‘->’ has non-pointer type ‘QSerialPort’
         serial->setBaudRate(baudRate);
               ^
      /path/to/my/repomain.cpp:28:9: error: base operand of ‘->’ has non-pointer type ‘QSerialPort’
         serial->setParity(parity);
               ^
      user_yats/CMakeFiles/user_yats_main.dir/build.make:57: recipe for target 'user_yats/CMakeFiles/user_yats_main.dir/main.cpp.o' failed
      make[2]: *** [user_yats/CMakeFiles/user_yats_main.dir/main.cpp.o] Error 1
      CMakeFiles/Makefile2:106: recipe for target 'user_yats/CMakeFiles/user_yats_main.dir/all' failed
      make[1]: *** [user_yats/CMakeFiles/user_yats_main.dir/all] Error 2
      Makefile:85: recipe for target 'all' failed
      make: *** [all] Error 2
      
      

      What am I doing wrong ?

      1 Reply Last reply Reply Quote 0
      • M
        mcosta last edited by

        Hi and welcome to devnet,

        I don't know if is related but since Qt 5.1 you can use QSerialPort.

        BTW the error is here

        QSerialPort serial;
        
        serial->setPortName(portName);
        serial->setBaudRate(baudRate);
        serial->setParity(parity);
        

        You have to write

        QSerialPort serial;
        
        serial.setPortName(portName);
        serial.setBaudRate(baudRate);
        serial.setParity(parity);
        

        Once your problem is solved don't forget to:

        • Mark the thread as SOLVED using the Topic Tool menu
        • Vote up the answer(s) that helped you to solve the issue

        You can embed images using (http://imgur.com/) or (http://postimage.org/)

        A 1 Reply Last reply Reply Quote 1
        • A
          Alfredo Palhares last edited by

          QSerialPort *serial;
          

          Instancing QSerialPort as a pointer works, why is this ?

          1 Reply Last reply Reply Quote 0
          • A
            Alfredo Palhares @mcosta last edited by

            Hello @mcosta thank you for your quick reply !

            What is the difference between -> and .set when using class methods on C++ ?
            And do you know why the -> works if I instance the QSerialPort as a pointer ?

            1 Reply Last reply Reply Quote 0
            • M
              mcosta last edited by mcosta

              Hi,

              in C++ you have to use . with objects and references and -> with pointers.
              Remember that to create an object in the heap (using pointers) you have to use the new operator

              QSerialPort *serial = new QSerialPort;
              
              serial->setPortNumber(port);
              ....
              

              *NOTE If you have this kind of doubt about the C++ language I suggest to study deeper it before go ahead with complex programs

              Once your problem is solved don't forget to:

              • Mark the thread as SOLVED using the Topic Tool menu
              • Vote up the answer(s) that helped you to solve the issue

              You can embed images using (http://imgur.com/) or (http://postimage.org/)

              1 Reply Last reply Reply Quote 0
              • First post
                Last post