Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Open the port which selected on combo box



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

  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

    @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");
    


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

    ui->ports->currentText()
    

    it didnt change anything


  • Lifetime Qt Champion

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


  • @jsulm ttyS5 because combobox keep it always



  • @suslucoder

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

    And this code scripts returns description and manufacturer empty.


  • Lifetime Qt Champion

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


  • Lifetime Qt Champion

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