[Solved]Connecting to a serial device



  • 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 ?



  • 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);
    


  • QSerialPort *serial;
    

    Instancing QSerialPort as a pointer works, why is this ?



  • 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 ?



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.