<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Serial terminal disconnecting unexpectedly]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">In my qt application I wanted to read data from a serial device every 3 seconds.<br />
Around 10 minutes the application is communicating with the device without any problem. But after some time there is no communication. If I am disconnecting and reconnecting the serial port then it will work. Why like this it is happening.<br />
Given below is my code</p>
<pre><code>void MainWindow::on_actionConnect_triggered()
{
    if(openserialFlag)                                         //connect to the serial terminal when this flag set
    {
        serial = new QSerialPort(this);

        OldComport = m_settingsDialog-&gt;settings().comport;
        COMport =OldComport;
        slaveID = m_settingsDialog-&gt;settings().SlaveID;
        serial-&gt;setPortName(COMport);  /*comport to modbus*/
        serial-&gt;setBaudRate(QSerialPort::Baud9600);                  //baud rate
        serial-&gt;setDataBits(QSerialPort::Data8);                     //Data bit
        serial-&gt;setParity(QSerialPort::NoParity);                    //no parity
        serial-&gt;setStopBits(QSerialPort::OneStop);                   //one stop bit
        serial-&gt;setFlowControl(QSerialPort::NoFlowControl);
        if (serial-&gt;open(QIODevice::ReadWrite)) {                    //open the serial port in read write mode
            openserialFlag=0;
            serialopenflag=1;
            statusBar()-&gt;showMessage("Connected");
            ui-&gt;actionConnect-&gt;setEnabled(false);     //disable the connect button
            ui-&gt;actionDisconnect-&gt;setEnabled(true);   //enable the disconnect button
            QDateTime now = QDateTime::currentDateTime();
            QString timestamp = now.toString(("yyyyMMdd"));
            QString filename = QString::fromLatin1("C:/Users/alphy/Google Drive/PROJECTS-Alphy/QT-Projects/HITECH/160520_GUISmartModule/SmartModuleGUI/Log Data/%1.txt").arg(timestamp);
            logFile.setFileName(filename);
            if (logFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
            {
                fileWrtOpenFlag =1;
            }
            else
            {
                statusBar()-&gt;showMessage("File open error");
            }
            connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));   //make the signal connecting to readdata slot
            connect(timer, SIGNAL(timeout()), this, SLOT(ReadRequest()));   //connect the timeout signal to readrequest slot
            timer-&gt;start(3000);                                             //start the timer timeout for every 2seconds

        } else {
            openserialFlag =1;
            QMessageBox::information(this,"Message","Device not connected");
        }
    }
}
void MainWindow::readData()
{
    //    timer-&gt;stop();
    //    readflag =1;
    QTextStream streamIn(&amp;logFile), streamOut(&amp;logFile);

    bool  ampMinFlag=0, amphrHlag=0, datareceivedFlag =0;
    float fmyvolage =0, fmycurrent =0,fmyAmpMin =0,fmyAmphr =0,fmyAmptotalizer =0;
    uint32_t u32myvolage =0,u32mycurrent =0,u32myAmpMin =0, u32myAmphr =0,u32myAmptotalizer =0;
    char *ProfName;
    uint8_t ProftimeHr =0, ProftimeMin =0, ProfElpsHr =0, ProfElpsMin =0;
    uint8_t glob_index =0,noOfReg =0, u8Index =0;
    statusBar()-&gt;clearMessage();             //clear the status bar

    memset(serialBuffer,0,sizeof (serialBuffer));
    //    //    delay(2500);
    //    serial-&gt;waitForReadyRead(50);
    //    //    qint64 ulLength= serial-&gt;bytesAvailable();
    //    Readdata = serial-&gt;readAll();
    QByteArray Readdata; //= data.toLocal8Bit();
    //    QByteArray responseData;
    //while (serial-&gt;waitForReadyRead(30))
    serial-&gt;waitForReadyRead(20);
    Readdata = serial-&gt;readAll();
    //    QByteArray Readdata;
    //        Readdata.append(data);
    for (int i=0;i&lt;Readdata.length();i++)
    {
        serialBuffer[glob_index++]=static_cast&lt;unsigned char&gt;(Readdata[i]);         //store the data read to an array
        noOfReg = serialBuffer[2];
        noOfReg =noOfReg+ 4;
        //        if(!writeFlag)                               //if write flag is not set then take the data length as 9byte
        //        {
        if(glob_index&gt;noOfReg)
        {
            glob_index=0;
            datareceivedFlag  =1;
            break;
        }
        //        }
    }
    if(datareceivedFlag){
        datareceivedFlag=0;
        crccalculated = crc16ForModbus(serialBuffer,(noOfReg -1 ));    //calculate the crc
        crcrecieved = static_cast&lt;quint16&gt;(serialBuffer[noOfReg-1] &lt;&lt; 8) | serialBuffer[noOfReg];
        if(crcrecieved==crccalculated)                  //check the crc received and crc calculated are equal
        {
            if(serialBuffer[u8Index++]==slaveID &amp;&amp; serialBuffer[u8Index++]==3)
            {
                u8Index++;
                u32myvolage = static_cast&lt;uint32_t&gt;(serialBuffer[u8Index++]&lt;&lt;24);
                u32myvolage |= static_cast&lt;uint32_t&gt;(serialBuffer[u8Index++]&lt;&lt;16);
                u32myvolage |= static_cast&lt;uint32_t&gt;(serialBuffer[u8Index++]&lt;&lt;8);
                u32myvolage |=static_cast&lt;uint32_t&gt;(serialBuffer[u8Index++]);
                fmyvolage = IEEE_HexToFloatPt(u32myvolage);
                ui-&gt;lineEdit_voltage-&gt;setText(QString::number(fmyvolage));
                //                ui-&gt;lineEdit_voltage-&gt;setText(QString::number(fmyvolage));

                u32mycurrent = static_cast&lt;uint32_t&gt;(serialBuffer[u8Index++])&lt;&lt;24;
                u32mycurrent |= static_cast&lt;uint32_t&gt;(serialBuffer[u8Index++])&lt;&lt;16;
                u32mycurrent |= static_cast&lt;uint32_t&gt;(serialBuffer[u8Index++]&lt;&lt;8);
                u32mycurrent |=static_cast&lt;uint32_t&gt;(serialBuffer[u8Index++]);
                fmycurrent = IEEE_HexToFloatPt(u32mycurrent);
                ui-&gt;lineEdit_Current-&gt;setText(QString::number(fmycurrent));

                if(serialBuffer[11]==0&amp;&amp;serialBuffer[12] ==0 &amp;&amp; serialBuffer[13]==0 &amp;&amp; serialBuffer[14] ==0)
                {
                    ampMinFlag=0;
                    amphrHlag=1;
                }
                else if(serialBuffer[15]==0&amp;&amp;serialBuffer[16] ==0 &amp;&amp; serialBuffer[17]==0 &amp;&amp; serialBuffer[18] ==0){
                    ampMinFlag=1;
                    amphrHlag=0;
                }
                if(ampMinFlag)
                {
                    u32myAmpMin = static_cast&lt;uint32_t&gt;(serialBuffer[u8Index++]&lt;&lt;24);
                    u32myAmpMin |= static_cast&lt;uint32_t&gt;(serialBuffer[u8Index++]&lt;&lt;16);
                    u32myAmpMin |= static_cast&lt;uint32_t&gt;(serialBuffer[u8Index++]&lt;&lt;8);
                    u32myAmpMin |=static_cast&lt;uint32_t&gt;(serialBuffer[u8Index++]);
                    fmyAmpMin = IEEE_HexToFloatPt(u32myAmpMin);
                    //                    fmyAmpMin =fmyAmpMin*1000;
                    ui-&gt;groupBox_AmpMINHR-&gt;setTitle("Current Ampere Minute");
                    ui-&gt;label_CrntAmpMinHr_unit-&gt;setText("Am");
                    ui-&gt;lineEdit_CrntAmp_MinHr-&gt;setText(QString::number(fmyAmpMin));
                    u8Index =19;
                }
                else if(amphrHlag){
                    u8Index =15;
                    u32myAmphr = static_cast&lt;uint32_t&gt;(serialBuffer[u8Index++]&lt;&lt;24);
                    u32myAmphr |= static_cast&lt;uint32_t&gt;(serialBuffer[u8Index++]&lt;&lt;16);
                    u32myAmphr |= static_cast&lt;uint32_t&gt;(serialBuffer[u8Index++]&lt;&lt;8);
                    u32myAmphr |=static_cast&lt;uint32_t&gt;(serialBuffer[u8Index++]);
                    fmyAmphr = IEEE_HexToFloatPt(u32myAmphr);
                    //                    fmyAmphr = fmyAmphr*60000;
                    ui-&gt;groupBox_AmpMINHR-&gt;setTitle("Current Ampere Hour");
                    ui-&gt;label_CrntAmpMinHr_unit-&gt;setText("Ah");
                    ui-&gt;lineEdit_CrntAmp_MinHr-&gt;setText(QString::number(fmyAmphr));
                    u8Index =19;
                }
                u32myAmptotalizer = static_cast&lt;uint32_t&gt;(serialBuffer[u8Index++]&lt;&lt;24);
                u32myAmptotalizer |= static_cast&lt;uint32_t&gt;(serialBuffer[u8Index++]&lt;&lt;16);
                u32myAmptotalizer |= static_cast&lt;uint32_t&gt;(serialBuffer[u8Index++]&lt;&lt;8);
                u32myAmptotalizer |=static_cast&lt;uint32_t&gt;(serialBuffer[u8Index++]);
                fmyAmptotalizer = IEEE_HexToFloatPt(u32myAmptotalizer);
                ui-&gt;lineEdit_Totalizer-&gt;setText(QString::number(fmyAmptotalizer));

                if(serialBuffer[23] ==0 &amp;&amp; serialBuffer[24] ==0 &amp;&amp; serialBuffer[25] ==0 &amp;&amp; serialBuffer[26] ==0 &amp;&amp; serialBuffer[27] ==0)
                {

                    if(noOfEntryJobnotSlct==0)
                    {
                        QSize availableSize = qApp-&gt;desktop()-&gt;availableGeometry().size();
                        int width = availableSize.width();
                        int height = availableSize.height();
                        width *= 0.6; // 90% of the screen size
                        height *= 0.6; // 90% of the screen size
                        QSize newSize( width, height );

                        setGeometry(
                                    QStyle::alignedRect(
                                        Qt::LeftToRight,
                                        Qt::AlignCenter,
                                        newSize,
                                        qApp-&gt;desktop()-&gt;availableGeometry()
                                        )
                                    );
                        ui-&gt;groupBox_JobProf-&gt;show();
                        noOfEntryJobnotSlct++;
                        No_ofEntry =0;
                    }
                    ui-&gt;groupBox_JobProf-&gt;hide();
                }
                else {
                    if(No_ofEntry==0)
                    {
                        noOfEntryJobnotSlct =0;
                        QSize availableSize = qApp-&gt;desktop()-&gt;availableGeometry().size();
                        int width = availableSize.width();
                        int height = availableSize.height();
                        width *= 0.6; // 90% of the screen size
                        height *= 0.9; // 90% of the screen size
                        QSize newSize( width, height );

                        setGeometry(
                                    QStyle::alignedRect(
                                        Qt::LeftToRight,
                                        Qt::AlignCenter,
                                        newSize,
                                        qApp-&gt;desktop()-&gt;availableGeometry()
                                        )
                                    );
                        ui-&gt;groupBox_JobProf-&gt;show();
                        No_ofEntry++;
                    }
                    //                    ProfName[0]= serialBuffer[u8Index++];
                    //                    ProfName[1]= serialBuffer[u8Index++];
                    //                    ProfName[2]= serialBuffer[u8Index++];
                    //                    ProfName[3]= serialBuffer[u8Index++];
                    //                    ProfName[4]= serialBuffer[u8Index++];
                    QString ProfileName;             //QString::fromUtf8(ProfName);
                    ProfileName.append(serialBuffer[u8Index++]);
                    ProfileName.append(serialBuffer[u8Index++]);
                    ProfileName.append(serialBuffer[u8Index++]);
                    ProfileName.append(serialBuffer[u8Index++]);
                    ProfileName.append(serialBuffer[u8Index++]);
                    u8Index++;
                    ProftimeHr = serialBuffer[u8Index++]&lt;&lt;8;
                    ProftimeHr |= serialBuffer[u8Index++];
                    ProftimeMin = serialBuffer[u8Index++]&lt;&lt;8;
                    ProftimeMin |= serialBuffer[u8Index++];
                    ProfElpsHr = serialBuffer[u8Index++]&lt;&lt;8;
                    ProfElpsHr |= serialBuffer[u8Index++];
                    ProfElpsMin = serialBuffer[u8Index++]&lt;&lt;8;
                    ProfElpsMin |= serialBuffer[u8Index++];

                    ui-&gt;groupBox_JobProf-&gt;setTitle(ProfileName);
                    QString ProfTime = QString::number(ProftimeHr);
                    QString ProfTimeMin = QString::number(ProftimeMin);
                    ProfTime.append(":");
                    ProfTime.append(ProfTimeMin);
                    ui-&gt;lineEdit_JobTime-&gt;setText(ProfTime);
                    QString ElapsTime = QString::number(ProfElpsHr);
                    QString ProfElpsedMin = QString::number(ProfElpsMin);
                    ElapsTime.append(":");
                    ElapsTime.append(ProfElpsedMin);
                    ui-&gt;lineEdit_ElpsTime-&gt;setText(ElapsTime);
                }
                if(fileWrtOpenFlag)
                {
                    streamOut&lt;&lt; "Voltage:  " &lt;&lt; fmyvolage &lt;&lt;"\t" &lt;&lt; "Current:  " &lt;&lt; fmycurrent&lt;&lt;"\r\n";
                    logFile.flush();
                }

                //                logFile.close();
                //                                 }
                readflag =0;
            }
        }
    }
//    serial-&gt;flush();
    serial-&gt;clear(QSerialPort::Directions(1|2));
    Readdata.clear();
    memset(serialBuffer,0,sizeof (serialBuffer));
    //    timer-&gt;start(3000);
}
void MainWindow::ReadRequest()
{
    //    timer-&gt;stop();
    unsigned char data[10];
    data[0]= slaveID;
    data[1]=3;     //Function code
    data[2]=0;     //MSB start register
    data[3]=0;     //LSB start register
    data[4]=0;     //MSB no.of register
    data[5]=17;    //LSB no.of register (no.of reister to read 30)
    //calculate crc
    crccalculated = crc16ForModbus(data,6);   //calculate the CRC of thw data to send
    data[6]=static_cast&lt;unsigned char&gt;(crccalculated&gt;&gt;8);
    data[7]=static_cast&lt;unsigned char&gt;(crccalculated);
    QByteArray datatowrite = QByteArray(reinterpret_cast&lt;char*&gt;(data), sizeof (data)); //convert cahr to bytearray
    //    QByteArray inttobyte;// = QByteArray::number(crccalculated,16);
    //     inttobyte.setNum(crccalculated);
    //        QDataStream out(&amp;datatowrite, QIODevice::WriteOnly | QIODevice::Append);            //append the CRC calculated to the data to write
    //        out.setByteOrder(QDataStream::BigEndian);                                          //set the byte order in bigendian
    //        out &lt;&lt; crccalculated;                                                             //append the crc calculated to the data
    //        out&gt;&gt;datatosend;                                                                 //data from the out to qbytearray
    //    datatowrite.append(inttobyte);

    serial-&gt;write(datatowrite);
    serial-&gt;flush();
    datatowrite.clear();
    memset(data,0,sizeof (data));

}

</code></pre>
<p dir="auto">Thank you,<br />
Alphy</p>
]]></description><link>https://forum.qt.io/topic/114998/serial-terminal-disconnecting-unexpectedly</link><generator>RSS for Node</generator><lastBuildDate>Tue, 17 Nov 2026 04:06:51 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/114998.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 20 May 2020 04:56:27 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Serial terminal disconnecting unexpectedly on Wed, 20 May 2020 11:47:39 GMT]]></title><description><![CDATA[<p dir="auto">@J-Hilk said in <a href="/post/595822">Serial terminal disconnecting unexpectedly</a>:</p>
<blockquote>
<p dir="auto">of course not, and there is no guarantee that 20ms will be enough each and every time!<br />
If new data arrives readyread will signal again. Your job as a programmer is to store the data, identify if all is arrived, and only then do your processing.</p>
</blockquote>
<p dir="auto">Thank you so much for your quick reply. I store the data in an array and wait for the entire data to come. Now there is no problem with serial communication.</p>
]]></description><link>https://forum.qt.io/post/595861</link><guid isPermaLink="true">https://forum.qt.io/post/595861</guid><dc:creator><![CDATA[Alphy]]></dc:creator><pubDate>Wed, 20 May 2020 11:47:39 GMT</pubDate></item><item><title><![CDATA[Reply to Serial terminal disconnecting unexpectedly on Wed, 20 May 2020 08:16:07 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alphy">@<bdi>Alphy</bdi></a> said in <a href="/post/595818">Serial terminal disconnecting unexpectedly</a>:</p>
<blockquote>
<p dir="auto">If I am not giving serial-&gt;waitForReadyRead(20); command then the application won't get the entire data the device send.</p>
</blockquote>
<p dir="auto">of course not, and there is no guarantee that 20ms will be enough each and every time!</p>
<p dir="auto">If new data arrives readyread will signal again. Your job as a programmer is to store the data, identify if all is arrived, and only then do your processing.</p>
<p dir="auto">If you call waitForReady read, inside the slot the signal will not be reemitted.</p>
<p dir="auto">and since you also send a new datagram from the slot, I'm pretty sure it breaks here.</p>
<blockquote>
<p dir="auto">Qt:Qt Creator 4.9.2</p>
</blockquote>
<p dir="auto">Qt version(the one from your kit) not QtCreator QSerialPort has gone through major revisions lately and perhaps you're on an old/faulty one.</p>
]]></description><link>https://forum.qt.io/post/595822</link><guid isPermaLink="true">https://forum.qt.io/post/595822</guid><dc:creator><![CDATA[J.Hilk]]></dc:creator><pubDate>Wed, 20 May 2020 08:16:07 GMT</pubDate></item><item><title><![CDATA[Reply to Serial terminal disconnecting unexpectedly on Wed, 20 May 2020 08:01:44 GMT]]></title><description><![CDATA[<p dir="auto">@J-Hilk said in <a href="/post/595766">Serial terminal disconnecting unexpectedly</a>:</p>
<blockquote>
<p dir="auto">do not mix waitForX and  signal &amp; slots. Stick with one, with SIGNAL and SLOTS</p>
</blockquote>
<p dir="auto">If I am not giving serial-&gt;waitForReadyRead(20); command then the application won't get the entire data the device send.</p>
<blockquote>
<p dir="auto">do not write in the slot connected to the ready read signal, IIRC there were some problems with that<br />
is that means to call a new method from the readData Slot.<br />
What os and what Qt version are you using?<br />
OS:Windows10<br />
Qt:Qt Creator 4.9.2</p>
</blockquote>
<p dir="auto">Thank you,<br />
Alphy</p>
]]></description><link>https://forum.qt.io/post/595818</link><guid isPermaLink="true">https://forum.qt.io/post/595818</guid><dc:creator><![CDATA[Alphy]]></dc:creator><pubDate>Wed, 20 May 2020 08:01:44 GMT</pubDate></item><item><title><![CDATA[Reply to Serial terminal disconnecting unexpectedly on Wed, 20 May 2020 05:28:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alphy">@<bdi>Alphy</bdi></a><br />
a few things to keep in mind</p>
<ul>
<li>do not mix waitForX and  signal &amp; slots. Stick with one, with <strong>SIGNAL</strong> and <strong>SLOTS</strong></li>
<li>do not write in the slot connected to the ready read signal, IIRC there were some problems with that</li>
<li>What os and what Qt version are you using?</li>
</ul>
]]></description><link>https://forum.qt.io/post/595766</link><guid isPermaLink="true">https://forum.qt.io/post/595766</guid><dc:creator><![CDATA[J.Hilk]]></dc:creator><pubDate>Wed, 20 May 2020 05:28:19 GMT</pubDate></item></channel></rss>