Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QSerialPort Close unexpectedly
Qt 6.12 LTS is out! See what's new in the release blog

QSerialPort Close unexpectedly

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
2 Posts 2 Posters 628 Views 1 Watching
  • 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
    Ashish Epsilon
    wrote on last edited by
    #1

    Hi all,

    I am using QSerialPort Module for communicating with the Esp32 When the Esp32 requests the data Qt application gives the data it will work fine for around 4 hours but after some time Qserialport close unexpectedly.

    However, When I Disconnect and reconnect Qserialport it will work again for around 4 hours and this cycle repeats.

    Does anyone have any idea what's going on...

    Here is my Code for the serial port.

    m_serialport::m_serialport(QObject *parent)
    : QObject{parent}
    {

    }

    void m_serialport::init_Serial_Port()
    {
    m_serial = new QSerialPort();
    }

    uint8_t m_serialport::Open_Serial_Port()
    {
    m_serial->setPortName(Serial_Port_Settings.Port);
    qDebug() << m_serial->setBaudRate(Serial_Port_Settings.baud);
    m_serial->setDataBits(Serial_Port_Settings.dataBits);
    m_serial->setParity(Serial_Port_Settings.parity);
    m_serial->setStopBits(Serial_Port_Settings.stopBits);
    m_serial->setFlowControl(QSerialPort::NoFlowControl);

    if (m_serial->open(QIODevice::ReadWrite)) {
    
        return QSerialPort::NoError;
    
    } else {
    
        Serial_Port_Error_String = m_serial->errorString();
    
        return QSerialPort::DeviceNotFoundError;
    
    }
    
    return QSerialPort::DeviceNotFoundError;
    

    }

    void m_serialport::Close_Serial_Port()
    {
    m_serial->close();
    }

    int ack_count =0;
    QByteArray data ="";

    void m_serialport::Read__data()
    {

    qDebug() << "Serial running " << QThread::currentThread();
    
    QByteArray serial_data = m_serial->readAll();
    
    qDebug() << " data =  " << serial_data;
    qDebug() << " last =  " << serial_data[serial_data.size()-1];
    
    if(serial_data[serial_data.size()-1] == '#')
    {
        serial_data[serial_data.size()-1] = ' ';
        if(serial_data[0] == '@')
        {
            data = serial_data;
        }
        else
        {
            data.append(serial_data);
        }
        data[0] = ' ';
    
        QJsonObject data_json ;
    
        QJsonDocument jsonDocument = QJsonDocument::fromJson(data);
    
        if (!jsonDocument.isNull() && jsonDocument.isObject())
        {
            data_json = jsonDocument.object();
        }
        qDebug() << data_json;
      
        if(data_json.contains("Request") == true  && data_json.value("Request").toString() == "DATA")
        {
        
              // logic portion that will collect data from JSON object and make packet 
    
                    QJsonValue value = jsonDoc.object().value(key);
                    if( jsonDoc.object().value(key).isObject())
                    {    qDebug() << "Key = " << key << "is group ";
                        foreach(const QString& key1, jsonDoc.object().value(key).toObject().keys()) {
                            QJsonValue value1 = jsonDoc.object().value(key).toObject().value(key1);
                            qDebug() << "Key = " << key1 << ", Value = " << value1.toString();
                        }
                    }
                    else
                    {
                        qDebug() << "Key = " << key << ", Value = " << value.toString();
                    }
                
                m_serial->write(jsonDoc.toJson(QJsonDocument::Compact) + '\0');
            
    
        }
        else
        {
            DATA_Read->Add_Modbus_write_string(data);
        }
    }
    else
    {
        if(serial_data[0] == '@')
        {
            data = serial_data;
        }
        else if(serial_data[0] != '@')
        {
            data.append(serial_data);
        }
    }
    

    }

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Did you check your system logs to see if there's anything happening at that time ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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