<?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[Error while writing and reading (SerialPort)]]></title><description><![CDATA[<p dir="auto">Hello There!</p>
<p dir="auto">I´m new in Qt and doing Serial Communication. I need to make the connection between a GPS receiver and my PC, the connection is correct, and the reading is ok as well... but there is a problem, when a try to write and send commands to the GPS always got something wrong...<br />
When I was able to send the information correctly, something strange happends. If a configure the GPS outside QT (RealTerm), and reconfigure with my code, the response that supose to give me just 2 short messages in the command window, get stuck with the same message and sends me infinite number of them</p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/5789dae1-f2ba-4fb8-b799-a3af9846b2f7.png" alt="0_1566857135181_error1.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">if there is no previous configuration, the program doesn't send anything, at least that seems to be.</p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/eb8d68b7-bee0-46a7-91d3-c979d206e809.png" alt="0_1566857202192_error2.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">This is my code</p>
<pre><code>#include "myserial.h"
#include &lt;QtSerialPort/QSerialPort&gt;
#include &lt;QObject&gt;
#include &lt;QtWidgets&gt;
#include &lt;QFile&gt;
#include &lt;QTextStream&gt;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

        //MySerialPort();

        MySerialPort iSerialPort;
        iSerialPort.openSerialPort();

        return a.exec();
}


MySerialPort::MySerialPort()
{

    serial = new QSerialPort(this);
    connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
    //openSerialPort();

}

void MySerialPort::openSerialPort()
{
    QString nombre;

    const auto infos = QSerialPortInfo::availablePorts();
    for (const QSerialPortInfo &amp;info : infos) {
        QString puerto = info.portName();
        if ( info.manufacturer() == "Prolific" )
        {
            nombre = puerto;
            qDebug().noquote() &lt;&lt; "El GPS se encuentra conectado en el puerto:" &lt;&lt; nombre;
        }
    }

    serial-&gt;setPortName(nombre);
    serial-&gt;setBaudRate(QSerialPort::Baud9600);
    serial-&gt;setDataBits(QSerialPort::Data8);
    serial-&gt;setParity(QSerialPort::NoParity);
    serial-&gt;setStopBits(QSerialPort::OneStop);
    serial-&gt;setFlowControl(QSerialPort::NoFlowControl);
    if (serial-&gt;open(QIODevice::ReadWrite)) {

        showStatusMessage("Contectado a GPS");

    } else {

        showStatusMessage(tr("Open error"));
    }
}

void MySerialPort::closeSerialPort()
{
    if (serial-&gt;isOpen())
        serial-&gt;close();

    showStatusMessage(tr("Disconnected"));
}

void MySerialPort::sendData()
{
    static char Cf[]={0x40, 0x40, 0x43, 0x66, 0x25, 0x0D, 0x0A};
    for (int i=0; i &lt; 7; i++)
        {
            serial-&gt;putChar(Cf[i]);
        }/*
    static char Gd[]={0x40, 0x40, 0x47, 0x64, 0x03, 0x20, 0x0D, 0x0A};
    for (int i=0; i &lt; 8; i++)
        {
            serial-&gt;putChar(Gd[i]);
        }
    static char Ge[]={0x40, 0x40, 0x47, 0x65, 0x01, 0x23, 0x0D, 0x0A};
    for (int i=0; i &lt; 8; i++)
        {
            serial-&gt;putChar(Ge[i]);
        }*/
    static char Hn[]={0x40, 0x40, 0x48, 0x6e, 0x02, 0x24, 0x0D, 0x0A};
    for (int i=0; i &lt; 8; i++)
        {
            serial-&gt;putChar(Hn[i]);
        }

}

void MySerialPort::readData()
{

    sendData();


    QByteArray data = serial-&gt;readAll();

    // qDebug() &lt;&lt; data;

    QByteArray received = data.toHex();

    qDebug()&lt;&lt;"testing :"&lt;&lt;received;


   


}

void MySerialPort::handleError(QSerialPort::SerialPortError error)
{
    if (error == QSerialPort::ResourceError) {
        closeSerialPort();
    }
}


void MySerialPort::showStatusMessage(const QString &amp;message)
{
    qDebug() &lt;&lt; message;
}


</code></pre>
<p dir="auto">Any help would be nice, thank you</p>
]]></description><link>https://forum.qt.io/topic/106274/error-while-writing-and-reading-serialport</link><generator>RSS for Node</generator><lastBuildDate>Sat, 18 Apr 2026 01:05:14 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/106274.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 26 Aug 2019 22:11:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Error while writing and reading (SerialPort) on Tue, 27 Aug 2019 20:02:51 GMT]]></title><description><![CDATA[<p dir="auto">Its works now!!</p>
<p dir="auto">I better call both functions in order to execute one at time, used the readyRead signal</p>
<pre><code>...
MySerialPort::MySerialPort()
{

    serial = new QSerialPort(this);
    connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
...

    serial-&gt;setPortName(nombre);
    serial-&gt;setBaudRate(QSerialPort::Baud9600);
    serial-&gt;setDataBits(QSerialPort::Data8);
    serial-&gt;setParity(QSerialPort::NoParity);
    serial-&gt;setStopBits(QSerialPort::OneStop);
    serial-&gt;setFlowControl(QSerialPort::NoFlowControl);
    if (serial-&gt;open(QIODevice::ReadWrite))
    {

        showStatusMessage("Contectado a GPS");

    } else {

        showStatusMessage(tr("Open error"));
    }

    sendData();

    readData();

}

...
...
</code></pre>
<p dir="auto">Used "waitForBytesWritten()" at the end of writing, and "QIODevice::readyRead();" before start reading, so there is no failure now. Thanks everybody!</p>
]]></description><link>https://forum.qt.io/post/548145</link><guid isPermaLink="true">https://forum.qt.io/post/548145</guid><dc:creator><![CDATA[Elite_Shergio]]></dc:creator><pubDate>Tue, 27 Aug 2019 20:02:51 GMT</pubDate></item><item><title><![CDATA[Reply to Error while writing and reading (SerialPort) on Tue, 27 Aug 2019 17:48:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/hskoglund">@<bdi>hskoglund</bdi></a></p>
<p dir="auto">H</p>
<p dir="auto">Now I get that, will use "readyRead()" and won´t ask for inmmediate answer haha thanks!</p>
]]></description><link>https://forum.qt.io/post/548117</link><guid isPermaLink="true">https://forum.qt.io/post/548117</guid><dc:creator><![CDATA[Elite_Shergio]]></dc:creator><pubDate>Tue, 27 Aug 2019 17:48:10 GMT</pubDate></item><item><title><![CDATA[Reply to Error while writing and reading (SerialPort) on Tue, 27 Aug 2019 17:30:41 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/aha_1980">@<bdi>aha_1980</bdi></a></p>
<p dir="auto">Hi!<br />
Didn´t know that module exists, will take a look</p>
]]></description><link>https://forum.qt.io/post/548116</link><guid isPermaLink="true">https://forum.qt.io/post/548116</guid><dc:creator><![CDATA[Elite_Shergio]]></dc:creator><pubDate>Tue, 27 Aug 2019 17:30:41 GMT</pubDate></item><item><title><![CDATA[Reply to Error while writing and reading (SerialPort) on Tue, 27 Aug 2019 13:11:51 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/hskoglund">@<bdi>hskoglund</bdi></a> said in <a href="/post/548083">Error while writing and reading (SerialPort)</a>:</p>
<blockquote>
<p dir="auto">Problem was: readyRead() signal is connected ok, but transmitting the 15 characters inside the readyRead() signal handler and expecting an immediate answer will fail, so I tried to explain that serial communication is a sloooow business.</p>
</blockquote>
<p dir="auto">That is fully correct. But sending the characters out will lead to another answer, i.e. another <code>readyRead()</code> signal and there the answer can be handled.</p>
<p dir="auto">Regards</p>
]]></description><link>https://forum.qt.io/post/548085</link><guid isPermaLink="true">https://forum.qt.io/post/548085</guid><dc:creator><![CDATA[aha_1980]]></dc:creator><pubDate>Tue, 27 Aug 2019 13:11:51 GMT</pubDate></item><item><title><![CDATA[Reply to Error while writing and reading (SerialPort) on Tue, 27 Aug 2019 13:06:44 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/aha_1980">@<bdi>aha_1980</bdi></a> said in <a href="/post/548037">Error while writing and reading (SerialPort)</a>:</p>
<blockquote>
<blockquote>
<p dir="auto">try waiting 15 milliseconds for the 15 characters to be transmitted and then wait at least 15 milliseconds more the characters to arrive, before doing serial-&gt;readAll()</p>
</blockquote>
<p dir="auto">No, that is wrong.</p>
</blockquote>
<p dir="auto">Yes, agreed it's a dumb way. (Also waitForBytesWritten() has a history of being unreliable, at least on Windows.)</p>
<p dir="auto">Problem was: <code>readyRead()</code> signal is connected ok, but transmitting the 15 characters inside the <code>readyRead()</code> signal handler and expecting an immediate answer will fail, so I tried to explain that serial communication is a sloooow business.</p>
]]></description><link>https://forum.qt.io/post/548083</link><guid isPermaLink="true">https://forum.qt.io/post/548083</guid><dc:creator><![CDATA[hskoglund]]></dc:creator><pubDate>Tue, 27 Aug 2019 13:06:44 GMT</pubDate></item><item><title><![CDATA[Reply to Error while writing and reading (SerialPort) on Tue, 27 Aug 2019 06:24:23 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/elite_shergio">@<bdi>elite_shergio</bdi></a></p>
<p dir="auto">By the way, can't you use the <a href="https://doc.qt.io/qt-5/qtpositioning-index.html" target="_blank" rel="noopener noreferrer nofollow ugc">Qt positioning</a> module?</p>
]]></description><link>https://forum.qt.io/post/548039</link><guid isPermaLink="true">https://forum.qt.io/post/548039</guid><dc:creator><![CDATA[aha_1980]]></dc:creator><pubDate>Tue, 27 Aug 2019 06:24:23 GMT</pubDate></item><item><title><![CDATA[Reply to Error while writing and reading (SerialPort) on Tue, 27 Aug 2019 06:20:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/hskoglund">@<bdi>hskoglund</bdi></a></p>
<p dir="auto">While I fully agree to the first part of your answer, I object to the second one.</p>
<blockquote>
<p dir="auto">try waiting 15 milliseconds for the 15 characters to be transmitted and then wait at least 15 milliseconds more the characters to arrive, before doing serial-&gt;readAll()</p>
</blockquote>
<p dir="auto">No, that is wrong.</p>
<p dir="auto">The correct way is to use a slot connected to <code>readyRead()</code> and read the incoming data there. Once the device answers, you will get the data there.</p>
<p dir="auto">Everything else, including <code>waitForBytesWritten()</code> is contra-productive (except if running in a separate thread).</p>
<p dir="auto">Regards</p>
]]></description><link>https://forum.qt.io/post/548037</link><guid isPermaLink="true">https://forum.qt.io/post/548037</guid><dc:creator><![CDATA[aha_1980]]></dc:creator><pubDate>Tue, 27 Aug 2019 06:20:03 GMT</pubDate></item><item><title><![CDATA[Reply to Error while writing and reading (SerialPort) on Tue, 27 Aug 2019 00:21:38 GMT]]></title><description><![CDATA[<p dir="auto">Hi, the connection to the GPS is 9600 baud, which means about 1000 characters per second, but in void MySerialPort::readData():</p>
<pre><code>void MySerialPort::readData()
{
// you're transmitting 15 characters, which takes about 15 milliseconds
     sendData();

// but you're immediately trying to get an answer from the GPS
    QByteArray data = serial-&gt;readAll();
</code></pre>
<p dir="auto">try waiting 15 milliseconds for the 15 characters to be transmitted and then wait at least 15 milliseconds more the characters to arrive, before doing serial-&gt;readAll()</p>
<p dir="auto">Note: there are lots of helper functions for this in the QSerialPort class, for example you can use <strong>waitForBytesWritten()</strong> to make sure all of 15 characters have been transmitted before you're expecting an answer.</p>
]]></description><link>https://forum.qt.io/post/548009</link><guid isPermaLink="true">https://forum.qt.io/post/548009</guid><dc:creator><![CDATA[hskoglund]]></dc:creator><pubDate>Tue, 27 Aug 2019 00:21:38 GMT</pubDate></item></channel></rss>