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]Connecting to a serial device

[Solved]Connecting to a serial device

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.3k 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.
  • A Offline
    A Offline
    Alfredo Palhares
    wrote on last edited by Alfredo Palhares
    #1

    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
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      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
      1
      • A Offline
        A Offline
        Alfredo Palhares
        wrote on last edited by
        #3
        QSerialPort *serial;
        

        Instancing QSerialPort as a pointer works, why is this ?

        1 Reply Last reply
        0
        • M mcosta

          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);
          
          A Offline
          A Offline
          Alfredo Palhares
          wrote on last edited by
          #4

          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
          0
          • M Offline
            M Offline
            mcosta
            wrote on last edited by mcosta
            #5

            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
            0

            • Login

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