Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Open the port which selected on combo box

    General and Desktop
    combobox serial port qt5
    3
    10
    1225
    Loading More Posts
    • 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 last edited by

      Hi everyone. I have a combobox for portinfo. Im keeping portnames in combo box. I want to open the port which is selected in combo box. Whatever i choose the box, it focus one port. Combobox shows me the same port always. How can i do it?

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QtSerialPort/QSerialPort>
      #include <QDebug>
      #include <QSerialPortInfo>
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          serial = new QSerialPort();
      
      }
      MainWindow::~MainWindow()
      {
          delete ui;
          serial->close();
      }
      
      void MainWindow::on_ports_activated(const QString &arg1)
      {
      ui->ports->clear();
      
      const auto infos = QSerialPortInfo::availablePorts();
      for(const QSerialPortInfo &info : infos )
      {
          QString s = info.portName();
          ui->ports->addItem(s);
      }
      if (!ui->ports->count())
                  ui->ports->addItem(tr("no com"));
      
       }
      
      void MainWindow::on_pushButton_clicked()
      {
          serial->setPortName("/dev/ttyS5");
          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()<< "ttyS5" << "is Open ";
                serial->clear();
          }
          else
          {
                qDebug()<<"Serial  Port Not Open !!!!";
          }
      
      }
      
      void MainWindow::on_Read_clicked()
      {
          QByteArray SerialData = serial->readAll();
          qDebug() << "okuma yapiyor" ;
          ui->plainTextEdit->insertPlainText(SerialData);
      }
      
      void MainWindow::on_Write_clicked()
      {
          QString a = "hello";
          QByteArray b = a.toUtf8();
          qDebug() << serial->write(b);
      }
      
      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @deleted286 last edited by

        @suslucoder said in Open the port which selected on combo box:

        Whatever i choose the box, it focus one port. Combobox shows me the same port always.

        What do you mean? How many ports do you have in the combo box?

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

        D 1 Reply Last reply Reply Quote 0
        • D
          deleted286 @jsulm last edited by

          @jsulm Screenshot from 2021-02-19 11-08-09.png

          I have 6 different ports. from ttyS0 to ttyS5. I saw all of them in combobox. When i choose ttyS3 for example, it couldt keep the ttys3, it returns ttyS5 immediately

          jsulm 1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @deleted286 last edited by

            @suslucoder said in Open the port which selected on combo box:

            When i choose ttyS3 for example, it couldt keep the ttys3, it returns ttyS5 immediately

            Can you please explain what you mean? Where does it return ttyS5? Are you aware that you always use ttyS5?!

            serial->setPortName("/dev/ttyS5");
            

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

            D 1 Reply Last reply Reply Quote 0
            • D
              deleted286 @jsulm last edited by

              @jsulm what should i write instead of it?
              when i wrote

              ui->ports->currentText()
              

              it didnt change anything

              jsulm 1 Reply Last reply Reply Quote 0
              • jsulm
                jsulm Lifetime Qt Champion @deleted286 last edited by

                @suslucoder

                void MainWindow::on_pushButton_clicked()
                {
                    qDebug() << ui->ports->currentText(); // WHAT DOES THIS PRINT OUT?
                    serial->setPortName("/dev/ttyS5");
                    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()<< "ttyS5" << "is Open ";
                          serial->clear();
                    }
                

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

                D 1 Reply Last reply Reply Quote 0
                • D
                  deleted286 @jsulm last edited by

                  @jsulm ttyS5 because combobox keep it always

                  D jsulm 2 Replies Last reply Reply Quote 0
                  • D
                    deleted286 @deleted286 last edited by

                    @suslucoder

                     qDebug() << "Name        : " << s;
                     qDebug() << "Description : " << info.description();
                     qDebug() << "Manufacturer:"  << info.manufacturer();
                    

                    And this code scripts returns description and manufacturer empty.

                    1 Reply Last reply Reply Quote 0
                    • Christian Ehrlicher
                      Christian Ehrlicher Lifetime Qt Champion last edited by Christian Ehrlicher

                      @suslucoder said in Open the port which selected on combo box:

                      on_ports_activated(const QString &arg1)

                      This is called every time you change the combobox index.

                      Don't use this stupid auto-connect feature and do the connects by your self in the ctor - then noone is wondering why this works at all.

                      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 Reply Quote 1
                      • jsulm
                        jsulm Lifetime Qt Champion @deleted286 last edited by

                        @suslucoder I don't see why it would not be possible to select other entries in a combo box. Do you do anything else with ui->ports?

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

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post