diff --git a/HSD_Config.pro b/HSD_Config.pro index f3690c1..a8fdb29 100644 --- a/HSD_Config.pro +++ b/HSD_Config.pro @@ -1,4 +1,4 @@ -QT += core gui +QT += core gui serialport greaterThan(QT_MAJOR_VERSION, 4): QT += widgets diff --git a/HSD_Config.pro.user b/HSD_Config.pro.user index dfd3f34..abd53f6 100644 --- a/HSD_Config.pro.user +++ b/HSD_Config.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -54,7 +54,22 @@ ProjectExplorer.Project.PluginSettings - + + + -fno-delayed-template-parsing + + true + Builtin.Questionable + + true + Builtin.DefaultTidyAndClazy + 8 + + + + true + + ProjectExplorer.Project.Target.0 @@ -293,19 +308,19 @@ 2 - - ProjectExplorer.CustomExecutableRunConfiguration - + Qt4ProjectManager.Qt4RunConfiguration:E:/Y/QT/HSD_Config/HSD_Config.pro + E:/Y/QT/HSD_Config/HSD_Config.pro false false true + true false false true - + E:/Y/QT/build-HSD_Config-Desktop_Qt_5_12_9_MinGW_32_bit-Debug 1 diff --git a/widget.cpp b/widget.cpp index 815d5f8..3c5d456 100644 --- a/widget.cpp +++ b/widget.cpp @@ -6,6 +6,17 @@ Widget::Widget(QWidget *parent) , ui(new Ui::Widget) { ui->setupUi(this); + + // 启动时刷新串口 + QStringList serialNamePort; + foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) + { + serialNamePort << info.portName() + "|" + info.description(); + } + ui -> SerialPort_comboBox -> clear(); + ui -> SerialPort_comboBox -> addItems(serialNamePort); + + Serial_port = new QSerialPort(this); } Widget::~Widget() @@ -13,3 +24,152 @@ Widget::~Widget() delete ui; } + +void Widget::on_SerialPort_refresh_pushButton_clicked() +{ + QStringList serialNamePort; + foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) + { + serialNamePort << info.portName() + "|" + info.description(); + } + ui -> SerialPort_comboBox -> clear(); + ui -> SerialPort_comboBox -> addItems(serialNamePort); +} + +//打开关闭按钮状态 +//1将打开按钮 +//0将关闭按钮 +int8_t open_button_status = 1; +void Widget::on_Open_Serial_pushButton_clicked() +{ + if(open_button_status == 1) + { + QSerialPort::BaudRate Baudrate = QSerialPort::Baud9600; + QSerialPort::DataBits Databits = QSerialPort::Data8; + QSerialPort::Parity Parity = QSerialPort::NoParity; + QSerialPort::StopBits Stopbits = QSerialPort::OneStop; + + QComboBox *SerialPortbox = ui -> SerialPort_comboBox; + QComboBox *Baudbox = ui -> BaudRate_comboBox; + QComboBox *Databitsbox = ui -> DataBits_comboBox; + QComboBox *Checkbitsbox = ui -> CheckBits_comboBox; + QComboBox *Stopbitsbox = ui -> StopBits_comboBox; + + //波特率 + if(Baudbox -> currentText() == "4800"){ + Baudrate = QSerialPort::Baud4800; + } + else if(Baudbox -> currentText() == "9600"){ + Baudrate = QSerialPort::Baud9600; + } + else if(Baudbox -> currentText() == "19200"){ + Baudrate = QSerialPort::Baud19200; + } + else if(Baudbox -> currentText() == "38400"){ + Baudrate = QSerialPort::Baud38400; + } + else if(Baudbox -> currentText() == "57600"){ + Baudrate = QSerialPort::Baud57600; + } + else if(Baudbox -> currentText() == "115200"){ + Baudrate = QSerialPort::Baud115200; + } + else { + QMessageBox::warning(this, "错误", "波特率未设置"); + return; + } + + //数据位 + if(Databitsbox -> currentText() == "6"){ + Databits = QSerialPort::Data6; + } + else if(Databitsbox -> currentText() == "7"){ + Databits = QSerialPort::Data7; + } + else if(Databitsbox -> currentText() == "8"){ + Databits = QSerialPort::Data8; + } + else { + QMessageBox::warning(this, "错误", "数据位未设置"); + return; + } + + //校验位 + if(Checkbitsbox -> currentText() == "无"){ + Parity = QSerialPort::NoParity; + } + else if(Checkbitsbox -> currentText() == "奇"){ + Parity = QSerialPort::OddParity; + } + else if(Checkbitsbox -> currentText() == "偶"){ + Parity = QSerialPort::EvenParity; + } + else { + QMessageBox::warning(this, "错误", "校验位未设置"); + return; + } + + //停止位 + if(Stopbitsbox -> currentText() == "1"){ + Stopbits = QSerialPort::OneStop; + } + else if(Stopbitsbox -> currentText() == "1.5"){ + Stopbits = QSerialPort::OneAndHalfStop; + } + else if(Stopbitsbox -> currentText() == "2"){ + Stopbits = QSerialPort::TwoStop; + } + else { + QMessageBox::warning(this, "错误", "停止位未设置"); + return; + } + + //设置参数 + Serial_port -> setPort((const QSerialPortInfo)(SerialPortbox -> currentText().section('|', 0, 0))); + Serial_port -> setBaudRate(Baudrate); + Serial_port -> setDataBits(Databits); + Serial_port -> setParity(Parity); + Serial_port -> setStopBits(Stopbits); + Serial_port -> setReadBufferSize(4096); + if(Serial_port -> open(QIODevice::ReadWrite) == true){ + // 打开后将按钮变为关闭按钮,绿色指示标志 + ui -> Open_Serial_pushButton -> setText("关闭串口"); + ui -> serial_status_label -> setStyleSheet("QLabel { background-color :rgb(0, 144, 0); border-radius: 10px;}"); + //打开串口后禁止配置 + ui -> SerialPort_comboBox -> setEnabled(false); + ui -> BaudRate_comboBox -> setEnabled(false); + ui -> DataBits_comboBox -> setEnabled(false); + ui -> CheckBits_comboBox -> setEnabled(false); + ui -> StopBits_comboBox -> setEnabled(false); + ui -> SerialPort_refresh_pushButton -> setEnabled(false); + ui -> BaudRate_label -> setEnabled(false); + ui -> DataBits_label -> setEnabled(false); + ui -> CheckBits_label -> setEnabled(false); + ui -> StopBits_label -> setEnabled(false); + + } + else{ + QMessageBox::critical(this, "提示", "串口打开失败"); + return; + } + } + else { + //将按钮变为打开按钮,红色指示标志 + ui -> Open_Serial_pushButton ->setText("打开串口"); + ui -> serial_status_label ->setStyleSheet("QLabel { background-color :rgb(184, 0, 0); border-radius: 10px;}"); + Serial_port -> close(); + //关闭串口后允许配置 + ui -> SerialPort_comboBox -> setEnabled(true); + ui -> BaudRate_comboBox -> setEnabled(true); + ui -> DataBits_comboBox -> setEnabled(true); + ui -> CheckBits_comboBox -> setEnabled(true); + ui -> StopBits_comboBox -> setEnabled(true); + ui -> SerialPort_refresh_pushButton -> setEnabled(true); + ui -> BaudRate_label -> setEnabled(true); + ui -> DataBits_label -> setEnabled(true); + ui -> CheckBits_label -> setEnabled(true); + ui -> StopBits_label -> setEnabled(true); + + } + open_button_status = !open_button_status; +} diff --git a/widget.h b/widget.h index 1fe2322..f7a5f20 100644 --- a/widget.h +++ b/widget.h @@ -2,6 +2,9 @@ #define WIDGET_H #include +#include +#include +#include QT_BEGIN_NAMESPACE namespace Ui { class Widget; } @@ -15,7 +18,16 @@ public: Widget(QWidget *parent = nullptr); ~Widget(); +private slots: + void on_SerialPort_refresh_pushButton_clicked(); + + void on_Open_Serial_pushButton_clicked(); + private: Ui::Widget *ui; + +private: + //串口 + QSerialPort * Serial_port; }; #endif // WIDGET_H diff --git a/widget.ui b/widget.ui index b90248d..41ede21 100644 --- a/widget.ui +++ b/widget.ui @@ -6,13 +6,610 @@ 0 0 - 800 - 600 + 530 + 231 - Widget + HSD变频器控制 + + + + + + + 11 + 10 + 171 + 211 + + + + + 1 + + + 1 + + + 1 + + + 1 + + + 1 + + + + + + 宋体 + 12 + + + + + + + + 刷新 + + + + + + + + + + 宋体 + 12 + + + + 波特率: + + + + + + + + 宋体 + 12 + + + + 6 + + + + 110 + + + + + 300 + + + + + 600 + + + + + 1200 + + + + + 2400 + + + + + 4800 + + + + + 9600 + + + + + 14400 + + + + + 19200 + + + + + 38400 + + + + + 43000 + + + + + 57600 + + + + + 76800 + + + + + 115200 + + + + + 128000 + + + + + 230400 + + + + + 256000 + + + + + 460800 + + + + + 921600 + + + + + 1000000 + + + + + 2000000 + + + + + 3000000 + + + + + + + + + + + + + 宋体 + 12 + + + + 数据位: + + + + + + + + 宋体 + 12 + + + + 3 + + + + 5 + + + + + 6 + + + + + 7 + + + + + 8 + + + + + + + + + + + + + 宋体 + 12 + + + + 校验位: + + + + + + + + 宋体 + 12 + + + + + + + + + + + + + + + + + + + + + + + + + + + + 宋体 + 12 + + + + 停止位: + + + + + + + + 宋体 + 12 + + + + + 1 + + + + + 1.5 + + + + + 2 + + + + + + + + + + + + + 0 + 0 + + + + + 宋体 + 12 + + + + 打开串口 + + + + + + + + 0 + 0 + + + + + 20 + 20 + + + + + 20 + 20 + + + + background-color:rgb(184, 0, 0); /* 背景 */ +border-radius: 10px; /* 圆角半径 */ + + + + + + + + + + + + + + 190 + 280 + 291 + 271 + + + + 01062000000143CA 开 +0106200000054209 关 +0106100003E88DB4 5Hz +0106100002E2 0C23 6.5Hz +0106100007D0 8EA6 10Hz +010610000A64 8A41 13.3Hz +010610000C1C 89C3 15.5Hz +010610000E38 88B8 18.2Hz +010610000FA0 8882 20Hz +0106100010048109 20.5Hz +0106100014A0 8272 26.4Hz +010610001608 82AC 28.2Hz +010610001770 831E 30Hz +010610001A04 87A9 33.3Hz +010610001AE0 87E2 34.4Hz +010610001BBC 864B 35.5Hz + + + + + + 190 + 10 + 218 + 211 + + + + + + + 常用频率 + + + + 1 + + + 1 + + + 1 + + + 1 + + + 1 + + + + + + + 5Hz + + + + + + + 10Hz + + + + + + + + + + + 15Hz + + + + + + + 20Hz + + + + + + + + + + + 25Hz + + + + + + + 30Hz + + + + + + + + + + + 35Hz + + + + + + + 40Hz + + + + + + + + + + + 45Hz + + + + + + + 50Hz + + + + + + + + + + + + 1 + + + + + + + + 设定频率 + + + + + + + + + + + 420 + 10 + 101 + 211 + + + + + + + + 30 + 30 + + + + + 500 + 500 + + + + + + + 启动 + + + + + + + + 30 + 30 + + + + + 500 + 500 + + + + 停机 + + + + +