Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0e69ec5e1e | ||
|
|
5bb9d8b01f | ||
|
|
45cd5d476f | ||
|
|
64ad69df92 | ||
|
|
b7e91a7f6c | ||
|
|
29dced3987 | ||
|
|
adf2fefd29 | ||
|
|
b1ac039671 | ||
|
|
1918061446 | ||
|
|
f446e0cebe | ||
|
|
82ccb3da77 | ||
|
|
6dabd73d59 | ||
|
|
9d82f1e0fd | ||
|
|
3debc889a8 |
No files matched your search
+15
-4
@@ -25,9 +25,20 @@ DEFINES += QT_DEPRECATED_WARNINGS
|
||||
|
||||
SOURCES += \
|
||||
main.cpp \
|
||||
mainwindow.cpp \
|
||||
serialcontroller.cpp
|
||||
serialcontroller.cpp \
|
||||
mainwidget.cpp \
|
||||
mainwindow.cpp
|
||||
|
||||
HEADERS += \
|
||||
mainwindow.h \
|
||||
serialcontroller.h
|
||||
serialcontroller.h \
|
||||
mainwidget.h \
|
||||
mainwindow.h
|
||||
|
||||
RESOURCES += \
|
||||
resource.qrc
|
||||
|
||||
DISTFILES += \
|
||||
serialasst.rc
|
||||
|
||||
RC_FILE += \
|
||||
serialasst.rc
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 2.5 KiB |
@@ -4,6 +4,7 @@
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
a.setWindowIcon(QIcon(QString(":/pic/icon/icon.png")));
|
||||
MainWindow w;
|
||||
w.show();
|
||||
|
||||
|
||||
+331
@@ -0,0 +1,331 @@
|
||||
#include "mainwidget.h"
|
||||
|
||||
MainWidget::MainWidget(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
{
|
||||
//初始化各参数列表
|
||||
BaudrateList<<"256000"<<"230400"<<"128000"<<"115200"<<"76800"<<"57600"
|
||||
<<"43000"<<"38400"<<"19200"<<"14400"<<"9600"<<"4800"<<"2400"<<"1200";
|
||||
StopbitsList<<"1"<<"1.5"<<"2";
|
||||
DatabitsList<<"8"<<"7"<<"6"<<"5";
|
||||
ParityList<<tr("无")<<tr("奇校验")<<tr("偶校验");
|
||||
|
||||
//初始化ComboBox
|
||||
COMBox = new QComboBox();
|
||||
BaudrateBox = new QComboBox();
|
||||
BaudrateBox->addItems(BaudrateList);
|
||||
StopbitsBox = new QComboBox();
|
||||
StopbitsBox->addItems(StopbitsList);
|
||||
DatabitsBox = new QComboBox();
|
||||
DatabitsBox->addItems(DatabitsList);
|
||||
ParityBox = new QComboBox();
|
||||
ParityBox->addItems(ParityList);
|
||||
|
||||
//初始化label
|
||||
BaudrateLabel = new QLabel(tr("波特率"));
|
||||
StopbitsLabel = new QLabel(tr("停止位"));
|
||||
DatabitsLabel = new QLabel(tr("数据位"));
|
||||
ParityLabel = new QLabel(tr("校验位"));
|
||||
|
||||
//发送与接受区域
|
||||
RecvArea = new QTextEdit();
|
||||
RecvArea->setReadOnly(true);
|
||||
QPalette pal;
|
||||
pal.setColor(QPalette::Base, Qt::black);
|
||||
pal.setColor(QPalette::Text, Qt::green);
|
||||
RecvArea->setPalette(pal);
|
||||
SendArea = new QTextEdit();
|
||||
RecvArea->setFont(QFont(tr("Microsoft YaHei UI"), 10));
|
||||
SendArea->setFont(QFont(tr("Microsoft YaHei UI Light"), 12));
|
||||
SendArea->installEventFilter(this);
|
||||
|
||||
//按钮
|
||||
OpenButton = new QPushButton(tr("打开串口"));
|
||||
SendButton = new QPushButton(tr("发送"));
|
||||
SendButton->setDisabled(true);
|
||||
SendButton->setToolTip(tr("Ctrl+Enter"));
|
||||
ClearButton = new QPushButton(tr("清除接收"));
|
||||
|
||||
connect(OpenButton, QPushButton::clicked, this, OpenSerial);
|
||||
connect(SendButton, QPushButton::clicked, this, SendContent);
|
||||
connect(ClearButton, QPushButton::clicked, this, ClearRecv);
|
||||
|
||||
//复选框
|
||||
NewLineBox = new QCheckBox(tr("发送新行"));
|
||||
TimerBox = new QCheckBox(tr("定时发送"));
|
||||
HexSend = new QCheckBox(tr("发送16进制"));
|
||||
HexRecv = new QCheckBox(tr("接收16进制"));
|
||||
HexSend->setToolTip(tr("以空格作为间隔符, \n非法字符及其后面的字符将被忽略"));
|
||||
|
||||
connect(NewLineBox, QCheckBox::stateChanged, this, detNewLine);
|
||||
connect(TimerBox, QCheckBox::stateChanged, this, ControlSendTimer);
|
||||
connect(HexSend, QCheckBox::stateChanged, this, detHex);
|
||||
connect(HexRecv, QCheckBox::stateChanged, this, detRecvHex);
|
||||
|
||||
//spinbox
|
||||
TimerSpin = new QSpinBox();
|
||||
TimerSpin->setSuffix(tr(" ms"));
|
||||
TimerSpin->setMaximum(1000000);
|
||||
TimerSpin->setValue(1000);
|
||||
|
||||
connect(TimerSpin, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
|
||||
this, changeSendTimer);
|
||||
|
||||
//初始化布局
|
||||
cvlayout = new QVBoxLayout();
|
||||
cvlayout->addWidget(RecvArea, 7);
|
||||
cvlayout->addWidget(SendArea, 2);
|
||||
hlayout = new QHBoxLayout();
|
||||
hlayout->addWidget(HexSend);
|
||||
hlayout->addWidget(NewLineBox, Qt::AlignRight);
|
||||
hlayout->addWidget(TimerBox, Qt::AlignRight);
|
||||
hlayout->addWidget(TimerSpin);
|
||||
hlayout->addWidget(SendButton);
|
||||
rvlayout = new QVBoxLayout();
|
||||
rvlayout->addWidget(ClearButton);
|
||||
rvlayout->addWidget(HexRecv);
|
||||
layout = new QGridLayout(this);
|
||||
layout->addWidget(COMBox, 0, 0, 1, 2);
|
||||
layout->addWidget(BaudrateLabel, 1, 0);
|
||||
layout->addWidget(StopbitsLabel, 2, 0);
|
||||
layout->addWidget(DatabitsLabel, 3, 0);
|
||||
layout->addWidget(ParityLabel, 4, 0);
|
||||
layout->addWidget(BaudrateBox, 1, 1);
|
||||
layout->addWidget(StopbitsBox, 2, 1);
|
||||
layout->addWidget(DatabitsBox, 3, 1);
|
||||
layout->addWidget(ParityBox, 4, 1);
|
||||
layout->addWidget(OpenButton, 5, 1);
|
||||
layout->addLayout(hlayout, 5, 2);
|
||||
// layout->addWidget(SendButton, 5, 2, Qt::AlignRight);
|
||||
layout->addLayout(cvlayout, 0, 2, 5, 1);
|
||||
// layout->addWidget(ClearButton, 0, 3, Qt::AlignLeft);
|
||||
layout->addLayout(rvlayout, 0, 3);
|
||||
layout->setColumnStretch(0, 1);
|
||||
layout->setColumnStretch(1, 1);
|
||||
layout->setColumnStretch(2, 4);
|
||||
layout->setColumnStretch(3, 1);
|
||||
|
||||
setLayout(layout);
|
||||
|
||||
//计时器 初始化
|
||||
CheckTimer = new QTimer(this);
|
||||
CheckTimer->start(1000);
|
||||
SendTimer = new QTimer(this);
|
||||
connect(CheckTimer, QTimer::timeout, this, CheckSerials);
|
||||
connect(SendTimer, QTimer::timeout, SendButton, QPushButton::click);
|
||||
|
||||
//初始化串口列表
|
||||
CheckSerials();
|
||||
|
||||
//开始串口进程
|
||||
serialController = new SerialController;
|
||||
serialController->moveToThread(&SerialThr);
|
||||
SerialThr.start();
|
||||
|
||||
connect(this, requestOpen, serialController, SerialController::openSerial);
|
||||
connect(this, requestClose, serialController, SerialController::closeSerial);
|
||||
connect(serialController, SerialController::openSuccess, this, serialOpened);
|
||||
connect(serialController, SerialController::openFailed, this, serialNotOpened);
|
||||
connect(serialController, SerialController::closeSuccess, this, serialClosed);
|
||||
connect(this, setBaudRate, serialController, SerialController::getBaudrate);
|
||||
connect(this, setStopBits, serialController, SerialController::getStopbits);
|
||||
connect(this, setDataBits, serialController, SerialController::getDatabits);
|
||||
connect(this, setParity, serialController, SerialController::getParity);
|
||||
connect(BaudrateBox, QComboBox::currentTextChanged, serialController, SerialController::getBaudrate);
|
||||
connect(StopbitsBox, QComboBox::currentTextChanged, serialController, SerialController::getStopbits);
|
||||
connect(DatabitsBox, QComboBox::currentTextChanged, serialController, SerialController::getDatabits);
|
||||
connect(ParityBox, QComboBox::currentTextChanged, serialController, SerialController::getParity);
|
||||
connect(this, sendData, serialController, SerialController::writeData);
|
||||
connect(serialController, SerialController::recvData, this, getRecv);
|
||||
}
|
||||
|
||||
void MainWidget::CheckSerials()
|
||||
{
|
||||
emit sendDateTime(QDateTime::currentDateTime().toString());
|
||||
QList<QSerialPortInfo> SerialList = QSerialPortInfo::availablePorts();
|
||||
if(!SerialList.isEmpty())
|
||||
{
|
||||
QStringList TmpComList;
|
||||
for(QSerialPortInfo serial : SerialList)
|
||||
{
|
||||
TmpComList<<serial.portName() + " " + serial.description();
|
||||
}
|
||||
if(COMList != TmpComList)
|
||||
{
|
||||
//只在串口发生变化时刷新ComboBox
|
||||
COMList = TmpComList;
|
||||
COMBox->setDisabled(false);
|
||||
COMBox->clear();
|
||||
COMBox->addItems(COMList);
|
||||
OpenButton->setDisabled(false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
COMBox->clear();
|
||||
COMList.clear();
|
||||
COMBox->addItem(tr("(空)"));
|
||||
COMBox->setDisabled(true);
|
||||
OpenButton->setDisabled(true);
|
||||
emit CloseSerial();
|
||||
}
|
||||
}
|
||||
|
||||
MainWidget::~MainWidget()
|
||||
{
|
||||
SerialThr.terminate();
|
||||
}
|
||||
|
||||
void MainWidget::serialOpened()
|
||||
{
|
||||
OpenButton->setText(tr("关闭串口"));
|
||||
emit sendStatus(QString(/*tr("串口已打开") + */COMBox->currentText()));
|
||||
COMBox->setDisabled(true);
|
||||
SendButton->setDisabled(false);
|
||||
disconnect(OpenButton, QPushButton::clicked, this, OpenSerial);
|
||||
connect(OpenButton, QPushButton::clicked, this, CloseSerial);
|
||||
}
|
||||
|
||||
void MainWidget::serialNotOpened()
|
||||
{
|
||||
emit sendStatus(tr("串口打开失败"));
|
||||
}
|
||||
|
||||
void MainWidget::serialClosed()
|
||||
{
|
||||
OpenButton->setText(tr("打开串口"));
|
||||
COMBox->setDisabled(false);
|
||||
SendButton->setDisabled(true);
|
||||
disconnect(OpenButton, QPushButton::clicked, this, CloseSerial);
|
||||
connect(OpenButton, QPushButton::clicked, this, OpenSerial);
|
||||
emit sendStatus(tr("串口已关闭"));
|
||||
}
|
||||
|
||||
void MainWidget::getRecv(QByteArray recv)
|
||||
{
|
||||
RecvArea->moveCursor(QTextCursor::End);
|
||||
if(!isRecvHex)
|
||||
RecvArea->textCursor().insertText(QString::fromLocal8Bit(recv));
|
||||
else
|
||||
{
|
||||
RecvArea->textCursor().insertText(QString(recv.toHex())+tr(" "));
|
||||
}
|
||||
}
|
||||
|
||||
void MainWidget::OpenSerial()
|
||||
{
|
||||
QString portName = COMBox->currentText().split(' ')[0];
|
||||
emit requestOpen(portName);
|
||||
emit setBaudRate(BaudrateBox->currentText());
|
||||
emit setStopBits(StopbitsBox->currentText());
|
||||
emit setDataBits(DatabitsBox->currentText());
|
||||
emit setParity(ParityBox->currentText());
|
||||
}
|
||||
|
||||
void MainWidget::CloseSerial()
|
||||
{
|
||||
emit requestClose();
|
||||
}
|
||||
|
||||
void MainWidget::ClearRecv()
|
||||
{
|
||||
RecvArea->clear();
|
||||
}
|
||||
|
||||
void MainWidget::detNewLine(int state)
|
||||
{
|
||||
if(state==2)
|
||||
isSendNewLine = true;
|
||||
else if(state == 0)
|
||||
isSendNewLine = false;
|
||||
}
|
||||
|
||||
void MainWidget::ControlSendTimer(int state)
|
||||
{
|
||||
if(state == 0)
|
||||
SendTimer->stop();
|
||||
else if(state == 2)
|
||||
SendTimer->start(TimerSpin->value());
|
||||
}
|
||||
|
||||
void MainWidget::changeSendTimer()
|
||||
{
|
||||
ControlSendTimer(TimerBox->checkState());
|
||||
}
|
||||
|
||||
void MainWidget::detHex(int state)
|
||||
{
|
||||
if(state == 2)
|
||||
{
|
||||
isSendHex = true;
|
||||
SendArea->setText(SendArea->toPlainText().toLocal8Bit().toHex(' '));
|
||||
}
|
||||
else if(state == 0)
|
||||
{
|
||||
isSendHex = false;
|
||||
QString tmpstr = SendArea->toPlainText();
|
||||
SendArea->clear();
|
||||
SendArea->setText(HexStringToString(tmpstr));
|
||||
}
|
||||
}
|
||||
|
||||
void MainWidget::detRecvHex(int state)
|
||||
{
|
||||
if(state == 2)
|
||||
{
|
||||
isRecvHex = true;
|
||||
RecvArea->setText(RecvArea->toPlainText().toLocal8Bit().toHex(' '));
|
||||
RecvArea->moveCursor(QTextCursor::End);
|
||||
RecvArea->insertPlainText(tr(" "));
|
||||
}
|
||||
else if(state == 0)
|
||||
{
|
||||
isRecvHex = false;
|
||||
QString tmpstr = RecvArea->toPlainText();
|
||||
RecvArea->clear();
|
||||
RecvArea->setText(HexStringToString(tmpstr));
|
||||
}
|
||||
}
|
||||
|
||||
QString MainWidget::HexStringToString(QString hexstr)
|
||||
{
|
||||
QStringList list = hexstr.split(' ');
|
||||
QByteArray bit;
|
||||
foreach (QString str, list)
|
||||
{
|
||||
if(!str.isEmpty())
|
||||
bit.append(str.toInt(nullptr, 16));
|
||||
}
|
||||
return QString::fromLocal8Bit(bit);
|
||||
}
|
||||
|
||||
void MainWidget::SendContent()
|
||||
{
|
||||
QString content = SendArea->toPlainText();
|
||||
if(isSendHex)
|
||||
content = HexStringToString(content);
|
||||
if(isSendNewLine)
|
||||
content += "\r\n";
|
||||
emit sendData(content);
|
||||
}
|
||||
|
||||
bool MainWidget::eventFilter(QObject *watched, QEvent *event)
|
||||
{
|
||||
if(watched==SendArea&&event->type()==QEvent::KeyPress)
|
||||
{
|
||||
QKeyEvent *e = (QKeyEvent*)event;
|
||||
if(e->modifiers()==Qt::ControlModifier)
|
||||
{
|
||||
switch (e->key())
|
||||
{
|
||||
case Qt::Key_Enter:
|
||||
case Qt::Key_Return:
|
||||
SendButton->click();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return QWidget::eventFilter(watched, event);
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
#ifndef MAINWIDGET_H
|
||||
#define MAINWIDGET_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QComboBox>
|
||||
#include <QGridLayout>
|
||||
#include <QVBoxLayout>
|
||||
#include <QHBoxLayout>
|
||||
#include <QTextEdit>
|
||||
#include <QPlainTextEdit>
|
||||
#include <QPushButton>
|
||||
#include <QTimer>
|
||||
#include <QSerialPortInfo>
|
||||
#include <QLabel>
|
||||
#include <QCheckBox>
|
||||
#include <QSpinBox>
|
||||
#include <QThread>
|
||||
#include <QDateTime>
|
||||
#include <QByteArray>
|
||||
#include "serialcontroller.h"
|
||||
|
||||
|
||||
|
||||
class MainWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
MainWidget(QWidget *parent = 0);
|
||||
void CheckSerials();
|
||||
~MainWidget();
|
||||
void SendContent();
|
||||
bool eventFilter(QObject *watched, QEvent *event);
|
||||
|
||||
signals:
|
||||
void requestOpen(QString portName);
|
||||
void requestClose();
|
||||
void setBaudRate(QString baudrate);
|
||||
void setStopBits(QString stopbits);
|
||||
void setDataBits(QString databits);
|
||||
void setParity(QString parity);
|
||||
void sendData(QString content);
|
||||
void sendStatus(QString status); //更新statusbar
|
||||
void sendDateTime(QString datetime); //更新stastatusbar中的时间
|
||||
|
||||
public slots:
|
||||
void serialOpened(); //串口打开成功
|
||||
void serialNotOpened(); //串口打开失败
|
||||
void serialClosed(); //串口关闭
|
||||
void getRecv(QByteArray recv); //串口接受数据
|
||||
void OpenSerial();
|
||||
void CloseSerial();
|
||||
void ClearRecv();
|
||||
void detNewLine(int state); //处理发送新行
|
||||
void ControlSendTimer(int state);
|
||||
void changeSendTimer();
|
||||
void detHex(int state); //处理16进制发送
|
||||
void detRecvHex(int state); //处理16进制接收
|
||||
|
||||
private:
|
||||
QComboBox *COMBox, *BaudrateBox, *StopbitsBox, *DatabitsBox, *ParityBox;
|
||||
QStringList COMList, BaudrateList, StopbitsList, DatabitsList, ParityList;
|
||||
QLabel *BaudrateLabel, *StopbitsLabel, *DatabitsLabel, *ParityLabel;
|
||||
QGridLayout *layout;
|
||||
QVBoxLayout *cvlayout, *rvlayout;
|
||||
QHBoxLayout *hlayout;
|
||||
QTextEdit *RecvArea, *SendArea;
|
||||
QPushButton *OpenButton, *SendButton, *ClearButton;
|
||||
QCheckBox *NewLineBox, *TimerBox, *HexSend, *HexRecv;
|
||||
QSpinBox *TimerSpin;
|
||||
QTimer *CheckTimer, *SendTimer;
|
||||
SerialController *serialController;
|
||||
QThread SerialThr;
|
||||
|
||||
bool isSendNewLine = false;
|
||||
bool isSendHex = false;
|
||||
bool isRecvHex = false;
|
||||
|
||||
QString HexStringToString(QString hexstr); //解码16进制字符串
|
||||
|
||||
};
|
||||
|
||||
#endif // MAINWIDGET_H
|
||||
+16
-160
@@ -1,171 +1,27 @@
|
||||
#include "mainwindow.h"
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
|
||||
widget(new MainWidget(this))
|
||||
{
|
||||
//初始化各参数列表
|
||||
BaudrateList<<"256000"<<"230400"<<"128000"<<"115200"<<"76800"<<"57600"
|
||||
<<"43000"<<"38400"<<"19200"<<"14400"<<"9600"<<"4800"<<"2400"<<"1200";
|
||||
StopbitsList<<"1"<<"1.5"<<"2";
|
||||
DatabitsList<<"8"<<"7"<<"6"<<"5";
|
||||
ParityList<<tr("无")<<tr("奇校验")<<tr("偶校验");
|
||||
statusbar = new QStatusBar(this);
|
||||
setStatusBar(statusbar);
|
||||
setCentralWidget(widget);
|
||||
statusbar->showMessage(tr("准备就绪"));
|
||||
connect(widget, MainWidget::sendStatus, this, setNewMsg);
|
||||
timeLabel = new QLabel(statusbar);
|
||||
statusBar()->addPermanentWidget(timeLabel);
|
||||
timeLabel->setText(QDateTime::currentDateTime().toString());
|
||||
|
||||
//初始化ComboBox
|
||||
COMBox = new QComboBox();
|
||||
BaudrateBox = new QComboBox();
|
||||
BaudrateBox->addItems(BaudrateList);
|
||||
StopbitsBox = new QComboBox();
|
||||
StopbitsBox->addItems(StopbitsList);
|
||||
DatabitsBox = new QComboBox();
|
||||
DatabitsBox->addItems(DatabitsList);
|
||||
ParityBox = new QComboBox();
|
||||
ParityBox->addItems(ParityList);
|
||||
|
||||
//初始化label
|
||||
BaudrateLabel = new QLabel(tr("波特率"));
|
||||
StopbitsLabel = new QLabel(tr("停止位"));
|
||||
DatabitsLabel = new QLabel(tr("数据位"));
|
||||
ParityLabel = new QLabel(tr("校验位"));
|
||||
|
||||
//发送与接受区域
|
||||
RecvArea = new QTextEdit();
|
||||
RecvArea->setReadOnly(true);
|
||||
//RecvArea->setText(tr("Hello"));
|
||||
SendArea = new QTextEdit();
|
||||
|
||||
//按钮
|
||||
OpenButton = new QPushButton(tr("打开串口"));
|
||||
SendButton = new QPushButton(tr("发送"));
|
||||
|
||||
connect(OpenButton, QPushButton::clicked, this, OpenSerial);
|
||||
|
||||
//初始化布局
|
||||
layout = new QGridLayout(this);
|
||||
layout->addWidget(BaudrateLabel, 1, 0);
|
||||
layout->addWidget(StopbitsLabel, 2, 0);
|
||||
layout->addWidget(DatabitsLabel, 3, 0);
|
||||
layout->addWidget(ParityLabel, 4, 0);
|
||||
layout->addWidget(COMBox, 0, 0, 1, 2);
|
||||
layout->addWidget(BaudrateBox, 1,1);
|
||||
layout->addWidget(StopbitsBox, 2, 1);
|
||||
layout->addWidget(DatabitsBox, 3, 1);
|
||||
layout->addWidget(ParityBox, 4, 1);
|
||||
layout->addWidget(OpenButton, 5, 1);
|
||||
layout->addWidget(SendButton, 5, 2);
|
||||
layout->addWidget(RecvArea, 0, 2, 3, 1);
|
||||
layout->addWidget(SendArea, 3, 2, 2, 1);
|
||||
setLayout(layout);
|
||||
|
||||
//计时器 初始化
|
||||
CheckTimer = new QTimer(this);
|
||||
connect(CheckTimer, QTimer::timeout, this, CheckSerials);
|
||||
CheckTimer->start(1000);
|
||||
|
||||
resize(800, 600);
|
||||
|
||||
//初始化串口列表
|
||||
CheckSerials();
|
||||
|
||||
//开始串口进程
|
||||
serialController = new SerialController;
|
||||
serialController->moveToThread(&SerialThr);
|
||||
SerialThr.start();
|
||||
|
||||
connect(this, requestOpen, serialController, SerialController::openSerial);
|
||||
connect(this, requestClose, serialController, SerialController::closeSerial);
|
||||
connect(serialController, SerialController::openSuccess, this, serialOpened);
|
||||
connect(serialController, SerialController::openFailed, this, serialNotOpened);
|
||||
connect(serialController, SerialController::closeSuccess, this, serialClosed);
|
||||
connect(this, setBaudRate, serialController, SerialController::getBaudrate);
|
||||
connect(this, setStopBits, serialController, SerialController::getStopbits);
|
||||
connect(this, setDataBits, serialController, SerialController::getDatabits);
|
||||
connect(this, setParity, serialController, SerialController::getParity);
|
||||
connect(BaudrateBox, QComboBox::currentTextChanged, serialController, SerialController::getBaudrate);
|
||||
connect(StopbitsBox, QComboBox::currentTextChanged, serialController, SerialController::getStopbits);
|
||||
connect(DatabitsBox, QComboBox::currentTextChanged, serialController, SerialController::getDatabits);
|
||||
connect(ParityBox, QComboBox::currentTextChanged, serialController, SerialController::getParity);
|
||||
connect(this, sendData, serialController, SerialController::writeData);
|
||||
connect(SendButton, QPushButton::clicked, this, SendContent);
|
||||
connect(serialController, SerialController::recvData, this, getRecv);
|
||||
connect(widget, MainWidget::sendDateTime, this, setDateTime);
|
||||
resize(900, 600);
|
||||
}
|
||||
|
||||
void MainWindow::CheckSerials()
|
||||
void MainWindow::setNewMsg(QString msg)
|
||||
{
|
||||
QList<QSerialPortInfo> SerialList = QSerialPortInfo::availablePorts();
|
||||
if(!SerialList.isEmpty())
|
||||
{
|
||||
QStringList TmpComList;
|
||||
for(QSerialPortInfo serial : SerialList)
|
||||
{
|
||||
TmpComList<<serial.portName() + " " + serial.description();
|
||||
}
|
||||
if(COMList != TmpComList)
|
||||
{
|
||||
//只在串口发生变化时刷新ComboBox
|
||||
COMList = TmpComList;
|
||||
COMBox->setDisabled(false);
|
||||
COMBox->clear();
|
||||
COMBox->addItems(COMList);
|
||||
OpenButton->setDisabled(false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
COMBox->clear();
|
||||
COMList.clear();
|
||||
COMBox->addItem(tr("(空)"));
|
||||
COMBox->setDisabled(true);
|
||||
OpenButton->setDisabled(true);
|
||||
}
|
||||
statusBar()->showMessage(msg);
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
void MainWindow::setDateTime(QString datetime)
|
||||
{
|
||||
SerialThr.terminate();
|
||||
}
|
||||
|
||||
void MainWindow::serialOpened()
|
||||
{
|
||||
OpenButton->setText(tr("关闭串口"));
|
||||
disconnect(OpenButton, QPushButton::clicked, this, OpenSerial);
|
||||
connect(OpenButton, QPushButton::clicked, this, CloseSerial);
|
||||
}
|
||||
|
||||
void MainWindow::serialNotOpened()
|
||||
{
|
||||
qDebug()<<"打开失败";
|
||||
}
|
||||
|
||||
void MainWindow::serialClosed()
|
||||
{
|
||||
OpenButton->setText(tr("打开串口"));
|
||||
disconnect(OpenButton, QPushButton::clicked, this, CloseSerial);
|
||||
connect(OpenButton, QPushButton::clicked, this, OpenSerial);
|
||||
}
|
||||
|
||||
void MainWindow::getRecv(QByteArray recv)
|
||||
{
|
||||
RecvArea->setText(RecvArea->toPlainText()+QString(recv));
|
||||
}
|
||||
|
||||
void MainWindow::OpenSerial()
|
||||
{
|
||||
QString portName = COMBox->currentText().split(' ')[0];
|
||||
qDebug()<<portName;
|
||||
emit requestOpen(portName);
|
||||
emit setBaudRate(BaudrateBox->currentText());
|
||||
emit setStopBits(StopbitsBox->currentText());
|
||||
emit setDataBits(DatabitsBox->currentText());
|
||||
emit setParity(ParityBox->currentText());
|
||||
}
|
||||
|
||||
void MainWindow::CloseSerial()
|
||||
{
|
||||
emit requestClose();
|
||||
}
|
||||
|
||||
void MainWindow::SendContent()
|
||||
{
|
||||
QString content = SendArea->toPlainText();
|
||||
emit sendData(content);
|
||||
timeLabel->setText(datetime);
|
||||
}
|
||||
+11
-43
@@ -1,59 +1,27 @@
|
||||
#ifndef MAINWINDOW_H
|
||||
#define MAINWINDOW_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QComboBox>
|
||||
#include <QGridLayout>
|
||||
#include <QTextEdit>
|
||||
#include <QPlainTextEdit>
|
||||
#include <QPushButton>
|
||||
#include <QTimer>
|
||||
#include <QSerialPortInfo>
|
||||
#include <QLabel>
|
||||
#include <QThread>
|
||||
#include "serialcontroller.h"
|
||||
#include <QMainWindow>
|
||||
#include <QStatusBar>
|
||||
#include <QKeyEvent>
|
||||
#include <mainwidget.h>
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
|
||||
class MainWindow : public QWidget
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
MainWindow(QWidget *parent = 0);
|
||||
void CheckSerials();
|
||||
~MainWindow();
|
||||
explicit MainWindow(QWidget *parent = nullptr);
|
||||
|
||||
signals:
|
||||
void requestOpen(QString portName);
|
||||
void requestClose();
|
||||
void setBaudRate(QString baudrate);
|
||||
void setStopBits(QString stopbits);
|
||||
void setDataBits(QString databits);
|
||||
void setParity(QString parity);
|
||||
void sendData(QString content);
|
||||
|
||||
public slots:
|
||||
void serialOpened(); //串口打开成功
|
||||
void serialNotOpened(); //串口打开失败
|
||||
void serialClosed(); //串口关闭
|
||||
void getRecv(QByteArray recv); //串口接受数据
|
||||
void setNewMsg(QString msg);
|
||||
void setDateTime(QString datetime);
|
||||
|
||||
private:
|
||||
QComboBox *COMBox, *BaudrateBox, *StopbitsBox, *DatabitsBox, *ParityBox;
|
||||
QStringList COMList, BaudrateList, StopbitsList, DatabitsList, ParityList;
|
||||
QLabel *BaudrateLabel, *StopbitsLabel, *DatabitsLabel, *ParityLabel;
|
||||
QGridLayout *layout;
|
||||
QTextEdit *RecvArea, *SendArea;
|
||||
QPushButton *OpenButton, *SendButton;
|
||||
QTimer *CheckTimer;
|
||||
SerialController *serialController;
|
||||
QThread SerialThr;
|
||||
|
||||
void OpenSerial();
|
||||
void CloseSerial();
|
||||
void SendContent();
|
||||
MainWidget *widget;
|
||||
QStatusBar *statusbar;
|
||||
QLabel *timeLabel;
|
||||
};
|
||||
|
||||
#endif // MAINWINDOW_H
|
||||
@@ -0,0 +1,5 @@
|
||||
<RCC>
|
||||
<qresource prefix="/pic">
|
||||
<file>icon/icon.png</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
@@ -0,0 +1 @@
|
||||
IDI_ICON1 ICON DISCARDABLE "icon.ico"
|
||||
@@ -34,7 +34,6 @@ void SerialController::closeSerial()
|
||||
void SerialController::getBaudrate(QString baudrate)
|
||||
{
|
||||
serial->setBaudRate(baudrate.toInt());
|
||||
qDebug()<<"The baudrate now is "<<baudrate.toInt();
|
||||
|
||||
}
|
||||
|
||||
@@ -46,13 +45,11 @@ void SerialController::getStopbits(QString stopbits)
|
||||
serial->setStopBits(QSerialPort::OneAndHalfStop);
|
||||
else if(stopbits == "2")
|
||||
serial->setStopBits(QSerialPort::TwoStop);
|
||||
qDebug()<<"The stopbits now is "<<stopbits;
|
||||
}
|
||||
|
||||
void SerialController::getDatabits(QString databits)
|
||||
{
|
||||
serial->setDataBits(QSerialPort::DataBits(databits.toInt()));
|
||||
qDebug()<<"The databits now is "<<databits;
|
||||
}
|
||||
|
||||
void SerialController::getParity(QString parity)
|
||||
@@ -65,14 +62,13 @@ void SerialController::getParity(QString parity)
|
||||
|
||||
else if(parity == tr("偶校验"))
|
||||
serial->setParity(QSerialPort::EvenParity);
|
||||
qDebug()<<"The parity now is "<<parity;
|
||||
}
|
||||
|
||||
void SerialController::writeData(QString content)
|
||||
{
|
||||
if(serial->isWritable())
|
||||
{
|
||||
qint64 result = serial->write(content.toLatin1().data());
|
||||
qint64 result = serial->write(content.toLocal8Bit().data());
|
||||
if(result == -1)
|
||||
emit writeFailed();
|
||||
else
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
#include <QObject>
|
||||
#include <QSerialPort>
|
||||
#include <QDebug>
|
||||
|
||||
class SerialController : public QObject
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user