Repository navigation
Wizard serial port detect #656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /* | ||
| * SerialPortComboHelper.cpp | ||
| * | ||
| * Project: Prismatik | ||
| * | ||
| * Prismatik is a free, open-source software: you can redistribute it and/or | ||
| * modify it under the terms of the GNU General Public License as published | ||
| * by the Free Software Foundation, either version 2 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * Prismatik and Lightpack files is distributed in the hope that it will be | ||
| * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| * General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| * | ||
| */ | ||
|
|
||
| #include "SerialPortComboHelper.hpp" | ||
|
|
||
| #include <QCollator> | ||
| #include <QComboBox> | ||
| #include <QSerialPortInfo> | ||
| #include <QSignalBlocker> | ||
| #include <QStringList> | ||
| #include <algorithm> | ||
|
|
||
| namespace SerialPortComboHelper | ||
| { | ||
|
|
||
| void populate(QComboBox *comboBox, const QString &preferredPort, const bool preferredIsDefault) | ||
| { | ||
| QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts(); | ||
|
|
||
| // USB serial adapters (Arduino, CH340, FTDI, CP210x...) first, then by port name in natural order (COM2 < COM10) | ||
| QCollator collator; | ||
| collator.setNumericMode(true); | ||
| collator.setCaseSensitivity(Qt::CaseInsensitive); | ||
| std::sort(ports.begin(), ports.end(), [&collator](const QSerialPortInfo &a, const QSerialPortInfo &b) { | ||
| if (a.hasVendorIdentifier() != b.hasVendorIdentifier()) | ||
| return a.hasVendorIdentifier(); | ||
| return collator.compare(a.portName(), b.portName()) < 0; | ||
| }); | ||
|
|
||
| const QSignalBlocker blocker(comboBox); | ||
| comboBox->clear(); | ||
|
|
||
| int preferredIdx = -1; | ||
| int firstUsbIdx = -1; | ||
| for (const QSerialPortInfo &port : std::as_const(ports)) { | ||
| const QString description = port.description().isEmpty() ? port.manufacturer() : port.description(); | ||
| const QString label = description.isEmpty() ? port.portName() : port.portName() + QStringLiteral(" - ") + description; | ||
|
|
||
| QStringList tooltip; | ||
| tooltip << port.systemLocation(); | ||
| if (!port.manufacturer().isEmpty()) | ||
| tooltip << port.manufacturer(); | ||
| if (port.hasVendorIdentifier() && port.hasProductIdentifier()) | ||
| tooltip << QStringLiteral("VID:PID %1:%2") | ||
| .arg(port.vendorIdentifier(), 4, 16, QLatin1Char('0')) | ||
| .arg(port.productIdentifier(), 4, 16, QLatin1Char('0')).toUpper(); | ||
|
|
||
| const int idx = comboBox->count(); | ||
| comboBox->addItem(label, port.portName()); | ||
| comboBox->setItemData(idx, tooltip.join(QLatin1Char('\n')), Qt::ToolTipRole); | ||
|
|
||
| if (preferredIdx < 0 && port.portName().compare(preferredPort, Qt::CaseInsensitive) == 0) | ||
| preferredIdx = idx; | ||
|
Comment on lines
+66
to
+70
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On Linux and macOS, Because Please match |
||
| if (firstUsbIdx < 0 && port.hasVendorIdentifier()) | ||
| firstUsbIdx = idx; | ||
| } | ||
|
|
||
| // Keep the configured port if it is connected, unless it is just the built-in default | ||
| // and a USB serial adapter is present. If the configured port is gone (e.g. the device | ||
| // was re-plugged and got a new number), pick the first USB serial adapter instead. | ||
| if (preferredIdx >= 0 && !(preferredIsDefault && firstUsbIdx >= 0)) | ||
| comboBox->setCurrentIndex(preferredIdx); | ||
| else if (firstUsbIdx >= 0) | ||
| comboBox->setCurrentIndex(firstUsbIdx); | ||
| else if (!preferredPort.isEmpty()) | ||
| selectPortName(comboBox, preferredPort); | ||
| else if (comboBox->count() > 0) | ||
| comboBox->setCurrentIndex(0); | ||
| } | ||
|
|
||
| QString selectedPortName(const QComboBox *comboBox) | ||
| { | ||
| const QString text = comboBox->currentText().trimmed(); | ||
| // A detected port is shown as "COM5 - USB-SERIAL CH340"; its port name is in the item data. | ||
| // Anything else was typed by hand and is used as-is. | ||
| const int idx = comboBox->findText(text); | ||
| if (idx >= 0) | ||
| return comboBox->itemData(idx).toString(); | ||
| return text; | ||
| } | ||
|
Comment on lines
+88
to
+97
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the combo box is editable and items use Either put only the bare port name into the |
||
|
|
||
| void selectPortName(QComboBox *comboBox, const QString &portName) | ||
| { | ||
| const int idx = comboBox->findData(portName); | ||
| if (idx >= 0) { | ||
| comboBox->setCurrentIndex(idx); | ||
| } else { | ||
| comboBox->setCurrentIndex(-1); | ||
| comboBox->setEditText(portName); | ||
| } | ||
| } | ||
|
|
||
| } // namespace SerialPortComboHelper | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * SerialPortComboHelper.hpp | ||
| * | ||
| * Project: Prismatik | ||
| * | ||
| * Prismatik is a free, open-source software: you can redistribute it and/or | ||
| * modify it under the terms of the GNU General Public License as published | ||
| * by the Free Software Foundation, either version 2 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * Prismatik and Lightpack files is distributed in the hope that it will be | ||
| * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| * General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| * | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <QString> | ||
|
|
||
| class QComboBox; | ||
|
|
||
| // Fills an (editable) QComboBox with the detected serial ports, labelled with | ||
| // their description (e.g. "COM5 - USB-SERIAL CH340"), keeping the bare port | ||
| // name in the item data. A port name typed by hand is also accepted. | ||
| namespace SerialPortComboHelper | ||
| { | ||
| // Rescans the ports and selects one: the preferred port if it is connected | ||
| // (unless it is only the built-in default and a USB serial adapter is present), | ||
| // otherwise the first USB serial adapter, otherwise the preferred port as text. | ||
| // Emits no signals from the combo box. | ||
| void populate(QComboBox *comboBox, const QString &preferredPort, bool preferredIsDefault); | ||
|
|
||
| // The port name for the current selection or typed text. | ||
| QString selectedPortName(const QComboBox *comboBox); | ||
|
|
||
| // Selects the item for portName, or shows it as typed text if not detected. | ||
| void selectPortName(QComboBox *comboBox, const QString &portName); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider extracting an overload that accepts a list of lightweight port-info structs (instead of calling
QSerialPortInfo::availablePorts()directly) and adding unit tests inSoftware/tests/covering the sorting, fallback selection, and Windows (COMx) vs. Unix (/dev/tty*) path matching.