76 lines
2.4 KiB
C++
76 lines
2.4 KiB
C++
|
|
#include "SerialConfig.h"
|
||
|
|
#include <QSerialPortInfo>
|
||
|
|
#include <QPushButton>
|
||
|
|
#include <QMessageBox>
|
||
|
|
|
||
|
|
SerialConfig::SerialConfig(QWidget *parent) : QDialog(parent) {
|
||
|
|
|
||
|
|
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
||
|
|
setWindowTitle("串口配置");
|
||
|
|
|
||
|
|
QFormLayout *formLayout = new QFormLayout(this);
|
||
|
|
|
||
|
|
serialPortComboBox = new QComboBox();
|
||
|
|
formLayout->addRow(new QLabel("串口:"), serialPortComboBox);
|
||
|
|
scanSerialPorts();
|
||
|
|
|
||
|
|
baudRateComboBox = new QComboBox();
|
||
|
|
baudRateComboBox->addItems({"9600", "19200", "38400", "57600", "115200"});
|
||
|
|
formLayout->addRow(new QLabel("波特率:"), baudRateComboBox);
|
||
|
|
|
||
|
|
dataBitsComboBox = new QComboBox();
|
||
|
|
dataBitsComboBox->addItems({"8", "7", "6", "5"});
|
||
|
|
formLayout->addRow(new QLabel("数据位:"), dataBitsComboBox);
|
||
|
|
|
||
|
|
stopBitsComboBox = new QComboBox();
|
||
|
|
stopBitsComboBox->addItems({"1", "1.5", "2"});
|
||
|
|
formLayout->addRow(new QLabel("停止位:"), stopBitsComboBox);
|
||
|
|
|
||
|
|
parityComboBox = new QComboBox();
|
||
|
|
parityComboBox->addItems({"0", "偶校验", "奇校验"});
|
||
|
|
formLayout->addRow(new QLabel("校验:"), parityComboBox);
|
||
|
|
|
||
|
|
QDialogButtonBox *buttonBox = new QDialogButtonBox();
|
||
|
|
QPushButton *okButton = new QPushButton("确认");
|
||
|
|
QPushButton *cancelButton = new QPushButton("取消");
|
||
|
|
buttonBox->addButton(okButton, QDialogButtonBox::AcceptRole);
|
||
|
|
buttonBox->addButton(cancelButton, QDialogButtonBox::RejectRole);
|
||
|
|
|
||
|
|
connect(okButton, &QPushButton::clicked, this, &QDialog::accept);
|
||
|
|
connect(cancelButton, &QPushButton::clicked, this, &QDialog::reject);
|
||
|
|
|
||
|
|
formLayout->addRow(buttonBox);
|
||
|
|
}
|
||
|
|
|
||
|
|
QString SerialConfig::getSerialPort() const {
|
||
|
|
return serialPortComboBox->currentText();
|
||
|
|
}
|
||
|
|
|
||
|
|
QString SerialConfig::getBaudRate() const {
|
||
|
|
return baudRateComboBox->currentText();
|
||
|
|
}
|
||
|
|
|
||
|
|
QString SerialConfig::getDataBits() const {
|
||
|
|
return dataBitsComboBox->currentText();
|
||
|
|
}
|
||
|
|
|
||
|
|
QString SerialConfig::getStopBits() const {
|
||
|
|
return stopBitsComboBox->currentText();
|
||
|
|
}
|
||
|
|
|
||
|
|
QString SerialConfig::getParity() const {
|
||
|
|
return parityComboBox->currentText();
|
||
|
|
}
|
||
|
|
|
||
|
|
void SerialConfig::scanSerialPorts() {
|
||
|
|
serialPortComboBox->clear();
|
||
|
|
const auto ports = QSerialPortInfo::availablePorts();
|
||
|
|
for (const QSerialPortInfo &port : ports) {
|
||
|
|
serialPortComboBox->addItem(port.portName());
|
||
|
|
}
|
||
|
|
|
||
|
|
if (serialPortComboBox->count() == 0) {
|
||
|
|
serialPortComboBox->addItem("无可用串口");
|
||
|
|
}
|
||
|
|
}
|