Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QSerialPort is(n't) sending data to ESP8266



  • Hi,

    this is my first time I work with QSerialPort and I try to send data to an ESP8266. I tried the tool "Serial Port Monitor" to make a test message to the ESP and the connection is working so the problem shouldn't be on the ESP's side.

    QSerialPort serialo;
    
            Q_FOREACH(QSerialPortInfo port, QSerialPortInfo::availablePorts()) {
    
                if (port.description().compare("Silicon Labs CP210x USB to UART Bridge") == 0) {
                    serialo.setPort(port);
                    qDebug() << serialo.portName();
                    serialo.setBaudRate(QSerialPort::Baud9600);
                    serialo.setDataBits(QSerialPort::Data8);
                    serialo.setParity(QSerialPort::NoParity);
                    serialo.setStopBits(QSerialPort::OneStop);
                    serialo.setFlowControl(QSerialPort::NoFlowControl);
                    qDebug() << "PortOpen: " << serialo.open(QIODevice::ReadWrite);
                    qDebug() << "BytesSend: " << serialo.write("test");
    
                    serialo.close();
    
                }
            }
    

    I wrote this. Qt can open the port and it sends 4 Bytes, but the ESP isn't getting anything.

    Am I doing something completely wrong over there? Or is it something like a known issue or something?

    Edit: The ESP is connected via USB



  • You close the serial port before that the write starts. QSP::write() does not write the data immediatelly, it starts writing when the QSP becomes ready to write, internally.



  • Oh, ok. Is there a function to wait for writing is done?


  • Moderators

    @Gh0stShell said in QSerialPort is(n't) sending data to ESP8266:

    Oh, ok. Is there a function to wait for writing is done?

    yes, but rather use the BytesWritten signal.



  • @Gh0stShell said in QSerialPort is(n't) sending data to ESP8266:

    Oh, ok. Is there a function to wait for writing is done?

    Yes, you can use QSP::waitForBytesWritten(), but it is a very bad design. You should create your serial port instance on a heap and to use the signals/slots instead, as @J-Hilk suggested.



  • Thank you, the problem was, that I didn't wait for the Bytes to be written.


Log in to reply