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. Find the current port and open it
Forum Updated to NodeBB v4.3 + New Features

Find the current port and open it

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 3 Posters 1.7k 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.
  • D Offline
    D Offline
    deleted286
    wrote on 2 Mar 2021, 11:02 last edited by
    #1

    I want to add all current ports into o combo box, and open the selected one.
    in open_serial(), currentport name returns empty always. How can i do it?

    ```
      ui->ports->clear();
      foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
          list << info.portName();
          ui->ports->addItem(info.portName());
        }
      currentPortName = ui->ports->currentText();
      }
    
      MainWindow::~MainWindow()
     {
    delete ui;
    if (serial && serial->isOpen())
    {
        serial->close();
        delete serial;
    }
    delete dizi_mtx;
     }
    
    void MainWindow::open_Serial()
      {
    serial->setPortName(ui->ports->currentText());
    //serial->setPortName("/dev/ttyUSB0");
    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))
    {
        qDebug() << currentPortName << "is Open ";
        serial->clear();
        write_Json();
        connect(serial, &QSerialPort::readyRead, this, &MainWindow::on_Serial_Read);
    }
    
    if(serial->isOpen())
    {
        ui->Open->setChecked(true);
    }
    else
    {
        ui->Close->setChecked(true);
     }
    

    }

    J 1 Reply Last reply 2 Mar 2021, 11:05
    0
    • D deleted286
      2 Mar 2021, 11:02

      I want to add all current ports into o combo box, and open the selected one.
      in open_serial(), currentport name returns empty always. How can i do it?

      ```
        ui->ports->clear();
        foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
            list << info.portName();
            ui->ports->addItem(info.portName());
          }
        currentPortName = ui->ports->currentText();
        }
      
        MainWindow::~MainWindow()
       {
      delete ui;
      if (serial && serial->isOpen())
      {
          serial->close();
          delete serial;
      }
      delete dizi_mtx;
       }
      
      void MainWindow::open_Serial()
        {
      serial->setPortName(ui->ports->currentText());
      //serial->setPortName("/dev/ttyUSB0");
      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))
      {
          qDebug() << currentPortName << "is Open ";
          serial->clear();
          write_Json();
          connect(serial, &QSerialPort::readyRead, this, &MainWindow::on_Serial_Read);
      }
      
      if(serial->isOpen())
      {
          ui->Open->setChecked(true);
      }
      else
      {
          ui->Close->setChecked(true);
       }
      

      }

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 2 Mar 2021, 11:05 last edited by
      #2

      @suslucoder said in Find the current port and open it:

      currentport name returns empty always

      So, is anything selected?

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

      D 1 Reply Last reply 2 Mar 2021, 11:05
      0
      • J jsulm
        2 Mar 2021, 11:05

        @suslucoder said in Find the current port and open it:

        currentport name returns empty always

        So, is anything selected?

        D Offline
        D Offline
        deleted286
        wrote on 2 Mar 2021, 11:05 last edited by
        #3

        @jsulm but it was selected

        J 1 Reply Last reply 2 Mar 2021, 11:18
        0
        • D deleted286
          2 Mar 2021, 11:05

          @jsulm but it was selected

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 2 Mar 2021, 11:18 last edited by
          #4

          @suslucoder I mean: is a port selected in combo box (or what ever widget ui->ports is)?

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

          D 1 Reply Last reply 2 Mar 2021, 11:23
          0
          • J jsulm
            2 Mar 2021, 11:18

            @suslucoder I mean: is a port selected in combo box (or what ever widget ui->ports is)?

            D Offline
            D Offline
            deleted286
            wrote on 2 Mar 2021, 11:23 last edited by
            #5

            @jsulm yes it was selected

            J 1 Reply Last reply 2 Mar 2021, 11:25
            0
            • D deleted286
              2 Mar 2021, 11:23

              @jsulm yes it was selected

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 2 Mar 2021, 11:25 last edited by
              #6

              @suslucoder How do you know that ui->ports->currentText() returns an empty string?

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

              D 1 Reply Last reply 2 Mar 2021, 11:31
              0
              • J jsulm
                2 Mar 2021, 11:25

                @suslucoder How do you know that ui->ports->currentText() returns an empty string?

                D Offline
                D Offline
                deleted286
                wrote on 2 Mar 2021, 11:31 last edited by
                #7

                @jsulm from here

                if (serial->open(QIODevice::ReadWrite))
                {
                    qDebug() << currentPortName << "is Open ";
                    serial->clear();
                    write_Json();
                    connect(serial, &QSerialPort::readyRead, this, &MainWindow::on_Serial_Read);
                }
                
                C J 2 Replies Last reply 2 Mar 2021, 11:43
                0
                • D deleted286
                  2 Mar 2021, 11:31

                  @jsulm from here

                  if (serial->open(QIODevice::ReadWrite))
                  {
                      qDebug() << currentPortName << "is Open ";
                      serial->clear();
                      write_Json();
                      connect(serial, &QSerialPort::readyRead, this, &MainWindow::on_Serial_Read);
                  }
                  
                  C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 2 Mar 2021, 11:43 last edited by
                  #8

                  @suslucoder You don't use ui->ports->currentText() anywhere in your code you show us here ...

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  D 1 Reply Last reply 2 Mar 2021, 11:56
                  0
                  • D deleted286
                    2 Mar 2021, 11:31

                    @jsulm from here

                    if (serial->open(QIODevice::ReadWrite))
                    {
                        qDebug() << currentPortName << "is Open ";
                        serial->clear();
                        write_Json();
                        connect(serial, &QSerialPort::readyRead, this, &MainWindow::on_Serial_Read);
                    }
                    
                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 2 Mar 2021, 11:45 last edited by
                    #9

                    @suslucoder said in Find the current port and open it:

                    currentPortName

                    If you mean this one, then this is wrong as you only set it once when nothing is selected! Use ui->ports->currentText():

                    qDebug() << ui->ports->currentText() << "is Open ";
                    

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

                    D 1 Reply Last reply 2 Mar 2021, 11:57
                    0
                    • C Christian Ehrlicher
                      2 Mar 2021, 11:43

                      @suslucoder You don't use ui->ports->currentText() anywhere in your code you show us here ...

                      D Offline
                      D Offline
                      deleted286
                      wrote on 2 Mar 2021, 11:56 last edited by
                      #10

                      @Christian-Ehrlicher im using it to open the port?

                      serial->setPortName(ui->ports->currentText());
                      
                      1 Reply Last reply
                      0
                      • J jsulm
                        2 Mar 2021, 11:45

                        @suslucoder said in Find the current port and open it:

                        currentPortName

                        If you mean this one, then this is wrong as you only set it once when nothing is selected! Use ui->ports->currentText():

                        qDebug() << ui->ports->currentText() << "is Open ";
                        
                        D Offline
                        D Offline
                        deleted286
                        wrote on 2 Mar 2021, 11:57 last edited by
                        #11

                        @jsulm it returns empty again

                        J 1 Reply Last reply 2 Mar 2021, 12:04
                        0
                        • D deleted286
                          2 Mar 2021, 11:57

                          @jsulm it returns empty again

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 2 Mar 2021, 12:04 last edited by
                          #12

                          @suslucoder said in Find the current port and open it:

                          it returns empty again

                          How did you verify that?
                          Please add

                          qDebug() << ui->ports->currentText();
                          

                          and see what it prints out.

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

                          D 1 Reply Last reply 2 Mar 2021, 12:15
                          0
                          • J jsulm
                            2 Mar 2021, 12:04

                            @suslucoder said in Find the current port and open it:

                            it returns empty again

                            How did you verify that?
                            Please add

                            qDebug() << ui->ports->currentText();
                            

                            and see what it prints out.

                            D Offline
                            D Offline
                            deleted286
                            wrote on 2 Mar 2021, 12:15 last edited by
                            #13

                            @jsulm said in Find the current port and open it:

                            qDebug() << ui->ports->currentText();

                            I've add it. It returns empty string.
                            Like ""
                            Why you didnt believe me?

                            J 1 Reply Last reply 2 Mar 2021, 12:25
                            0
                            • D deleted286
                              2 Mar 2021, 12:15

                              @jsulm said in Find the current port and open it:

                              qDebug() << ui->ports->currentText();

                              I've add it. It returns empty string.
                              Like ""
                              Why you didnt believe me?

                              J Offline
                              J Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 2 Mar 2021, 12:25 last edited by
                              #14

                              @suslucoder said in Find the current port and open it:

                              Why you didnt believe me?

                              Because it was not clear how you actually verified that it is indeed empty. Often people simply assume something, that's why it's worth asking.

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

                              1 Reply Last reply
                              0
                              • C Offline
                                C Offline
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on 2 Mar 2021, 12:27 last edited by
                                #15

                                Please output also the count and the current index of the combobox. Is it really filled? Do you create it two times by accident?

                                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                Visit the Qt Academy at https://academy.qt.io/catalog

                                1 Reply Last reply
                                1

                                4/15

                                2 Mar 2021, 11:18

                                topic:navigator.unread, 11
                                • Login

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