<?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[QFile bytesAvailable() returns always 0]]></title><description><![CDATA[<p dir="auto">Hello everyone,<br />
I'm trying to write a simple sequential program controlling an embedded device (through a device file "/dev/usbtmc0" in Linux). I don't want to use signals and slots. The device doesn't return new line or carriage return. And here comes the problem ... My code is this (in a separate thread):</p>
<pre><code>void osc::run(){
    QString line;

    dev_file_stream.setDevice(&amp;dev_file);
    dev_file_stream &lt;&lt; "*IDN?\n";
    dev_file_stream.flush();
    qDebug() &lt;&lt; "waiting reply ...";
    dev_file.waitForReadyRead(-1);
    qDebug() &lt;&lt; "reply received!";
    quint64 bytes = dev_file.bytesAvailable();
    qDebug() &lt;&lt; "bytes: " &lt;&lt; bytes;
    line = dev_file_stream.read(bytes);
    //line = dev_file_stream.readLine();
    qDebug() &lt;&lt; "line: " &lt;&lt; line;
}
</code></pre>
<p dir="auto">If I use this code, bytesAvailable( ) returns always 0.<br />
If I use readLine( ), reading becomes terribly slow (5 or 6 seconds) probably because the reply doesn't have a '\n' or a '\r'.<br />
If I use readLine(10), reading becomes fast but I'm missing chars because the replies are of different length (20, 30, 40, etc).</p>
<p dir="auto">Any help is appreciated!</p>
<p dir="auto">Regards,<br />
L. B.</p>
<p dir="auto">p.s. I've been struggling with this problem for years, and have always been forced to write complex signal-slot programs.</p>
]]></description><link>https://forum.qt.io/topic/139387/qfile-bytesavailable-returns-always-0</link><generator>RSS for Node</generator><lastBuildDate>Thu, 05 Nov 2026 00:51:52 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/139387.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 17 Sep 2022 13:26:15 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QFile bytesAvailable() returns always 0 on Sun, 18 Sep 2022 19:08:46 GMT]]></title><description><![CDATA[<p dir="auto">Well, I tried with waitForReadyRead( ) but reading still is slow (5 seconds to get the reply):</p>
<pre><code>void osc::run(){
    QByteArray line;

    dev_file.write("*IDN?\n");
    dev_file.waitForReadyRead(-1);
    line = dev_file.readAll();

    qDebug() &lt;&lt; "line: " &lt;&lt; line;
}
</code></pre>
<p dir="auto">However, now I read in the Qt documentation:</p>
<p dir="auto">"Unlike other QIODevice implementations, such as QTcpSocket, QFile does not emit the aboutToClose(), bytesWritten(), or readyRead() signals. This implementation detail means that <strong>QFile is not suitable</strong> for reading and writing certain types of files, such as <strong>device files on Unix</strong> platforms"</p>
<p dir="auto">So, I guess this is not the way to do it ...</p>
]]></description><link>https://forum.qt.io/post/729213</link><guid isPermaLink="true">https://forum.qt.io/post/729213</guid><dc:creator><![CDATA[LubomirBogdanov]]></dc:creator><pubDate>Sun, 18 Sep 2022 19:08:46 GMT</pubDate></item><item><title><![CDATA[Reply to QFile bytesAvailable() returns always 0 on Sun, 18 Sep 2022 18:59:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/lubomirbogdanov">@<bdi>LubomirBogdanov</bdi></a><br />
<code>readAll()</code> returns whatever is available at that instant.  You can't put it immediately after a <code>write()</code>, the device will not have had time to respond.  Why have you removed your <code>dev_file.waitForReadyRead(-1)</code>?  Or do it better with <code>readyRead</code> signal?</p>
]]></description><link>https://forum.qt.io/post/729210</link><guid isPermaLink="true">https://forum.qt.io/post/729210</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sun, 18 Sep 2022 18:59:24 GMT</pubDate></item><item><title><![CDATA[Reply to QFile bytesAvailable() returns always 0 on Sun, 18 Sep 2022 18:42:51 GMT]]></title><description><![CDATA[<p dir="auto">Hi, sorry forgot to include the types:</p>
<pre><code>    QFile dev_file;
    QTextStream dev_file_stream;
</code></pre>
<p dir="auto">OK, so I tried as you said, but still the readAll( ) API returns after a few seconds ... Here is the new code:</p>
<pre><code>void osc::run(){
    QByteArray line;

    dev_file.write("*IDN?\n");
    line = dev_file.readAll();
    qDebug() &lt;&lt; "line: " &lt;&lt; line;
}
</code></pre>
<p dir="auto">Regards,<br />
L. B.</p>
]]></description><link>https://forum.qt.io/post/729207</link><guid isPermaLink="true">https://forum.qt.io/post/729207</guid><dc:creator><![CDATA[LubomirBogdanov]]></dc:creator><pubDate>Sun, 18 Sep 2022 18:42:51 GMT</pubDate></item><item><title><![CDATA[Reply to QFile bytesAvailable() returns always 0 on Sat, 17 Sep 2022 14:13:15 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/lubomirbogdanov">@<bdi>LubomirBogdanov</bdi></a><br />
Firstly, what Qt classes are your <code>dev_file_stream</code> and <code>dev_file</code>?</p>
<p dir="auto">Your situation may (well) be "messed up" by your use of <code>dev_file.bytesAvailable();</code> when you have it "wrapped" inside a <code>dev_file_stream</code>.  Assuming that is some kind of "buffer", it looks like the buffer has filled (from <code>readyRead()</code> signal?) with bytes it reads from <code>dev_file</code>.  That would mean <code>dev_file.bytesAvailable()</code> <em>now</em> returns <code>0</code> while <code> dev_file_stream.read()</code> has bytes in it available to read.  And using <code>readLine()</code> when your device does not send <code>\n</code> is not "useful".</p>
<p dir="auto">Start by just getting rid of your <code>dev_file_stream</code> wrapper level.  Use only the <code>dev_file</code>.  Depending on its type, use just <code>read</code> &amp; <code>write</code>, don't worry about <code>&lt;&lt;</code> convenience.  That ought work!?  Also try <code>dev_file.readAll()</code> without worrying about what <code>dev_file.bytesAvailable()</code> returns.  At least use this to verify what is going on.</p>
<p dir="auto">Of course, nothing will work "quickly" if your device buffers its output, or buffers it for some seconds before flushing.</p>
<p dir="auto">I will just say that the signals and slots are pretty easy to work with.  <code>waitForReadyRead()</code> is just some Qt code wrapping around the signals with a blocking loop.</p>
]]></description><link>https://forum.qt.io/post/729107</link><guid isPermaLink="true">https://forum.qt.io/post/729107</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sat, 17 Sep 2022 14:13:15 GMT</pubDate></item></channel></rss>