#include "mainwindow.h" #include "ui_mainwindow.h" #include #include #include "utils.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); // init UI signals QObject::connect(ui->pbReloadPorts, &QPushButton::clicked, this, &MainWindow::loadPortList); QObject::connect(ui->pbOpenPort, &QPushButton::clicked, this, &MainWindow::togglePort); QObject::connect(this, &MainWindow::portToggled, this, &MainWindow::onPortToggled); QObject::connect(ui->cbPortList, SELECT::OVERLOAD_OF(&QComboBox::activated), this, &MainWindow::selectPort); QObject::connect(ui->cbBaudRate, SELECT::OVERLOAD_OF(&QComboBox::activated), this, &MainWindow::selectBaudRate); QObject::connect(&(this->serialPort), &QSerialPort::readyRead, this, &MainWindow::onDataReady); loadPortList(); loadBaudRateList(); ui->cbBaudRate->setCurrentIndex(ui->cbBaudRate->findText("9600")); } MainWindow::~MainWindow() { delete ui; } void MainWindow::loadPortList() { QString currentSelection = ui->cbPortList->currentText(); ui->cbPortList->clear(); for (auto port : QSerialPortInfo::availablePorts()) { ui->cbPortList->addItem(port.portName()); } // find current selection in the new list, maybe it doesn't exist anymore? int currentSelectionIndex = ui->cbPortList->findText(currentSelection); if (currentSelectionIndex >= 0) { ui->cbPortList->setCurrentIndex(currentSelectionIndex); } else // our port doesn't exist anymore, close port if it's open { if (serialPort.isOpen()) togglePort(); } } void MainWindow::loadBaudRateList() { ui->cbBaudRate->clear(); for (auto baudRate : QSerialPortInfo::standardBaudRates()) { ui->cbBaudRate->addItem(QString::number(baudRate)); } } void MainWindow::togglePort() { if (serialPort.isOpen()) { serialPort.close(); qDebug() << "Port closed, " << serialPort.portName(); emit portToggled(false); } else { serialPort.setPortName(ui->cbPortList->currentText()); // open port if (serialPort.open(QIODevice::ReadWrite)) { qDebug() << "Port opened, " << serialPort.portName(); emit portToggled(true); // set baud rate if (!serialPort.setBaudRate(ui->cbBaudRate->currentText().toInt())) { qDebug() << "Set baud rate failed during port opening: " << serialPort.error(); } } else { qDebug() << "Port open error: " << serialPort.error(); } } } void MainWindow::selectPort(QString portName) { // has selection actually changed if (portName != serialPort.portName()) { // if another port is already open, close it by toggling if (serialPort.isOpen()) { togglePort(); // open new selection by toggling togglePort(); } } } void MainWindow::selectBaudRate(QString baudRate) { if (serialPort.isOpen()) { if (!serialPort.setBaudRate(baudRate.toInt())) { qDebug() << "Set baud rate failed during select: " << serialPort.error(); } else { qDebug() << "Baud rate changed: " << serialPort.baudRate(); } } } void MainWindow::onPortToggled(bool open) { ui->pbOpenPort->setChecked(open); } void MainWindow::onDataReady() { qDebug() << "Data: " << serialPort.readAll().toHex(); }