Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. serial port
Forum Updated to NodeBB v4.3 + New Features

serial port

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 741 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rockerz
    wrote on last edited by
    #1

    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();
    }
    
    JonBJ 1 Reply Last reply
    0
    • R Rockerz

      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();
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Rockerz
      Your code shows nothing about how you attempt to use serial port or share it across pages so nobody can answer. Hiding windows does not affect serial port.

      R 1 Reply Last reply
      0
      • JonBJ JonB

        @Rockerz
        Your code shows nothing about how you attempt to use serial port or share it across pages so nobody can answer. Hiding windows does not affect serial port.

        R Offline
        R Offline
        Rockerz
        wrote on last edited by
        #3

        @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();
        }

        jsulmJ 1 Reply Last reply
        0
        • R Rockerz

          @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();
          }

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          R 1 Reply Last reply
          2
          • jsulmJ jsulm

            @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.

            R Offline
            R Offline
            Rockerz
            wrote on last edited by Rockerz
            #5

            @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();
            }
            
            jsulmJ 1 Reply Last reply
            0
            • R Rockerz

              @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();
              }
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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?!

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              R 1 Reply Last reply
              1
              • jsulmJ jsulm

                @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?!

                R Offline
                R Offline
                Rockerz
                wrote on last edited by
                #7

                @jsulm how to write back to main window ? provide me a idea for back to main window

                jsulmJ 1 Reply Last reply
                0
                • R Rockerz

                  @jsulm how to write back to main window ? provide me a idea for back to main window

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Rockerz said in serial port:

                  how to write back to main window ?

                  Using signals/slots...

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  R 1 Reply Last reply
                  1
                  • jsulmJ jsulm

                    @Rockerz said in serial port:

                    how to write back to main window ?

                    Using signals/slots...

                    R Offline
                    R Offline
                    Rockerz
                    wrote on last edited by
                    #9

                    @jsulm I'm not still get your point what should I give in signals?

                    JonBJ 1 Reply Last reply
                    0
                    • R Rockerz

                      @jsulm I'm not still get your point what should I give in signals?

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @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.

                      1 Reply Last reply
                      0
                      • R Rockerz has marked this topic as solved on

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved