qtserial-portsignals-slotsqt6.4.1

QT 6.4.1 Use a SERIAL PORT in a subwindow


I wrote a small program to connect via Serial Port, send strings via LineEdit and receive the response in a TextBrowser. (for testing I use an Arduino which sends back all what it receives as an answer).

I wrote the connection in a class that I named "Serialport". As long as I run it as MainWindow and include and call the class "Serialport" everything works without problems.

But since I want to use it as a sub-window in my program, I want to open and use it in a separate window.

I call the class like this:

// subwindow.cpp
.
.
.

    serialport = new SerialPort(parent);

    connect(ui->pushButtonConnect, SIGNAL(clicked(bool)), this, SLOT(verbinden()));
    connect(ui->pushButtonDisconnect, SIGNAL(clicked(bool)), this, SLOT(trennen()));
    connect(ui->pushButtonSend, SIGNAL(clicked(bool)), this, SLOT(senLinEdit()));
    connect(serialport, SIGNAL(dataEmpfangen(QByteArray)), this, SLOT(beiDatenEmpfangen(QByteArray)));

.
.
.

void subWindow::beiDatenEmpfangen(const QByteArray &empfData) 
{
    zwischengespeicherterText += QString::fromLocal8Bit(empfData); //zwischengespeicherterText IS a QString
    qDebug() << zwischengespeicherterText + "\n";

    if(zwischengespeicherterText.endsWith("\r\n"))  //\r\n is my end signal from the Arduion
    {
        zwischengespeicherterText.replace("\r\n", "\n");
        ui->textBrowser->append(zwischengespeicherterText);

        zwischengespeicherterText = "";
    }

}

.
.
.

// mainwindow.cpp

    subWindow sn_vb(this); //
    sn_vb.setWindowTitle("Teste die Serielle Verbindung"); 
    sn_vb.exec();

Now only sending to the SerialPort works I don't receive anything anymore, There must be an error somewhere but I don't understand exactly where it could be.... I can post the code if you wish.

I am a complete newbie and have learned everything I know about videos so far please don't be too angry if I made basic mistakes.

I tryed: I ask ChatGPT hundrets of times, look YT Videos, googled it, ask a Friend who is good in C and C++

I hope that someone can explain to me where my error lies.


Solution

  • I found out how it works,

    in my serialport class i add a function called "verbindungCheck" and it connect it again in the right way:

        bool SerialPort::verbindungCheck()
    {
        if(m_serial->open(QIODevice::ReadWrite)) {
            // its connected 
            // the following line was missing 
            connect(m_serial, &QSerialPort::readyRead, this, &SerialPort::liesDaten);
            return true;
        } else {
            // something went wrong
            QMessageBox::warning(nullptr, "Error", "Couldn't connect ERROR Line: " + m_serial->errorString());
            return false;
        }
    }
    

    and this in the Subwindow.cpp

    if(serialport->verbindungCheck()) {
        // Connected
        QMessageBox box;
        box.setText("Connectet sucessfully with: " + ui->comboBox->currentText());
        box.setIcon(QMessageBox::Information);
        box.addButton("OK", QMessageBox::AcceptRole);
        box.exec();
    } else {
        // something went wrong
        QMessageBox box;
        box.setText("Connection faild");
        box.setIcon(QMessageBox::Warning);
        box.addButton("OK", QMessageBox::AcceptRole);
        box.exec();
    }
    

    Sorry for my bad english I am verry tiered and just want to go to bed.