<?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[Qt Open Serial Port But Not Write]]></title><description><![CDATA[<p dir="auto">I'm trying  to open a serial communication with my arduino mega. I open the port and make all the necessary config, but when i write some data, arduino doesnt receive nothing. I cant figure out what am I doing wrong.</p>
<p dir="auto">Communication.h</p>
<pre><code>#ifndef QSERIALCOM_H
#define QSERIALCOM_H

#include &lt;QSerialPort&gt;
#include &lt;QSerialPortInfo&gt;
#include &lt;QMutex&gt;

#include &lt;string&gt;

class QSerialCom : public QObject {
    Q_OBJECT

public:
    explicit QSerialCom(QObject* parent = nullptr);
    ~QSerialCom();

    static void printPortInfo();

    void updateThreads();

private:
    QList&lt;QSerialPortInfo&gt; ports;
    QSerialPort opened;

    QMutex openedmut;

    std::array&lt;int, 6&gt; transmission;

    bool automaticPortSelection;



public slots:
    void recieveUpdate();
    void recieveNewPort(QSerialPortInfo);
    void recieveSetAutomaticPortsSelection(bool);

signals:
    void sendDevicesChanged(QList&lt;QSerialPortInfo&gt; devs);
    void sendDoneUpdating();
};

namespace codes {
    static const char COMMSTART = 0;
    static const char COMMEND = 1;
    static const char ACK = 2;
    static const char ERR = 3;
}


Q_DECLARE_METATYPE(QSerialPortInfo)

#endif // QSERIALCOM_H

</code></pre>
<p dir="auto">Communication.cpp:</p>
<pre><code>#include "QSerialCom.h"

#include &lt;QDebug&gt;

#include "Control.h"

// Comparador de QSerialPortInfos
bool operator== (const QSerialPortInfo p1, const QSerialPortInfo p2) {
    return p1.portName() == p2.portName() &amp;&amp;
           p1.systemLocation() == p2.systemLocation() &amp;&amp;
           p1.manufacturer() == p2.manufacturer() &amp;&amp;
           p1.serialNumber() == p2.serialNumber();
}

void QSerialCom::printPortInfo() {
    for(const auto &amp;port : QSerialPortInfo::availablePorts()) {
        qDebug() &lt;&lt; "Nome: " &lt;&lt; port.portName();
        qDebug() &lt;&lt; "Localização: " &lt;&lt; port.systemLocation();
        qDebug() &lt;&lt; "Descrição: " &lt;&lt; port.description();
        qDebug() &lt;&lt; "Ocupado: " &lt;&lt; port.isBusy();
        qDebug() &lt;&lt; "Produtor: " &lt;&lt; port.manufacturer();
        qDebug() &lt;&lt; "Id: " &lt;&lt; port.productIdentifier();
        qDebug() &lt;&lt; "Número Serial: " &lt;&lt; port.serialNumber();
        qDebug() &lt;&lt; "Id do vendedor: " &lt;&lt; port.vendorIdentifier();
    }
}

QSerialCom::QSerialCom(QObject* parent) : QObject(parent),
    ports(),
    opened(),
    openedmut(),
    transmission(),
    automaticPortSelection(true)
{}

QSerialCom::~QSerialCom() {}

void QSerialCom::updateThreads() {
    opened.moveToThread(thread());
}

void QSerialCom::recieveNewPort(QSerialPortInfo newport) {
    QMutexLocker lock(&amp;openedmut);
    opened.setPort(newport);
    if(!opened.isOpen())
        opened.open(QIODevice::ReadWrite);
}

#include "MacroLogger.h"
void QSerialCom::recieveUpdate() {
    Control::calculateAngles();

    auto newports = QSerialPortInfo::availablePorts();

    if(ports != newports) {
        emit sendDevicesChanged(newports);
        ports = newports;
        if(ports.size() != 0 &amp;&amp; !opened.isOpen() &amp;&amp; automaticPortSelection) {
            recieveNewPort(ports[0]);
        }
        opened.setBaudRate(QSerialPort::Baud9600);
        opened.setDataBits(QSerialPort::Data8);
        opened.setFlowControl(QSerialPort::NoFlowControl);
        opened.setParity(QSerialPort::NoParity);
        opened.setStopBits(QSerialPort::OneStop);
    }

    if(opened.isOpen()) {
        char buf[1] = {codes::COMMSTART};
        printf("\n SENDING COMMSTART TO ARDUINO...");
        opened.write("A");
        printf("\n WRITING BYTES...");
        opened.waitForBytesWritten(-1);
        printf("\n COMMSTART SENT \n");

        \\THE PROGRAM BLOCKS HERE
        printf("\nWAITING MSG FROM ARDUINO..."); 
        opened.waitForReadyRead(-1);
        printf("\nRECEIVED");
        QLOG(opened.bytesAvailable());
        if(opened.bytesAvailable()) {
            opened.getChar(buf);
            if(buf[0] == codes::COMMSTART)
                STDLOG("primeira mensagem recebida: codes::COMMSTART");
            else if(buf[0] == codes::ACK)
                STDLOG("primeira mensagem recebida: codes::ACK");
            else if(buf[0] == codes::ERR)
                STDLOG("primeira mensagem recebida: codes::ERR");
            else
                printf("primeira mensagem recebida: %d", buf[0]);

            if(buf[0] != codes::COMMSTART) {
                printf("\n COMMSTART ARDUINO NOT RECEIVED \n");
                emit sendDoneUpdating();
                return;
            }
            printf("\n COMMSTART FROM ARDUINO RECEIVED \n");

            for(const auto&amp; sent : transmission) {
                opened.waitForBytesWritten();
                opened.write(reinterpret_cast&lt;const char*&gt;(&amp;sent), 4);

                opened.waitForReadyRead();
                opened.getChar(buf);
                if(buf[0] != codes::ACK) {
                    emit sendDoneUpdating();
                    return;
                }
            }

            opened.putChar(codes::COMMEND);
            opened.waitForBytesWritten();
            opened.waitForReadyRead();
            opened.getChar(buf);
            if(buf[0] != codes::COMMEND) STDLOG("deu merdinha");
            else STDLOG("deu certinho");
        }
    }

    else {
        printf("\n NOT OPENED \n");
    }

    emit sendDoneUpdating();
}

void QSerialCom::recieveSetAutomaticPortsSelection(bool newstate) {
    automaticPortSelection = newstate;
}
</code></pre>
<p dir="auto">Arduino code:</p>
<pre><code>void setup() {
  Serial.begin(9600);
  //Serial.setTimeout(-1);
  pinMode(13, OUTPUT);
  Serial.flush();
}

void loop() {
  if (Serial.available()) {
    Serial.println("E");
  }
}
</code></pre>
<p dir="auto">The Arduino Serial never become available and the letter "E" is never send</p>
]]></description><link>https://forum.qt.io/topic/107349/qt-open-serial-port-but-not-write</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Apr 2026 07:00:02 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/107349.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 01 Oct 2019 15:17:35 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Qt Open Serial Port But Not Write on Tue, 01 Oct 2019 18:28:58 GMT]]></title><description><![CDATA[<p dir="auto">Hi and welcome to devnet,</p>
<p dir="auto">Can you communicate with the Arduino with e.g. the terminal example ?</p>
<p dir="auto">Are you sure your settings are correct on both sides ?</p>
<p dir="auto">Are you sure you are using the correct com port ?</p>
]]></description><link>https://forum.qt.io/post/553850</link><guid isPermaLink="true">https://forum.qt.io/post/553850</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Tue, 01 Oct 2019 18:28:58 GMT</pubDate></item></channel></rss>