Find the current port and open it
-
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); }
}
-
@suslucoder said in Find the current port and open it:
currentport name returns empty always
So, is anything selected?
-
@suslucoder I mean: is a port selected in combo box (or what ever widget ui->ports is)?
-
@suslucoder How do you know that ui->ports->currentText() returns an empty string?
-
@suslucoder You don't use ui->ports->currentText() anywhere in your code you show us here ...
-
@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 ";
-
@Christian-Ehrlicher im using it to open the port?
serial->setPortName(ui->ports->currentText());
-
@suslucoder said in Find the current port and open it:
it returns empty again
How did you verify that?
Please addqDebug() << ui->ports->currentText();
and see what it prints out.
-
@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? -
@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.
-
Please output also the count and the current index of the combobox. Is it really filled? Do you create it two times by accident?