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.6k 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 deleted286

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

    }

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on 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
    0
    • jsulmJ jsulm

      @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 last edited by
      #3

      @jsulm but it was selected

      jsulmJ 1 Reply Last reply
      0
      • D deleted286

        @jsulm but it was selected

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on 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
        0
        • jsulmJ jsulm

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

          D Offline
          D Offline
          deleted286
          wrote on last edited by
          #5

          @jsulm yes it was selected

          jsulmJ 1 Reply Last reply
          0
          • D deleted286

            @jsulm yes it was selected

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on 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
            0
            • jsulmJ jsulm

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

              D Offline
              D Offline
              deleted286
              wrote on 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);
              }
              
              Christian EhrlicherC jsulmJ 2 Replies Last reply
              0
              • D deleted286

                @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);
                }
                
                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 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
                0
                • D deleted286

                  @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);
                  }
                  
                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 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
                  0
                  • Christian EhrlicherC Christian Ehrlicher

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

                    D Offline
                    D Offline
                    deleted286
                    wrote on last edited by
                    #10

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

                    serial->setPortName(ui->ports->currentText());
                    
                    1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @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 last edited by
                      #11

                      @jsulm it returns empty again

                      jsulmJ 1 Reply Last reply
                      0
                      • D deleted286

                        @jsulm it returns empty again

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 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
                        0
                        • jsulmJ jsulm

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

                          jsulmJ 1 Reply Last reply
                          0
                          • D deleted286

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

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 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
                            • Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 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

                              • Login

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