265 lines
9.0 KiB
C++
265 lines
9.0 KiB
C++
#include "formserialportsettingdialog.h"
|
|
#include "ui_formserialportsettingdialog.h"
|
|
|
|
#include <QListView>
|
|
#include <QStandardItemModel>
|
|
#include <QPushButton>
|
|
#include <QMessageBox>
|
|
#include <QFile>
|
|
#include <QTextStream>
|
|
#include <QByteArray>
|
|
#include <QDebug>
|
|
#include <QSerialPort>
|
|
#include <QSerialPortInfo>
|
|
#include <QGraphicsDropShadowEffect>
|
|
#include <QSettings>
|
|
#include <QFileInfo>
|
|
|
|
FormSerialPortSettingDialog::FormSerialPortSettingDialog(QWidget *parent)
|
|
: QDialog(parent)
|
|
, ui(new Ui::FormSerialPortSettingDialog)
|
|
{
|
|
ui->setupUi(this);
|
|
InitializeUi();
|
|
}
|
|
|
|
FormSerialPortSettingDialog::~FormSerialPortSettingDialog()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void FormSerialPortSettingDialog::InitializeUi()
|
|
{
|
|
this->setStyleSheet("background-color: white;");
|
|
this->setProperty("canMove",true);
|
|
//设置窗体透明
|
|
this->setAttribute(Qt::WA_TranslucentBackground, true);
|
|
//设置无边框
|
|
this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
|
|
//实例阴影shadow
|
|
QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
|
|
//设置阴影距离
|
|
shadow->setOffset(0, 0);
|
|
//设置阴影颜色
|
|
shadow->setColor(QColor(44,44,44));
|
|
//设置阴影圆角
|
|
shadow->setBlurRadius(16);
|
|
//给嵌套QDialog设置阴影
|
|
ui->w_bg->setGraphicsEffect(shadow);
|
|
//给垂直布局器设置边距(此步很重要, 设置宽度为阴影的宽度)
|
|
//ui->lay_bg->setMargin(12);
|
|
|
|
QApplication::setOverrideCursor(Qt::WaitCursor);
|
|
|
|
ui->delay_timeout->setText("1000");
|
|
ui->slave_id->setText("1");
|
|
|
|
//设置奇偶校验
|
|
ui->cb_parity->setView(new QListView(this));
|
|
ui->cb_parity->addItem("NoParity", QVariant::fromValue(QSerialPort::NoParity));
|
|
ui->cb_parity->addItem("Even", QVariant::fromValue(QSerialPort::EvenParity));
|
|
ui->cb_parity->addItem("Odd", QVariant::fromValue(QSerialPort::OddParity));
|
|
ui->cb_parity->setCurrentIndex(0);
|
|
|
|
//设置数据位
|
|
ui->cb_data->setView(new QListView(this));
|
|
ui->cb_data->addItem("5", QVariant::fromValue(QSerialPort::Data5));
|
|
ui->cb_data->addItem("6", QVariant::fromValue(QSerialPort::Data6));
|
|
ui->cb_data->addItem("7", QVariant::fromValue(QSerialPort::Data7));
|
|
ui->cb_data->addItem("8", QVariant::fromValue(QSerialPort::Data8));
|
|
ui->cb_data->setCurrentIndex(3);
|
|
|
|
//设置数据位
|
|
ui->cb_stop->setView(new QListView(this));
|
|
ui->cb_stop->addItem("1", QVariant::fromValue(QSerialPort::OneStop));
|
|
ui->cb_stop->addItem("2", QVariant::fromValue(QSerialPort::TwoStop));
|
|
ui->cb_stop->addItem("3", QVariant::fromValue(QSerialPort::OneAndHalfStop));
|
|
ui->cb_stop->setCurrentIndex(0);
|
|
|
|
|
|
//设置串口模式
|
|
//ui->cb_baund->setView(new QListView(this));
|
|
//QStandardItemModel* model_baund = new QStandardItemModel(this);
|
|
|
|
ui->cb_baund->addItem("4800", QVariant::fromValue(QSerialPort::Baud4800));
|
|
ui->cb_baund->addItem("9600", QVariant::fromValue(QSerialPort::Baud9600));
|
|
ui->cb_baund->addItem("19200", QVariant::fromValue(QSerialPort::Baud19200));
|
|
ui->cb_baund->addItem("38400", QVariant::fromValue(QSerialPort::Baud38400));
|
|
ui->cb_baund->addItem("57600", QVariant::fromValue(QSerialPort::Baud57600));
|
|
ui->cb_baund->addItem("115200", QVariant::fromValue(QSerialPort::Baud115200));
|
|
ui->cb_baund->setCurrentIndex(1);
|
|
|
|
//设置串口类型
|
|
ui->cb_type->setView(new QListView(this));
|
|
ui->cb_type->addItem("Modbus RTU");
|
|
|
|
//设置串口号
|
|
ui->cb_serialport->setView(new QListView(this));
|
|
QStringList comport = get_avail_sp_();
|
|
foreach (const QString& str, comport)
|
|
{
|
|
QStringList parts = str.split("-", Qt::SkipEmptyParts);
|
|
ui->cb_serialport->addItem(str,parts[0].trimmed());
|
|
}
|
|
|
|
ui->interval->setText("15");
|
|
|
|
ui->buttonBox->clear();
|
|
QPushButton *okButton = ui->buttonBox->addButton(QDialogButtonBox::Ok);
|
|
QPushButton *cancelButton = ui->buttonBox->addButton(QDialogButtonBox::Cancel);
|
|
okButton->setText(tr("OK"));
|
|
cancelButton->setText(tr("Close"));
|
|
okButton->setFixedSize(100, 30);
|
|
cancelButton->setFixedSize(100, 30);
|
|
|
|
connect(okButton, &QPushButton::clicked, this, &FormSerialPortSettingDialog::onOkClicked);
|
|
connect(cancelButton, &QPushButton::clicked, this, &FormSerialPortSettingDialog::onCancelClicked);
|
|
|
|
//恢复原配置
|
|
QString appDir = QCoreApplication::applicationDirPath();
|
|
QString iniFilePath = appDir + QString::fromStdString("/emsshower.ini");
|
|
|
|
QFileInfo checkFile(iniFilePath);
|
|
|
|
// 文件已存在且是普通文件
|
|
if (checkFile.exists() && checkFile.isFile())
|
|
{
|
|
QSettings* pSettings = new QSettings(iniFilePath, QSettings::IniFormat,this);
|
|
|
|
int baund = pSettings->value("modbus/baund",9600).toInt();
|
|
QString tmp = QString("%1").arg(baund);
|
|
|
|
int targetIndex = ui->cb_baund->findText(tmp); // 精确匹配
|
|
if (targetIndex != -1)
|
|
{
|
|
ui->cb_baund->setCurrentIndex(targetIndex); // 自动触发滚动定位[3](@ref)
|
|
}
|
|
|
|
QString com = pSettings->value("modbus/com","COM1").toString();
|
|
for(int i=0; i<ui->cb_serialport->count(); ++i)
|
|
{
|
|
if(ui->cb_serialport->itemText(i).contains(com+" -"))
|
|
{
|
|
ui->cb_serialport->setCurrentIndex(i);
|
|
}
|
|
}
|
|
|
|
int data = pSettings->value("modbus/data",8).toInt();
|
|
tmp = QString("%1").arg(data);
|
|
|
|
targetIndex = ui->cb_data->findText(tmp); // 精确匹配
|
|
if (targetIndex != -1)
|
|
{
|
|
ui->cb_data->setCurrentIndex(targetIndex); // 自动触发滚动定位[3](@ref)
|
|
}
|
|
|
|
int parity = pSettings->value("modbus/parity",0).toInt();
|
|
|
|
if (parity == 0)
|
|
tmp = "NoParity";
|
|
else if(parity == 2)
|
|
tmp = "Even";
|
|
else if(parity == 3)
|
|
tmp = "Odd";
|
|
else
|
|
tmp = "NoParity";
|
|
|
|
targetIndex = ui->cb_parity->findText(tmp); // 精确匹配
|
|
if (targetIndex != -1)
|
|
{
|
|
ui->cb_parity->setCurrentIndex(targetIndex); // 自动触发滚动定位[3](@ref)
|
|
}
|
|
|
|
int stop = pSettings->value("modbus/stop",1).toInt();
|
|
tmp = QString("%1").arg(stop);
|
|
|
|
targetIndex = ui->cb_stop->findText(tmp); // 精确匹配
|
|
if (targetIndex != -1)
|
|
{
|
|
ui->cb_stop->setCurrentIndex(targetIndex); // 自动触发滚动定位[3](@ref)
|
|
}
|
|
|
|
int interval = pSettings->value("modbus/interval",15).toInt();
|
|
tmp = QString("%1").arg(interval);
|
|
ui->interval->setText(tmp);
|
|
|
|
int slave_id = pSettings->value("slaves/slave_id",1).toInt();
|
|
tmp = QString("%1").arg(slave_id);
|
|
ui->slave_id->setText(tmp);
|
|
|
|
int delay = pSettings->value("modbus/delay",1000).toInt();
|
|
tmp = QString("%1").arg(delay);
|
|
ui->delay_timeout->setText(tmp);
|
|
}
|
|
else
|
|
{
|
|
QSettings* pSettings = new QSettings(iniFilePath, QSettings::IniFormat,this);
|
|
|
|
pSettings->setValue("modbus/baund",9600);
|
|
pSettings->setValue("modbus/com","COM1");
|
|
pSettings->setValue("modbus/data",8);
|
|
pSettings->setValue("modbus/parity",0);
|
|
pSettings->setValue("modbus/stop",1);
|
|
pSettings->setValue("modbus/interval",15);
|
|
pSettings->setValue("modbus/delay",1000);
|
|
pSettings->setValue("slaves/slave_id",1);
|
|
|
|
pSettings->setValue("version/ver","1_0_0");
|
|
pSettings->setValue("modbus/type",1);
|
|
|
|
pSettings->setValue("modbus/ip","127.0.0.1");
|
|
pSettings->setValue("modbus/port",502);
|
|
}
|
|
// 恢复默认光标
|
|
QApplication::restoreOverrideCursor();
|
|
}
|
|
|
|
QStringList FormSerialPortSettingDialog::get_avail_sp_() noexcept
|
|
{
|
|
QStringList list_avail_sp;
|
|
|
|
foreach (const QSerialPortInfo &port, QSerialPortInfo::availablePorts())
|
|
{
|
|
list_avail_sp.append(port.portName() + " - " + port.description());
|
|
}
|
|
|
|
list_avail_sp.sort();
|
|
|
|
return list_avail_sp;
|
|
}
|
|
|
|
void FormSerialPortSettingDialog::onOkClicked()
|
|
{
|
|
//写到INI文件
|
|
QString appDir = QCoreApplication::applicationDirPath();
|
|
QString iniFilePath = appDir + QString::fromStdString("/emsshower.ini");
|
|
QSettings* pSettings = new QSettings(iniFilePath, QSettings::IniFormat,this);
|
|
|
|
//写选中的串口
|
|
int baund = ui->cb_baund->currentData().toInt();
|
|
QString com = ui->cb_serialport->currentData().toString();
|
|
int data = ui->cb_data->currentData().toInt();
|
|
int parity = ui->cb_parity->currentData().toInt();
|
|
int stop = ui->cb_stop->currentData().toInt();
|
|
int slave_id = ui->slave_id->text().toInt();
|
|
int intervai = ui->interval->text().toInt();
|
|
int delay = ui->delay_timeout->text().toInt();
|
|
|
|
pSettings->setValue("modbus/baund",baund);
|
|
pSettings->setValue("modbus/com",com);
|
|
pSettings->setValue("modbus/data",data);
|
|
pSettings->setValue("modbus/parity",parity);
|
|
pSettings->setValue("modbus/stop",stop);
|
|
pSettings->setValue("modbus/interval",intervai);
|
|
pSettings->setValue("modbus/delay",delay);
|
|
pSettings->setValue("slaves/slave_id",slave_id);
|
|
|
|
accept();
|
|
}
|
|
|
|
void FormSerialPortSettingDialog::onCancelClicked()
|
|
{
|
|
QDialog::reject();
|
|
}
|
|
|