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. Open the port which selected on combo box
QtWS25 Last Chance

Open the port which selected on combo box

Scheduled Pinned Locked Moved Unsolved General and Desktop
comboboxserial portqt5
10 Posts 3 Posters 2.2k 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 19 Feb 2021, 07:55 last edited by
    #1

    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);
    }
    
    J 1 Reply Last reply 19 Feb 2021, 08:02
    0
    • D deleted286
      19 Feb 2021, 07:55

      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);
      }
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 19 Feb 2021, 08:02 last edited by
      #2

      @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 19 Feb 2021, 08:10
      0
      • J jsulm
        19 Feb 2021, 08:02

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

        D Offline
        D Offline
        deleted286
        wrote on 19 Feb 2021, 08:10 last edited by
        #3

        @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

        J 1 Reply Last reply 19 Feb 2021, 08:14
        0
        • D deleted286
          19 Feb 2021, 08:10

          @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

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 19 Feb 2021, 08:14 last edited by
          #4

          @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 19 Feb 2021, 08:15
          0
          • J jsulm
            19 Feb 2021, 08:14

            @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");
            
            D Offline
            D Offline
            deleted286
            wrote on 19 Feb 2021, 08:15 last edited by
            #5

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

            ui->ports->currentText()
            

            it didnt change anything

            J 1 Reply Last reply 19 Feb 2021, 08:17
            0
            • D deleted286
              19 Feb 2021, 08:15

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

              ui->ports->currentText()
              

              it didnt change anything

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 19 Feb 2021, 08:17 last edited by
              #6

              @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 19 Feb 2021, 08:21
              0
              • J jsulm
                19 Feb 2021, 08:17

                @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();
                    }
                
                D Offline
                D Offline
                deleted286
                wrote on 19 Feb 2021, 08:21 last edited by
                #7

                @jsulm ttyS5 because combobox keep it always

                D J 2 Replies Last reply 19 Feb 2021, 08:22
                0
                • D deleted286
                  19 Feb 2021, 08:21

                  @jsulm ttyS5 because combobox keep it always

                  D Offline
                  D Offline
                  deleted286
                  wrote on 19 Feb 2021, 08:22 last edited by
                  #8

                  @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
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 19 Feb 2021, 08:23 last edited by Christian Ehrlicher
                    #9

                    @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
                    1
                    • D deleted286
                      19 Feb 2021, 08:21

                      @jsulm ttyS5 because combobox keep it always

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 19 Feb 2021, 08:23 last edited by
                      #10

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

                      1/10

                      19 Feb 2021, 07:55

                      • Login

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