7 Commits
Author SHA1 Message Date
才明洋 97a30114d3 关于 2018-02-10 21:30:49 +08:00
才明洋 fc79c2f327 取消图标 2018-02-10 20:04:01 +08:00
才明洋 b690aef900 添加图标 2018-02-10 19:52:36 +08:00
才明洋 0a1d127390 打开串口失败时声音提示 2018-02-10 19:41:20 +08:00
才明洋 1e2fad8cbe 增加RTS DTR控制功能 2018-02-10 18:41:06 +08:00
才明洋 7ff5fc91a6 改进代码 2018-02-08 11:40:59 +08:00
才明洋 9044a30873 改变串口信息显示方法 调整ui 2018-02-08 11:34:50 +08:00
16 changed files with 322 additions and 158 deletions

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
{
"CurrentProjectSetting": "x86-Debug"
}
Binary file not shown.
Binary file not shown.
+7
View File
@@ -0,0 +1,7 @@
{
"ExpandedNodes": [
""
],
"SelectedNode": "\\serialcontroller.cpp",
"PreviewInSolutionExplorer": false
}
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 750 B

+1
View File
@@ -4,6 +4,7 @@
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setAttribute(Qt::AA_UseHighDpiPixmaps);
a.setWindowIcon(QIcon(QString(":/pic/icon/icon.png")));
MainWindow w;
w.show();
+165 -72
View File
@@ -4,11 +4,28 @@ MainWidget::MainWidget(QWidget *parent)
: QWidget(parent), settings("TurnMeOn", "SerialAsst")
{
//初始化各参数列表
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("偶校验");
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();
@@ -26,6 +43,7 @@ MainWidget::MainWidget(QWidget *parent)
ParityBox->setCurrentIndex(settings.value("Parity", 0).toInt());
//初始化label
COMLabel = new QLabel(tr("串口号"));
BaudrateLabel = new QLabel(tr("波特率"));
StopbitsLabel = new QLabel(tr("停止位"));
DatabitsLabel = new QLabel(tr("数据位"));
@@ -48,11 +66,13 @@ MainWidget::MainWidget(QWidget *parent)
SendButton = new QPushButton(tr("发送"));
SendButton->setDisabled(true);
SendButton->setToolTip(tr("Ctrl+Enter"));
ClearButton = new QPushButton(tr("清除接收"));
ClearRecvButton = new QPushButton(tr("清除接收"));
ClearSendButton = new QPushButton(tr("清除发送"));
connect(OpenButton, QPushButton::clicked, this, OpenSerial);
connect(SendButton, QPushButton::clicked, this, SendContent);
connect(ClearButton, QPushButton::clicked, this, ClearRecv);
connect(ClearRecvButton, QPushButton::clicked, this, ClearRecv);
connect(ClearSendButton, QPushButton::clicked, this, ClearSend);
//复选框
NewLineBox = new QCheckBox(tr("发送新行"));
@@ -60,11 +80,15 @@ MainWidget::MainWidget(QWidget *parent)
HexSend = new QCheckBox(tr("发送16进制"));
HexRecv = new QCheckBox(tr("接收16进制"));
HexSend->setToolTip(tr("以空格作为间隔符, \n非法字符及其后面的字符将被忽略"));
RTSBox = new QCheckBox(tr("RTS"));
DTRBox = new QCheckBox(tr("DTR"));
connect(NewLineBox, QCheckBox::stateChanged, this, detNewLine);
connect(TimerBox, QCheckBox::stateChanged, this, ControlSendTimer);
connect(HexSend, QCheckBox::stateChanged, this, detHex);
connect(HexRecv, QCheckBox::stateChanged, this, detRecvHex);
connect(RTSBox, QCheckBox::stateChanged, this, RTSControl);
connect(DTRBox, QCheckBox::stateChanged, this, DTRControl);
//spinbox
TimerSpin = new QSpinBox();
@@ -76,40 +100,56 @@ MainWidget::MainWidget(QWidget *parent)
this, changeSendTimer);
//初始化布局
leftLlayout = new QFormLayout();
leftLlayout->addRow(BaudrateLabel, BaudrateBox);
leftLlayout->addRow(StopbitsLabel, StopbitsBox);
leftLlayout->addRow(DatabitsLabel, DatabitsBox);
leftLlayout->addRow(ParityLabel, ParityBox);
leftLlayout->addRow(COMLabel, COMBox);
leftLlayout->addRow(OpenButton);
leftLlayout->setAlignment(OpenButton, Qt::AlignVCenter);
leftLlayout->setMargin(30);
leftLlayout->setHorizontalSpacing(20);
leftLlayout->setVerticalSpacing(60);
paramGroup = new QGroupBox();
paramGroup->setLayout(leftLlayout);
paramGroup->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
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);
bottomLayout = new QHBoxLayout();
bottomLayout->addWidget(ClearSendButton, 0, Qt::AlignLeft);
bottomLayout->addWidget(HexSend);
bottomLayout->addWidget(NewLineBox, 0, Qt::AlignLeft);
bottomLayout->addWidget(TimerBox, 0, Qt::AlignRight);
bottomLayout->addWidget(TimerSpin, 1, Qt::AlignLeft);
bottomLayout->addWidget(SendButton, 0, Qt::AlignRight);
rightLayout = new QVBoxLayout();
rightLayout->addWidget(ClearRecvButton, 0, Qt::AlignCenter);
rightLayout->addWidget(HexRecv);
rightLayout->addWidget(RTSBox);
rightLayout->addWidget(DTRBox);
centralLayout = new QGridLayout(this);
// layout->addLayout(flayout, 0, 0, 2, 1);
centralLayout->addWidget(paramGroup, 0, 0, 2, 1);
centralLayout->addLayout(bottomLayout, 1, 1);
centralLayout->addLayout(cvlayout, 0, 1, 1, 1);
centralLayout->addLayout(rightLayout, 0, 2, Qt::AlignTop);
centralLayout->setColumnMinimumWidth(0, 150);
centralLayout->setColumnMinimumWidth(2, 100);
centralLayout->setColumnStretch(0, 0);
centralLayout->setColumnStretch(1, 2);
centralLayout->setColumnStretch(2, 0);
setLayout(centralLayout);
//计时器 初始化
CheckTimer = new QTimer(this);
@@ -139,30 +179,37 @@ MainWidget::MainWidget(QWidget *parent)
connect(StopbitsBox, QComboBox::currentTextChanged, serialController, SerialController::getStopbits);
connect(DatabitsBox, QComboBox::currentTextChanged, serialController, SerialController::getDatabits);
connect(ParityBox, QComboBox::currentTextChanged, serialController, SerialController::getParity);
connect(this, changeRTS, serialController, SerialController::contrloRTS);
connect(this, changeDTR, serialController, SerialController::controlDTR);
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())
if (!SerialList.isEmpty())
{
QStringList TmpComList;
for(QSerialPortInfo serial : SerialList)
QStringList TmpComList, TmpPortNameList, TmpDesList;
for (QSerialPortInfo serial : SerialList)
{
TmpComList<<serial.portName() + " " + serial.description();
TmpComList << serial.portName() + " " + serial.description();
TmpPortNameList << serial.portName();
TmpDesList << serial.description();
}
if(COMList != TmpComList)
if (COMList != TmpComList)
{
//只在串口发生变化时刷新ComboBox
COMList = TmpComList;
PortNameList = TmpPortNameList;
DescList = TmpDesList;
COMBox->setDisabled(false);
COMBox->clear();
COMBox->addItems(COMList);
COMBox->addItems(PortNameList);
OpenButton->setDisabled(false);
for (int i = 0; i < COMList.count(); i++)
COMBox->setItemData(i, DescList[i], Qt::ToolTipRole);
}
}
else
@@ -173,6 +220,7 @@ void MainWidget::CheckSerials()
COMBox->setDisabled(true);
OpenButton->setDisabled(true);
emit CloseSerial();
isOpened = false;
}
}
@@ -187,43 +235,41 @@ MainWidget::~MainWidget()
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);
isOpened = true;
int portIndex = COMBox->currentIndex();
emit sendStatus(QString(PortNameList[portIndex] + tr(" ") + DescList[portIndex]));
emit RTSBox->stateChanged(RTSBox->checkState());
emit DTRBox->stateChanged(DTRBox->checkState());
ACtionAttachToSerial(true);
}
void MainWidget::serialNotOpened()
{
emit sendStatus(tr("串口打开失败"));
QApplication::beep();
}
void MainWidget::serialClosed()
{
OpenButton->setText(tr("打开串口"));
COMBox->setDisabled(false);
SendButton->setDisabled(true);
disconnect(OpenButton, QPushButton::clicked, this, CloseSerial);
connect(OpenButton, QPushButton::clicked, this, OpenSerial);
isOpened = false;
ACtionAttachToSerial(false);
emit sendStatus(tr("串口已关闭"));
}
void MainWidget::getRecv(QByteArray recv)
{
RecvArea->moveCursor(QTextCursor::End);
if(!isRecvHex)
if (!isRecvHex)
RecvArea->textCursor().insertText(QString::fromLocal8Bit(recv));
else
{
RecvArea->textCursor().insertText(QString(recv.toHex())+tr(" "));
RecvArea->textCursor().insertText(QString(recv.toHex()) + tr(" "));
}
}
void MainWidget::OpenSerial()
{
QString portName = COMBox->currentText().split(' ')[0];
QString portName = COMBox->currentText();
emit requestOpen(portName);
emit setBaudRate(BaudrateBox->currentText());
emit setStopBits(StopbitsBox->currentText());
@@ -241,19 +287,24 @@ void MainWidget::ClearRecv()
RecvArea->clear();
}
void MainWidget::ClearSend()
{
SendArea->clear();
}
void MainWidget::detNewLine(int state)
{
if(state==2)
if (state == 2)
isSendNewLine = true;
else if(state == 0)
else if (state == 0)
isSendNewLine = false;
}
void MainWidget::ControlSendTimer(int state)
{
if(state == 0)
if (state == 0)
SendTimer->stop();
else if(state == 2)
else if (state == 2)
SendTimer->start(TimerSpin->value());
}
@@ -264,12 +315,12 @@ void MainWidget::changeSendTimer()
void MainWidget::detHex(int state)
{
if(state == 2)
if (state == 2)
{
isSendHex = true;
SendArea->setText(SendArea->toPlainText().toLocal8Bit().toHex(' '));
}
else if(state == 0)
else if (state == 0)
{
isSendHex = false;
QString tmpstr = SendArea->toPlainText();
@@ -280,14 +331,14 @@ void MainWidget::detHex(int state)
void MainWidget::detRecvHex(int state)
{
if(state == 2)
if (state == 2)
{
isRecvHex = true;
RecvArea->setText(RecvArea->toPlainText().toLocal8Bit().toHex(' '));
RecvArea->moveCursor(QTextCursor::End);
RecvArea->insertPlainText(tr(" "));
}
else if(state == 0)
else if (state == 0)
{
isRecvHex = false;
QString tmpstr = RecvArea->toPlainText();
@@ -296,34 +347,76 @@ void MainWidget::detRecvHex(int state)
}
}
void MainWidget::RTSControl(int state)
{
if(isOpened)
{
if(state == 2)
emit changeRTS(true);
else if(state == 0)
emit changeRTS(false);
}
}
void MainWidget::DTRControl(int state)
{
if(isOpened)
{
if(state == 2)
emit changeDTR(true);
else if(state == 0)
emit changeDTR(false);
}
}
QString MainWidget::HexStringToString(QString hexstr)
{
QStringList list = hexstr.split(' ');
QByteArray bit;
foreach (QString str, list)
{
if(!str.isEmpty())
if (!str.isEmpty())
bit.append(str.toInt(nullptr, 16));
}
return QString::fromLocal8Bit(bit);
}
void MainWidget::ACtionAttachToSerial(bool set)
{
if(set)
{
OpenButton->setText(tr("关闭串口"));
COMBox->setDisabled(true);
SendButton->setDisabled(false);
disconnect(OpenButton, QPushButton::clicked, this, OpenSerial);
connect(OpenButton, QPushButton::clicked, this, CloseSerial);
}
else
{
OpenButton->setText(tr("打开串口"));
COMBox->setDisabled(false);
SendButton->setDisabled(true);
disconnect(OpenButton, QPushButton::clicked, this, CloseSerial);
connect(OpenButton, QPushButton::clicked, this, OpenSerial);
}
}
void MainWidget::SendContent()
{
QString content = SendArea->toPlainText();
if(isSendHex)
if (isSendHex)
content = HexStringToString(content);
if(isSendNewLine)
if (isSendNewLine)
content += "\r\n";
emit sendData(content);
}
bool MainWidget::eventFilter(QObject *watched, QEvent *event)
{
if(watched==SendArea&&event->type()==QEvent::KeyPress)
if (watched == SendArea && event->type() == QEvent::KeyPress)
{
QKeyEvent *e = (QKeyEvent*)event;
if(e->modifiers()==Qt::ControlModifier)
QKeyEvent *e = (QKeyEvent *)event;
if (e->modifiers() == Qt::ControlModifier)
{
switch (e->key())
{
+58 -48
View File
@@ -4,6 +4,7 @@
#include <QWidget>
#include <QComboBox>
#include <QGridLayout>
#include <QApplication>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QTextEdit>
@@ -18,68 +19,77 @@
#include <QDateTime>
#include <QByteArray>
#include <QSettings>
#include <QFormLayout>
#include <QGroupBox>
#include "serialcontroller.h"
class MainWidget : public QWidget
{
Q_OBJECT
Q_OBJECT
public:
MainWidget(QWidget *parent = 0);
void CheckSerials();
~MainWidget();
void SendContent();
bool eventFilter(QObject *watched, QEvent *event);
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中的时间
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中的时间
void changeRTS(bool set);
void changeDTR(bool set);
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进制接收
void serialOpened(); //串口打开成功
void serialNotOpened(); //串口打开失败
void serialClosed(); //串口关闭
void getRecv(QByteArray recv); //串口接受数据
void OpenSerial();
void CloseSerial();
void ClearRecv();
void ClearSend();
void detNewLine(int state); //处理发送新行
void ControlSendTimer(int state);
void changeSendTimer();
void detHex(int state); //处理16进制发送
void detRecvHex(int state); //处理16进制接收
void RTSControl(int state);
void DTRControl(int state);
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;
QSettings settings;
QComboBox *COMBox, *BaudrateBox, *StopbitsBox, *DatabitsBox, *ParityBox;
QStringList BaudrateList, StopbitsList, DatabitsList, ParityList;
QStringList COMList, PortNameList, DescList;
QLabel *COMLabel, *BaudrateLabel, *StopbitsLabel, *DatabitsLabel, *ParityLabel;
QGridLayout *centralLayout;
QVBoxLayout *cvlayout, *rightLayout;
QHBoxLayout *bottomLayout;
QFormLayout *leftLlayout;
QGroupBox *paramGroup;
QTextEdit *RecvArea, *SendArea;
QPushButton *OpenButton, *SendButton, *ClearRecvButton, *ClearSendButton;
QCheckBox *NewLineBox, *TimerBox, *HexSend, *HexRecv, *RTSBox, *DTRBox;
QSpinBox *TimerSpin;
QTimer *CheckTimer, *SendTimer;
SerialController *serialController;
QThread SerialThr;
QSettings settings;
bool isSendNewLine = false;
bool isSendHex = false;
bool isRecvHex = false;
QString HexStringToString(QString hexstr); //解码16进制字符串
bool isSendNewLine = false;
bool isSendHex = false;
bool isRecvHex = false;
bool isOpened = false;
QString HexStringToString(QString hexstr); //解码16进制字符串
void ACtionAttachToSerial(bool set);
};
#endif // MAINWIDGET_H
+31 -2
View File
@@ -1,10 +1,18 @@
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
widget(new MainWidget(this))
widget(new MainWidget(this)),
menuBar(new QMenuBar(this)),
statusbar(new QStatusBar(this))
{
statusbar = new QStatusBar(this);
setMenuBar(menuBar);
setStatusBar(statusbar);
aboutQtAct = new QAction(/*QIcon(":/pic/icon/qt-icon.png"), */tr("&关于Qt"), this);
aboutAct = new QAction(/*QIcon(":/pic/icon/about.png"), */tr("&关于SerialAsst"), this);
helpMenu = menuBar->addMenu(tr("帮助(&H)"));
helpMenu->addAction(aboutQtAct);
helpMenu->addAction(aboutAct);
setCentralWidget(widget);
statusbar->showMessage(tr("准备就绪"));
connect(widget, MainWidget::sendStatus, this, setNewMsg);
@@ -12,6 +20,8 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
statusBar()->addPermanentWidget(timeLabel);
timeLabel->setText(QDateTime::currentDateTime().toString());
connect(aboutQtAct, &QAction::triggered, this, &showAboutQt);
connect(aboutAct, &QAction::triggered, this, showAbout);
connect(widget, MainWidget::sendDateTime, this, setDateTime);
resize(900, 600);
}
@@ -25,3 +35,22 @@ void MainWindow::setDateTime(QString datetime)
{
timeLabel->setText(datetime);
}
void MainWindow::showAboutQt()
{
QMessageBox *aboutQtWindow = new QMessageBox(this);
aboutQtWindow->aboutQt(this);
}
void MainWindow::showAbout()
{
QMessageBox *aboutWindow = new QMessageBox(this);
aboutWindow->setStandardButtons(QMessageBox::Ok);
aboutWindow->setText(tr("<h1>SerialAsst</h1>"
"<p>Based on Qt 5.10.0 (MinGW 5.3.0, 32bit)</p>"
"Source Code: <a href=\"https://github.com/TurnMeOn/SerialAsst\">https://github.com/TurnMeOn/SerialAsst</a><br/>"
"<address>"
"Email: <a href=\"mailto:cmy1113@yeah.net?subject=SerialAsst Feedback\">TurnMeOn</a>"
"</address>"));
aboutWindow->show();
}
+15 -7
View File
@@ -2,26 +2,34 @@
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMenuBar>
#include <QStatusBar>
#include <QKeyEvent>
#include <QMessageBox>
#include <mainwidget.h>
class MainWindow : public QMainWindow
{
Q_OBJECT
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
explicit MainWindow(QWidget *parent = nullptr);
signals:
public slots:
void setNewMsg(QString msg);
void setDateTime(QString datetime);
void setNewMsg(QString msg); //设置状态栏信息
void setDateTime(QString datetime); //设置状态栏时间
void showAboutQt();
void showAbout();
private:
MainWidget *widget;
QStatusBar *statusbar;
QLabel *timeLabel;
MainWidget *widget;
QMenuBar *menuBar;
QMenu *helpMenu;
QAction *aboutQtAct, *aboutAct;
QStatusBar *statusbar;
QLabel *timeLabel;
};
#endif // MAINWINDOW_H
+2
View File
@@ -1,5 +1,7 @@
<RCC>
<qresource prefix="/pic">
<file>icon/icon.png</file>
<file>icon/qt-icon.png</file>
<file>icon/about.png</file>
</qresource>
</RCC>
+21 -11
View File
@@ -9,10 +9,11 @@ SerialController::SerialController(QObject *parent) : QObject(parent)
void SerialController::openSerial(QString name)
{
serial->setPortName(name);
if(serial->open(QIODevice::ReadWrite))
if (serial->open(QIODevice::ReadWrite))
{
//串口打开成功
serial->setDataTerminalReady(false);
serial->setDataTerminalReady(false);
emit openSuccess();
}
else
@@ -24,7 +25,7 @@ void SerialController::openSerial(QString name)
void SerialController::closeSerial()
{
if(serial->isOpen())
if (serial->isOpen())
{
serial->close();
emit closeSuccess();
@@ -34,16 +35,15 @@ void SerialController::closeSerial()
void SerialController::getBaudrate(QString baudrate)
{
serial->setBaudRate(baudrate.toInt());
}
void SerialController::getStopbits(QString stopbits)
{
if(stopbits == "1")
if (stopbits == "1")
serial->setStopBits(QSerialPort::OneStop);
else if(stopbits == "1.5")
else if (stopbits == "1.5")
serial->setStopBits(QSerialPort::OneAndHalfStop);
else if(stopbits == "2")
else if (stopbits == "2")
serial->setStopBits(QSerialPort::TwoStop);
}
@@ -54,22 +54,22 @@ void SerialController::getDatabits(QString databits)
void SerialController::getParity(QString parity)
{
if(parity == tr("无"))
if (parity == tr("无"))
serial->setParity(QSerialPort::NoParity);
else if(parity == tr("奇校验"))
else if (parity == tr("奇校验"))
serial->setParity(QSerialPort::OddParity);
else if(parity == tr("偶校验"))
else if (parity == tr("偶校验"))
serial->setParity(QSerialPort::EvenParity);
}
void SerialController::writeData(QString content)
{
if(serial->isWritable())
if (serial->isWritable())
{
qint64 result = serial->write(content.toLocal8Bit().data());
if(result == -1)
if (result == -1)
emit writeFailed();
else
emit writeSuccess(result);
@@ -81,3 +81,13 @@ void SerialController::handleRecv()
QByteArray data = serial->readAll();
emit recvData(data);
}
void SerialController::contrloRTS(bool set)
{
serial->setRequestToSend(set);
}
void SerialController::controlDTR(bool set)
{
serial->setDataTerminalReady(set);
}
+19 -18
View File
@@ -6,31 +6,32 @@
class SerialController : public QObject
{
Q_OBJECT
Q_OBJECT
public:
explicit SerialController(QObject *parent = nullptr);
explicit SerialController(QObject *parent = nullptr);
signals:
void openSuccess(); //串口打开成功
void openFailed(); //串口打开失败
void closeSuccess(); //串口关闭成功
void writeSuccess(int len); //串口发送成功 len为成功发送的字节
void writeFailed(); //串口发送失败
void recvData(QByteArray content); //发送接收到的数据
void openSuccess(); //串口打开成功
void openFailed(); //串口打开失败
void closeSuccess(); //串口关闭成功
void writeSuccess(int len); //串口发送成功 len为成功发送的字节
void writeFailed(); //串口发送失败
void recvData(QByteArray content); //发送接收到的数据
public slots:
void openSerial(QString name);
void closeSerial();
void getBaudrate(QString baudrate); //改变波特率
void getStopbits(QString stopbits); //改变停止位
void getDatabits(QString databits); //改变数据为
void getParity(QString parity); //改变校验位
void writeData(QString content); //发送数据
void handleRecv(); //接受数据
void openSerial(QString name);
void closeSerial();
void getBaudrate(QString baudrate); //改变波特率
void getStopbits(QString stopbits); //改变停止位
void getDatabits(QString databits); //改变数据为
void getParity(QString parity); //改变校验位
void writeData(QString content); //发送数据
void handleRecv(); //接受数据
void contrloRTS(bool set); //设置RTS位
void controlDTR(bool set); //设置DTR位
private:
QSerialPort *serial;
QSerialPort *serial;
};
#endif // SERIALCONTROLLER_H