149 lines
4.1 KiB
C++
149 lines
4.1 KiB
C++
#include "mainwindow.h"
|
|
#include "ui_mainwindow.h"
|
|
|
|
#include<QGridLayout>
|
|
#include<QFileDialog>
|
|
#include<QPushButton>
|
|
#include <QDebug>
|
|
#include <QMessageBox>
|
|
|
|
|
|
MainWindow::MainWindow(QWidget *parent)
|
|
: QMainWindow(parent)
|
|
, ui(new Ui::MainWindow)
|
|
{
|
|
ui->setupUi(this);
|
|
|
|
Init();
|
|
}
|
|
|
|
MainWindow::~MainWindow()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void MainWindow::openFile()
|
|
{
|
|
QString s = QFileDialog::getOpenFileName(this,"选择文件","/", "Files(*.json)");
|
|
qDebug() << s << endl;
|
|
}
|
|
|
|
void MainWindow::scanSerialPort()
|
|
{
|
|
/* 查找可用串口 */
|
|
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
|
|
// ui->SerialPort->addItem(info.portName());
|
|
ui->SerialPort->addItem(info.portName() + ":" +info.description());
|
|
}
|
|
|
|
}
|
|
|
|
void MainWindow::parityItemInit()
|
|
{
|
|
QList <QString> list;
|
|
list<<"None"<<"Even"<<"Odd"<<"Space"<<"Mark";
|
|
for (int i = 0; i < 5; i++) {
|
|
ui->comboBox_4->addItem(list[i]);
|
|
}
|
|
ui->comboBox_4->setCurrentIndex(0);
|
|
}
|
|
|
|
void MainWindow::Init()
|
|
{
|
|
scanSerialPort();
|
|
parityItemInit();
|
|
serialPort = new QSerialPort(this);
|
|
|
|
connect(ui->openFile,SIGNAL(clicked()),this,SLOT(openFile()));
|
|
connect(ui->SerialPortPushButton,SIGNAL(clicked())
|
|
,this,SLOT(openSerialPortPushButtonClicked()));
|
|
|
|
}
|
|
|
|
void MainWindow::openSerialPortPushButtonClicked()
|
|
{
|
|
if (ui->SerialPortPushButton->text() == "打开串口") {
|
|
/* 设置串口名 */
|
|
// serialPort->setPortName(ui->SerialPort->currentText());
|
|
serialPort->setPort((const QSerialPortInfo)(ui->SerialPort->currentText().section(':', 0, 0)));
|
|
|
|
/* 设置波特率 */
|
|
serialPort->setBaudRate(ui->comboBox_2->currentText().toInt());
|
|
|
|
/* 设置数据位数 */
|
|
switch (ui->comboBox_3->currentText().toInt()) {
|
|
case 5:
|
|
serialPort->setDataBits(QSerialPort::Data5);
|
|
break;
|
|
case 6:
|
|
serialPort->setDataBits(QSerialPort::Data6);
|
|
break;
|
|
case 7:
|
|
serialPort->setDataBits(QSerialPort::Data7);
|
|
break;
|
|
case 8:
|
|
serialPort->setDataBits(QSerialPort::Data8);
|
|
break;
|
|
default: break;
|
|
}
|
|
|
|
/* 设置奇偶校验 */
|
|
switch (ui->comboBox_4->currentIndex()) {
|
|
case 0:
|
|
serialPort->setParity(QSerialPort::NoParity);
|
|
break;
|
|
case 1:
|
|
serialPort->setParity(QSerialPort::EvenParity);
|
|
break;
|
|
case 2:
|
|
serialPort->setParity(QSerialPort::OddParity);
|
|
break;
|
|
case 3:
|
|
serialPort->setParity(QSerialPort::SpaceParity);
|
|
break;
|
|
case 4:
|
|
serialPort->setParity(QSerialPort::MarkParity);
|
|
break;
|
|
default: break;
|
|
}
|
|
|
|
/* 设置停止位 */
|
|
switch (ui->comboBox_5->currentText().toInt()) {
|
|
case 1:
|
|
serialPort->setStopBits(QSerialPort::OneStop);
|
|
break;
|
|
case 2:
|
|
serialPort->setStopBits(QSerialPort::TwoStop);
|
|
break;
|
|
default: break;
|
|
}
|
|
|
|
/* 设置流控制 */
|
|
serialPort->setFlowControl(QSerialPort::NoFlowControl);
|
|
if (!serialPort->open(QIODevice::ReadWrite))
|
|
QMessageBox::about(NULL, "错误",
|
|
"串口无法打开!可能串口已经被占用!");
|
|
else {
|
|
ui->SerialPort->setEnabled(false);
|
|
ui->comboBox_2->setEnabled(false);
|
|
ui->comboBox_3->setEnabled(false);
|
|
ui->comboBox_4->setEnabled(false);
|
|
ui->comboBox_5->setEnabled(false);
|
|
|
|
ui->SerialPortPushButton->setText("关闭串口");
|
|
ui->label_6->setStyleSheet("color: red;");
|
|
// pushButton[0]->setEnabled(true);
|
|
}
|
|
|
|
} else {
|
|
serialPort->close();
|
|
ui->SerialPort->setEnabled(true);
|
|
ui->comboBox_2->setEnabled(true);
|
|
ui->comboBox_3->setEnabled(true);
|
|
ui->comboBox_4->setEnabled(true);
|
|
ui->comboBox_5->setEnabled(true);
|
|
ui->label_6->setStyleSheet("color: block;");
|
|
ui->SerialPortPushButton->setText("打开串口");
|
|
}
|
|
}
|