serial port
-
I created two pages in qt c++ (main window.cpp , test.cpp) in first page using serial port connected and clicked test page serial port disconnected what is the reason and how to fix this issue
Test * page1; page1 = new Test(this); void MainWindow::on_next_page_clicked() { this->hide(); page1->show(); }
-
@JonB ```
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "test.h"
Test * page1;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
page1 = new Test(this);serial_init();
}
void MainWindow::serial_init()
{
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
Value = info.portName();
ui->comboBox->addItem(Value);
ui->comboBox->setCurrentIndex(0);
}
}void MainWindow::readData()
{QByteArray data = serial->readAll(); QString receivedData = QString::fromLocal8Bit(data); ui->plainTextEdit->insertPlainText(receivedData);
}
void MainWindow::on_connect_clicked()
{
if (isConnected)
{
serial->close();ui->radioButton->setStyleSheet("QRadioButton::indicator { background-color: RED; width: 10px; height: 10px; border-radius: 5px;}"); ui->radioButton->setText("DEVICE NOT CONNECTED !"); isConnected = false; ui->connect->setText("CONNECT"); } else { ui->radioButton->setStyleSheet("QRadioButton::indicator { background-color: green; width: 10px; height: 10px; border-radius: 5px;}"); ui->radioButton->setText("DEVICE CONNECTED"); serial = new QSerialPort(ui->comboBox->currentText()); serial->setBaudRate(QSerialPort::Baud115200); serial->setDataBits(QSerialPort::Data8); serial->setParity(QSerialPort::NoParity); serial->setStopBits(QSerialPort::OneStop); serial->setFlowControl(QSerialPort::NoFlowControl); if (serial->open(QIODevice::ReadWrite)) { ui->label->setText("DEVICE CONNECTED"); ui->connect->setText("DISCONNECT"); connect(serial, SIGNAL(readyRead()), this, SLOT(readData())); } }
}
void MainWindow::on_next_page_clicked()
{
this->hide();
page1->show();
} -
@Rockerz How do you know "serial port disconnected"? I don't see anything in the code you posted.
And please format your code properly using code tags, it is hard to read what you posted.One more thing: in void MainWindow::on_connect_clicked() you create a new QSerialPort everytime you do a new connection, you do not delte the old instance -> memory leak.
-
@jsulm When I run this code switching test page and return to main window page device disconnected message I saw and How to fix this issue and in this page. In mainwindow serial used test page serial 1 is used it work or not because I'm getting error when used serial.
#include "Test.h" #include "ui_Test.h" #include "mainwindow.h" QSerialPort * serial1; int check1 =0, values1; Test::Test(QWidget *parent) : QMainWindow(parent), ui(new Ui::Test) { ui->setupUi(this); ui->Test_linEdit->setValidator(new QIntValidator(this)); ui->Test_linEdit->setMaxLength(5); } Test::~Test() { delete ui; } void Test::on_Send_Ref_clicked() { QByteArray ref_burn; ref_burn = ui->Test_linEdit->text().toUtf8(); QByteArray bytes; bytes.resize(1); bytes[0] = '#'; bytes[1] = 'B'; bytes[2] = 'T'; bytes[3] = '='; bytes[4] = '0'; bytes[5] = QString::number(values1).toUtf8()[0]; bytes[6] = QString::number(values1).toUtf8()[1]; bytes[7] = QString::number(values1).toUtf8()[2]; bytes[8] = QString::number(values1).toUtf8()[3]; bytes[9] = '!'; serial1->write(bytes); qDebug()<<"Reference Test "<<bytes; } void Test::on_back_clicked() { this->hide(); MainWindow *page1 = new MainWindow(); page1->show(); } void Test::on_exit_clicked() { this->close(); }
-
@Rockerz said in serial port:
void Test::on_back_clicked()
{
this->hide();
MainWindow *page1 = new MainWindow();
page1->show();
}Why are you creating a new MainWindow here?!
-
-
@Rockerz
In a signal you create for this purpose you should pass as (a) parameter(s) whatever information you want to pass, e.g. a string you want displayed. A slot you write in your existing main window should receive that string and do whatever on the UI side, such as displaying it.You need to understand conceptually why creating a
new MainWindow()
will never be right and will never address your existing main window instance. -