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 from QT app to STM32 by serial port communication
Forum Updated to NodeBB v4.3 + New Features

How to send data from QT app to STM32 by serial port communication

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 2.4k 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.
  • H Offline
    H Offline
    Hamza.B
    wrote on 16 Dec 2019, 10:35 last edited by aha_1980
    #1

    I am tring to send data from my QT app to the STM32F7 board using USB serial communication.
    from the board side i used STM32 USB CDC class, i connect the card to the PC then i can open the usb port using realterm or herculus, but i can't open it with Qt .
    i get this code error : QSerialPort::SerialPortError(UnsupportedOperationError)
    i am developping on windows 10.
    this is the code:

    QList<QSerialPortInfo> UsbPorts;
    UsbPorts =   QSerialPortInfo::availablePorts();
    
    qDebug() << "Number of serial ports:" << QSerialPortInfo::availablePorts().count();
    
    foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
    {
                    qDebug()<<"Name: "<<info.portName();
                    qDebug()<<"Description: "<<info.description();
                    qDebug()<<"Manufactures: "<<info.manufacturer();
                    
                    QSerialPort *port = new QSerialPort(info);               
                    if (port->open(QIODevice::ReadWrite)) {
                                qDebug() << "Baud rate:" << port->baudRate();
                                qDebug() << "Data bits:" << port->dataBits();
                                qDebug() << "Stop bits:" << port->stopBits();
                                qDebug() << "Parity:" << port->parity();
                                qDebug() << "Flow control:" << port->flowControl();
                                qDebug() << "Read buffer size:" << port->readBufferSize();
                                qDebug() << "Read buffer size:" <<port->portName();
                                
                                port->close();
    
                        } else {
                                qDebug() << "Unable to open port, error code" << port->error();
                        }
                }
    

    [Edit aha_1980: added code tags]

    K 1 Reply Last reply 16 Dec 2019, 10:56
    0
    • H Hamza.B
      16 Dec 2019, 10:35

      I am tring to send data from my QT app to the STM32F7 board using USB serial communication.
      from the board side i used STM32 USB CDC class, i connect the card to the PC then i can open the usb port using realterm or herculus, but i can't open it with Qt .
      i get this code error : QSerialPort::SerialPortError(UnsupportedOperationError)
      i am developping on windows 10.
      this is the code:

      QList<QSerialPortInfo> UsbPorts;
      UsbPorts =   QSerialPortInfo::availablePorts();
      
      qDebug() << "Number of serial ports:" << QSerialPortInfo::availablePorts().count();
      
      foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
      {
                      qDebug()<<"Name: "<<info.portName();
                      qDebug()<<"Description: "<<info.description();
                      qDebug()<<"Manufactures: "<<info.manufacturer();
                      
                      QSerialPort *port = new QSerialPort(info);               
                      if (port->open(QIODevice::ReadWrite)) {
                                  qDebug() << "Baud rate:" << port->baudRate();
                                  qDebug() << "Data bits:" << port->dataBits();
                                  qDebug() << "Stop bits:" << port->stopBits();
                                  qDebug() << "Parity:" << port->parity();
                                  qDebug() << "Flow control:" << port->flowControl();
                                  qDebug() << "Read buffer size:" << port->readBufferSize();
                                  qDebug() << "Read buffer size:" <<port->portName();
                                  
                                  port->close();
      
                          } else {
                                  qDebug() << "Unable to open port, error code" << port->error();
                          }
                  }
      

      [Edit aha_1980: added code tags]

      K Offline
      K Offline
      KroMignon
      wrote on 16 Dec 2019, 10:56 last edited by KroMignon
      #2

      @Hamza-B First there is a memory leak in your code.. I would suggest you not to use pointer there.

      Second, as mentioned in the documentation, try to open a QSerialPort with invalid settings ends with an error:

      Note: The method returns false if opening the port is successful, but could not set any of the port settings successfully. In that case, the port is closed automatically not to leave the port around with incorrect settings.

      So before opening the serial port, setup the communication.

      To resume:

      for(const auto &info : QSerialPortInfo::availablePorts())
      {
          qDebug()<<"Name: "<<info.portName();
          qDebug()<<"Description: "<<info.description();
          qDebug()<<"Manufactures: "<<info.manufacturer();
          
          QSerialPort port(info);
          
          port.setBaudRate (QSerialPort::Baud115200);
          port.setDataBits(QSerialPort::Data8);
          port.setParity (QSerialPort::NoParity);
          port.setStopBits (QSerialPort::OneStop);
          
          port.setFlowControl(QSerialPort::NoFlowControl);
          if (port.open(QIODevice::ReadWrite)) {
              qDebug() << "Baud rate:" << port.baudRate();
              qDebug() << "Data bits:" << port.dataBits();
              qDebug() << "Stop bits:" << port.stopBits();
              qDebug() << "Parity:" << port.parity();
              qDebug() << "Flow control:" << port.flowControl();
              qDebug() << "Read buffer size:" << port.readBufferSize();
              qDebug() << "Read buffer size:" <<port.portName();
              
              port.close();
      
          } else {
              qDebug() << "Unable to open port, error code" << port.error();
          }
      }
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      H 1 Reply Last reply 16 Dec 2019, 15:39
      0
      • K Offline
        K Offline
        kuzulis
        Qt Champions 2020
        wrote on 16 Dec 2019, 12:33 last edited by kuzulis
        #3

        i get this code error : QSerialPort::SerialPortError(UnsupportedOperationError)

        If your provided code is an 'actual' code, then this error can be caused if you have not implemented some calls in your USB CDC class.

        Also, you can re-build the QSerialPort yourself, add there the qDebug() output and to see where it fails. Look in the QSerialPort sources on a function QSerialPortPrivate::open: https://github.com/qt/qtserialport/blob/5.13/src/serialport/qserialport_win.cpp#L176

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kuzulis
          Qt Champions 2020
          wrote on 16 Dec 2019, 12:34 last edited by
          #4

          @KroMignon said in How to send data from QT app to STM32 by serial port communication:

          So before opening the serial port, setup the communication.

          No, that's not true.

          K 1 Reply Last reply 16 Dec 2019, 12:49
          0
          • K kuzulis
            16 Dec 2019, 12:34

            @KroMignon said in How to send data from QT app to STM32 by serial port communication:

            So before opening the serial port, setup the communication.

            No, that's not true.

            K Offline
            K Offline
            KroMignon
            wrote on 16 Dec 2019, 12:49 last edited by
            #5

            @kuzulis said in How to send data from QT app to STM32 by serial port communication:

            No, that's not true.

            what is not true?

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kuzulis
              Qt Champions 2020
              wrote on 16 Dec 2019, 12:57 last edited by
              #6

              @KroMignon said in How to send data from QT app to STM32 by serial port communication:

              what is not true?

              This:

              "So before opening the serial port, setup the communication."

              1 Reply Last reply
              1
              • K KroMignon
                16 Dec 2019, 10:56

                @Hamza-B First there is a memory leak in your code.. I would suggest you not to use pointer there.

                Second, as mentioned in the documentation, try to open a QSerialPort with invalid settings ends with an error:

                Note: The method returns false if opening the port is successful, but could not set any of the port settings successfully. In that case, the port is closed automatically not to leave the port around with incorrect settings.

                So before opening the serial port, setup the communication.

                To resume:

                for(const auto &info : QSerialPortInfo::availablePorts())
                {
                    qDebug()<<"Name: "<<info.portName();
                    qDebug()<<"Description: "<<info.description();
                    qDebug()<<"Manufactures: "<<info.manufacturer();
                    
                    QSerialPort port(info);
                    
                    port.setBaudRate (QSerialPort::Baud115200);
                    port.setDataBits(QSerialPort::Data8);
                    port.setParity (QSerialPort::NoParity);
                    port.setStopBits (QSerialPort::OneStop);
                    
                    port.setFlowControl(QSerialPort::NoFlowControl);
                    if (port.open(QIODevice::ReadWrite)) {
                        qDebug() << "Baud rate:" << port.baudRate();
                        qDebug() << "Data bits:" << port.dataBits();
                        qDebug() << "Stop bits:" << port.stopBits();
                        qDebug() << "Parity:" << port.parity();
                        qDebug() << "Flow control:" << port.flowControl();
                        qDebug() << "Read buffer size:" << port.readBufferSize();
                        qDebug() << "Read buffer size:" <<port.portName();
                        
                        port.close();
                
                    } else {
                        qDebug() << "Unable to open port, error code" << port.error();
                    }
                }
                
                H Offline
                H Offline
                Hamza.B
                wrote on 16 Dec 2019, 15:39 last edited by
                #7

                @KroMignon what you said was correct, i must set the communication first , then i sent the data and everything worked just fine . thanks

                K 1 Reply Last reply 16 Dec 2019, 15:42
                0
                • H Hamza.B
                  16 Dec 2019, 15:39

                  @KroMignon what you said was correct, i must set the communication first , then i sent the data and everything worked just fine . thanks

                  K Offline
                  K Offline
                  KroMignon
                  wrote on 16 Dec 2019, 15:42 last edited by
                  #8

                  @Hamza-B Your welcome

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kuzulis
                    Qt Champions 2020
                    wrote on 16 Dec 2019, 19:21 last edited by kuzulis
                    #9

                    @Hamza-B said in How to send data from QT app to STM32 by serial port communication:

                    , i must set the communication first

                    Whats? By default the QSP opens in 9600 8 N 1.. So, it is strange why this defaults does not work for you.. Maybe 9600 baud does not supported by your USB CDC class?

                    H 1 Reply Last reply 17 Dec 2019, 16:27
                    0
                    • K kuzulis
                      16 Dec 2019, 19:21

                      @Hamza-B said in How to send data from QT app to STM32 by serial port communication:

                      , i must set the communication first

                      Whats? By default the QSP opens in 9600 8 N 1.. So, it is strange why this defaults does not work for you.. Maybe 9600 baud does not supported by your USB CDC class?

                      H Offline
                      H Offline
                      Hamza.B
                      wrote on 17 Dec 2019, 16:27 last edited by
                      #10

                      @kuzulis i set the baud on 9600

                      1 Reply Last reply
                      0

                      1/10

                      16 Dec 2019, 10:35

                      • Login

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