<?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[problem with writing on serial port]]></title><description><![CDATA[[[topic:post-is-deleted]]]]></description><link>https://forum.qt.io/topic/123057/problem-with-writing-on-serial-port</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Apr 2026 18:17:59 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/123057.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 25 Jan 2021 06:50:48 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to problem with writing on serial port on Mon, 25 Jan 2021 13:09:21 GMT]]></title><description><![CDATA[<p dir="auto">@J-Hilk It is specified to use while(1) loop</p>
]]></description><link>https://forum.qt.io/post/640093</link><guid isPermaLink="true">https://forum.qt.io/post/640093</guid><dc:creator><![CDATA[nanor]]></dc:creator><pubDate>Mon, 25 Jan 2021 13:09:21 GMT</pubDate></item><item><title><![CDATA[Reply to problem with writing on serial port on Mon, 25 Jan 2021 13:08:11 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nanor">@<bdi>nanor</bdi></a> said in <a href="/post/640086">problem with writing on serial port</a>:</p>
<blockquote>
<p dir="auto">I want to constantly read from and write to a port (</p>
</blockquote>
<p dir="auto">is it specified how you have to <code>constantly read from and write to a port</code> or is that implementation detail up to you?</p>
<p dir="auto">because while- loops and threads I the least optimal way to do it</p>
]]></description><link>https://forum.qt.io/post/640092</link><guid isPermaLink="true">https://forum.qt.io/post/640092</guid><dc:creator><![CDATA[J.Hilk]]></dc:creator><pubDate>Mon, 25 Jan 2021 13:08:11 GMT</pubDate></item><item><title><![CDATA[Reply to problem with writing on serial port on Mon, 25 Jan 2021 13:04:14 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> This is a required exersize</p>
]]></description><link>https://forum.qt.io/post/640091</link><guid isPermaLink="true">https://forum.qt.io/post/640091</guid><dc:creator><![CDATA[nanor]]></dc:creator><pubDate>Mon, 25 Jan 2021 13:04:14 GMT</pubDate></item><item><title><![CDATA[Reply to problem with writing on serial port on Mon, 25 Jan 2021 13:05:06 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nanor">@<bdi>nanor</bdi></a> said in <a href="/post/640086">problem with writing on serial port</a>:</p>
<blockquote>
<p dir="auto">Now I know that I have to declare a class inheritted from qThread class and inside its run function,</p>
</blockquote>
<p dir="auto">Why?  <em>Is this a required exercise to use a thread</em>, or are you just doing it because you <em>think</em> you need to??</p>
<p dir="auto">Oh, now I see</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> I was asked to do this. I was asked to send a text in while(1) loop.</p>
</blockquote>
<p dir="auto"><strong>EDIT</strong> OK, I have seen now you have answered that you must do it this way.  Seems terrible to me!  But OK :)</p>
]]></description><link>https://forum.qt.io/post/640089</link><guid isPermaLink="true">https://forum.qt.io/post/640089</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 25 Jan 2021 13:05:06 GMT</pubDate></item><item><title><![CDATA[Reply to problem with writing on serial port on Mon, 25 Jan 2021 12:57:50 GMT]]></title><description><![CDATA[<p dir="auto">@J-Hilk Hi<br />
I have a button that when a user pushes it, "salam" is printed. At the same time, I want to constantly read from and write to a port (I want to read byte by byte and print them(debug them)). Now I know that I have to declare a class inheritted from qThread class and inside its run function, read and write. But I don't know if I have to declare while loop inside the run function in order to read constantly or not. In the code I posted, I have:</p>
<pre><code>
        QByteArray data = serial-&gt;read(1);
        for(int i=0; i&lt;data.size(); i++)
        {
                qDebug() &lt;&lt; data[i];
        }
</code></pre>
<p dir="auto">Is this part reading the datas byte by byte and store them inside an array and print the datas?</p>
]]></description><link>https://forum.qt.io/post/640086</link><guid isPermaLink="true">https://forum.qt.io/post/640086</guid><dc:creator><![CDATA[nanor]]></dc:creator><pubDate>Mon, 25 Jan 2021 12:57:50 GMT</pubDate></item><item><title><![CDATA[Reply to problem with writing on serial port on Mon, 25 Jan 2021 12:50:34 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> said in <a href="/post/640079">problem with writing on serial port</a>:</p>
<blockquote>
<p dir="auto">I asked already why threads. Seems to be an exercise.</p>
</blockquote>
<p dir="auto">possibly, but the OP only states the while loop as a task. Not threads specifically.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nanor">@<bdi>nanor</bdi></a>  can you word exactly, what you want to do ?</p>
]]></description><link>https://forum.qt.io/post/640084</link><guid isPermaLink="true">https://forum.qt.io/post/640084</guid><dc:creator><![CDATA[J.Hilk]]></dc:creator><pubDate>Mon, 25 Jan 2021 12:50:34 GMT</pubDate></item><item><title><![CDATA[Reply to problem with writing on serial port on Mon, 25 Jan 2021 12:40:45 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> I asked already why threads. Seems to be an exercise.</p>
]]></description><link>https://forum.qt.io/post/640079</link><guid isPermaLink="true">https://forum.qt.io/post/640079</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Mon, 25 Jan 2021 12:40:45 GMT</pubDate></item><item><title><![CDATA[Reply to problem with writing on serial port on Mon, 25 Jan 2021 12:39:32 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nanor">@<bdi>nanor</bdi></a> I gave you a link to an example which uses the synchronous API. Apparently you did not really read it carefully, did you? In your loop you should use waitForReadyRead(...) as is done in the example. This makes sure that the event loop can actually do its job.</p>
<pre><code>if (serial.waitForReadyRead(currentWaitTimeout)) {
    QByteArray responseData = serial.readAll();
</code></pre>
]]></description><link>https://forum.qt.io/post/640078</link><guid isPermaLink="true">https://forum.qt.io/post/640078</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Mon, 25 Jan 2021 12:39:32 GMT</pubDate></item><item><title><![CDATA[Reply to problem with writing on serial port on Mon, 25 Jan 2021 12:17:31 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nanor">@<bdi>nanor</bdi></a><br />
My own view, often times expressed, is why are you going anywhere near <code>QThread</code> at all?  Every time it causes posters problems.  Qt framework is asynchronous, so why do you need any threads when you have signals?</p>
]]></description><link>https://forum.qt.io/post/640065</link><guid isPermaLink="true">https://forum.qt.io/post/640065</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 25 Jan 2021 12:17:31 GMT</pubDate></item><item><title><![CDATA[Reply to problem with writing on serial port on Mon, 25 Jan 2021 12:14:36 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> Thank you for mentioning using debug. I added the line  qDebug() &lt;&lt; "inside the run function"; inside the run function, but after running, this message isn't shown. But I have started the thread inside the mainwindow class. I can't understand why this is happened!</p>
]]></description><link>https://forum.qt.io/post/640063</link><guid isPermaLink="true">https://forum.qt.io/post/640063</guid><dc:creator><![CDATA[nanor]]></dc:creator><pubDate>Mon, 25 Jan 2021 12:14:36 GMT</pubDate></item><item><title><![CDATA[Reply to problem with writing on serial port on Mon, 25 Jan 2021 12:09:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nanor">@<bdi>nanor</bdi></a><br />
Start by putting in <code>qDebug()</code> statements to see for yourself where you are actually getting to, plus more error checking.</p>
<p dir="auto">I don't know if any serial data is available to read at the instant you open the port.  And you only try to read one byte once.</p>
]]></description><link>https://forum.qt.io/post/640059</link><guid isPermaLink="true">https://forum.qt.io/post/640059</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 25 Jan 2021 12:09:10 GMT</pubDate></item><item><title><![CDATA[Reply to problem with writing on serial port on Mon, 25 Jan 2021 12:01:02 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> I removed the while loop and mythread.cpp is now:</p>
<pre><code>#include "mythread.h"

MyThread::MyThread(QObject *parent) : QThread(parent)
{

}

void MyThread::run()
{
    serial = new QSerialPort();
    serial-&gt;setPortName("COM1");

    for(const auto &amp;serialPortInfo : QSerialPortInfo::availablePorts())
    {
       qDebug() &lt;&lt; "find serial port: " &lt;&lt; serialPortInfo.portName() ;
    }

    serial-&gt;open(QIODevice::ReadWrite);


    if(serial-&gt;isOpen())
    {
        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);



        QByteArray data = serial-&gt;read(1);
        for(int i=0; i&lt;data.size(); i++)
         {
                qDebug() &lt;&lt; data[i];
         }




    }

    else
    {
      qDebug() &lt;&lt; "can't open the port";
    }

}

</code></pre>
<p dir="auto">But still I can't get the messages. I send data from hercules.</p>
]]></description><link>https://forum.qt.io/post/640056</link><guid isPermaLink="true">https://forum.qt.io/post/640056</guid><dc:creator><![CDATA[nanor]]></dc:creator><pubDate>Mon, 25 Jan 2021 12:01:02 GMT</pubDate></item><item><title><![CDATA[Reply to problem with writing on serial port on Mon, 25 Jan 2021 11:58:41 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nanor">@<bdi>nanor</bdi></a><br />
Absolutely not!  Since it's <code>while(1)</code> how is it <em>ever</em> going to exit the loop??  <code>while (1)</code> is "always" wrong in Qt....</p>
<p dir="auto">You have now changed your code...</p>
<blockquote>
<p dir="auto">but nothing is printed</p>
</blockquote>
<p dir="auto">So either it never enters the loop, or it never has anything to read.</p>
]]></description><link>https://forum.qt.io/post/640055</link><guid isPermaLink="true">https://forum.qt.io/post/640055</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 25 Jan 2021 11:58:41 GMT</pubDate></item><item><title><![CDATA[Reply to problem with writing on serial port on Mon, 25 Jan 2021 11:56:53 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> Sorry again. I have another question. inside the while loop, I have to constantly read data from the port and store them inside an array adn then debug them . I have written the following line. but nothing is printed</p>
<pre><code>while(1)
        {
            QByteArray data = serial-&gt;read(1);
            for(int i=0; i&lt;data.size(); i++)
            {
                qDebug() &lt;&lt; data[i];
            }


        }
</code></pre>
]]></description><link>https://forum.qt.io/post/640048</link><guid isPermaLink="true">https://forum.qt.io/post/640048</guid><dc:creator><![CDATA[nanor]]></dc:creator><pubDate>Mon, 25 Jan 2021 11:56:53 GMT</pubDate></item><item><title><![CDATA[Reply to problem with writing on serial port on Mon, 25 Jan 2021 11:23:27 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> inside the mainwindow.cpp I didn't initialize the thread (myThread = new QThread();) . I added this and it got fixed.</p>
]]></description><link>https://forum.qt.io/post/640043</link><guid isPermaLink="true">https://forum.qt.io/post/640043</guid><dc:creator><![CDATA[nanor]]></dc:creator><pubDate>Mon, 25 Jan 2021 11:23:27 GMT</pubDate></item><item><title><![CDATA[Reply to problem with writing on serial port on Mon, 25 Jan 2021 11:14:56 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nanor">@<bdi>nanor</bdi></a> Please post what is in level 2 and 3, else I'm out of this thread as I do not want to guess what is wrong...<br />
It is your job to find out where your app is crashing.</p>
]]></description><link>https://forum.qt.io/post/640040</link><guid isPermaLink="true">https://forum.qt.io/post/640040</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Mon, 25 Jan 2021 11:14:56 GMT</pubDate></item><item><title><![CDATA[Reply to problem with writing on serial port on Mon, 25 Jan 2021 10:58:28 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nanor">@<bdi>nanor</bdi></a> Please post the stack trace (as text). It's in the middle of your screen-shot, in "Debugger" section.</p>
]]></description><link>https://forum.qt.io/post/640036</link><guid isPermaLink="true">https://forum.qt.io/post/640036</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Mon, 25 Jan 2021 10:58:28 GMT</pubDate></item><item><title><![CDATA[Reply to problem with writing on serial port on Mon, 25 Jan 2021 10:44:07 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nanor">@<bdi>nanor</bdi></a> Please build in debug mode before running in debugger!</p>
]]></description><link>https://forum.qt.io/post/640031</link><guid isPermaLink="true">https://forum.qt.io/post/640031</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Mon, 25 Jan 2021 10:44:07 GMT</pubDate></item><item><title><![CDATA[Reply to problem with writing on serial port on Mon, 25 Jan 2021 10:39:56 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nanor">@<bdi>nanor</bdi></a> said in <a href="/post/640026">problem with writing on serial port</a>:</p>
<blockquote>
<p dir="auto">serial = new QSerialPort(this);</p>
</blockquote>
<p dir="auto">Remove "this".<br />
If it is still crashing then please use the debugger to see where exactly and post the stack trace after crash.</p>
]]></description><link>https://forum.qt.io/post/640028</link><guid isPermaLink="true">https://forum.qt.io/post/640028</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Mon, 25 Jan 2021 10:39:56 GMT</pubDate></item><item><title><![CDATA[Reply to problem with writing on serial port on Mon, 25 Jan 2021 10:36:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> Thank you so much.<br />
For the firt step (I want to continuesly read data byte by byte) I have written the following code. But it crashes. Could you please guide me how to fix this?</p>
<p dir="auto">mainwindow.h:</p>
<pre><code>#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include &lt;QMainWindow&gt;
#include &lt;QtSerialPort/QSerialPort&gt;
#include "mythread.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    QSerialPort *serial;

    QThread *myThread;


private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
</code></pre>
<p dir="auto">mythread.h:</p>
<pre><code>#ifndef MYTHREAD_H
#define MYTHREAD_H

#include &lt;QObject&gt;
#include &lt;QThread&gt;
#include &lt;QtSerialPort/QSerialPort&gt;
#include &lt;QtSerialPort/QSerialPortInfo&gt;
#include &lt;QDebug&gt;

class MyThread : public QThread
{
    Q_OBJECT
public:
    explicit MyThread(QObject *parent = nullptr);

    void run();

    QSerialPort *serial;

signals:

public slots:
};

#endif // MYTHREAD_H

</code></pre>
<p dir="auto">mainwindow.cpp:</p>
<pre><code>#include "mainwindow.h"
#include "ui_mainwindow.h"
#include &lt;QtSerialPort/QSerialPort&gt;
#include &lt;QtSerialPort/QSerialPortInfo&gt;
#include &lt;QDebug&gt;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui-&gt;setupUi(this);
    myThread-&gt;start();

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    qDebug() &lt;&lt; "salam";
}
</code></pre>
<p dir="auto">mythread.cpp:</p>
<pre><code>#include "mythread.h"

MyThread::MyThread(QObject *parent) : QThread(parent)
{

}

void MyThread::run()
{
    serial = new QSerialPort(this);
    serial-&gt;setPortName("COM1");

    for(const auto &amp;serialPortInfo : QSerialPortInfo::availablePorts())
    {
       qDebug() &lt;&lt; "find serial port: " &lt;&lt; serialPortInfo.portName() ;
    }

    serial-&gt;open(QIODevice::ReadWrite);


    if(serial-&gt;isOpen())
    {
        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);

         while(1)
        {
            serial-&gt;read(1);

       }

    }

    else
    {
      qDebug() &lt;&lt; "can't open the port";
    }

}

</code></pre>
<p dir="auto">the error:</p>
<p dir="auto">10:33:50: Starting C:\Users\nanor\OneDrive\Desktop\qt codes\build-serialportreading3-Desktop_Qt_5_12_2_MinGW_32_bit-Debug\debug\serialportreading3.exe...<br />
ASSERT: "timeout &gt;= 0" in file thread\qmutex.cpp, line 582<br />
10:33:52: The program has unexpectedly finished.<br />
10:33:52: The process was ended forcefully.<br />
10:33:52: C:/Users/nanor/OneDrive/Desktop/qt codes/build-serialportreading3-Desktop_Qt_5_12_2_MinGW_32_bit-Debug/debug/serialportreading3.exe crashed.</p>
]]></description><link>https://forum.qt.io/post/640026</link><guid isPermaLink="true">https://forum.qt.io/post/640026</guid><dc:creator><![CDATA[nanor]]></dc:creator><pubDate>Mon, 25 Jan 2021 10:36:13 GMT</pubDate></item><item><title><![CDATA[Reply to problem with writing on serial port on Mon, 25 Jan 2021 09:58:11 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nanor">@<bdi>nanor</bdi></a> This example does exactly what you need: <a href="https://doc.qt.io/qt-5/qtserialport-blockingmaster-example.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/qtserialport-blockingmaster-example.html</a><br />
It uses blocking QSerialPort API in a worker thread.<br />
Take a look.</p>
]]></description><link>https://forum.qt.io/post/640010</link><guid isPermaLink="true">https://forum.qt.io/post/640010</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Mon, 25 Jan 2021 09:58:11 GMT</pubDate></item></channel></rss>