增加RTS DTR控制功能
This commit is contained in:
+68
-18
@@ -78,11 +78,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();
|
||||
@@ -102,13 +106,14 @@ MainWidget::MainWidget(QWidget *parent)
|
||||
flayout->addRow(COMLabel, COMBox);
|
||||
flayout->addRow(OpenButton);
|
||||
flayout->setAlignment(OpenButton, Qt::AlignVCenter);
|
||||
// flayout->setSpacing(50);
|
||||
// flayout->setMargin(10);
|
||||
flayout->setVerticalSpacing(50);
|
||||
// flayout->setSpacing(50);
|
||||
flayout->setMargin(30);
|
||||
flayout->setHorizontalSpacing(20);
|
||||
flayout->setVerticalSpacing(60);
|
||||
|
||||
// paramGroup = new QGroupBox();
|
||||
// paramGroup->setLayout(flayout);
|
||||
// paramGroup->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||||
paramGroup = new QGroupBox();
|
||||
paramGroup->setLayout(flayout);
|
||||
paramGroup->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||||
|
||||
cvlayout = new QVBoxLayout();
|
||||
cvlayout->addWidget(RecvArea, 7);
|
||||
@@ -126,10 +131,13 @@ MainWidget::MainWidget(QWidget *parent)
|
||||
rvlayout = new QVBoxLayout();
|
||||
rvlayout->addWidget(ClearButton, 0, Qt::AlignCenter);
|
||||
rvlayout->addWidget(HexRecv);
|
||||
rvlayout->addWidget(RTSBox);
|
||||
rvlayout->addWidget(DTRBox);
|
||||
|
||||
layout = new QGridLayout(this);
|
||||
|
||||
layout->addLayout(flayout, 0, 0, 2, 1);
|
||||
// layout->addLayout(flayout, 0, 0, 2, 1);
|
||||
layout->addWidget(paramGroup, 0, 0, 2, 1);
|
||||
layout->addLayout(hlayout, 1, 1);
|
||||
layout->addLayout(cvlayout, 0, 1, 1, 1);
|
||||
layout->addLayout(rvlayout, 0, 2, Qt::AlignTop);
|
||||
@@ -171,6 +179,8 @@ 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);
|
||||
}
|
||||
@@ -210,6 +220,7 @@ void MainWidget::CheckSerials()
|
||||
COMBox->setDisabled(true);
|
||||
OpenButton->setDisabled(true);
|
||||
emit CloseSerial();
|
||||
isOpened = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,12 +235,12 @@ 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()
|
||||
@@ -239,11 +250,8 @@ void MainWidget::serialNotOpened()
|
||||
|
||||
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("串口已关闭"));
|
||||
}
|
||||
|
||||
@@ -333,6 +341,28 @@ 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(' ');
|
||||
@@ -345,6 +375,26 @@ QString MainWidget::HexStringToString(QString hexstr)
|
||||
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();
|
||||
|
||||
+7
-1
@@ -43,6 +43,8 @@ signals:
|
||||
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(); //串口打开成功
|
||||
@@ -57,6 +59,8 @@ public slots:
|
||||
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;
|
||||
@@ -70,7 +74,7 @@ private:
|
||||
QGroupBox *paramGroup;
|
||||
QTextEdit *RecvArea, *SendArea;
|
||||
QPushButton *OpenButton, *SendButton, *ClearButton;
|
||||
QCheckBox *NewLineBox, *TimerBox, *HexSend, *HexRecv;
|
||||
QCheckBox *NewLineBox, *TimerBox, *HexSend, *HexRecv, *RTSBox, *DTRBox;
|
||||
QSpinBox *TimerSpin;
|
||||
QTimer *CheckTimer, *SendTimer;
|
||||
SerialController *serialController;
|
||||
@@ -80,8 +84,10 @@ private:
|
||||
bool isSendNewLine = false;
|
||||
bool isSendHex = false;
|
||||
bool isRecvHex = false;
|
||||
bool isOpened = false;
|
||||
|
||||
QString HexStringToString(QString hexstr); //解码16进制字符串
|
||||
void ACtionAttachToSerial(bool set);
|
||||
};
|
||||
|
||||
#endif // MAINWIDGET_H
|
||||
|
||||
+15
-2
@@ -1,10 +1,16 @@
|
||||
#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(tr("&关于Qt"), this);
|
||||
helpMenu = menuBar->addMenu(tr("&帮助"));
|
||||
helpMenu->addAction(aboutQtAct);
|
||||
setCentralWidget(widget);
|
||||
statusbar->showMessage(tr("准备就绪"));
|
||||
connect(widget, MainWidget::sendStatus, this, setNewMsg);
|
||||
@@ -12,6 +18,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
|
||||
statusBar()->addPermanentWidget(timeLabel);
|
||||
timeLabel->setText(QDateTime::currentDateTime().toString());
|
||||
|
||||
connect(aboutQtAct, &QAction::triggered, this, &showAboutQt);
|
||||
connect(widget, MainWidget::sendDateTime, this, setDateTime);
|
||||
resize(900, 600);
|
||||
}
|
||||
@@ -25,3 +32,9 @@ void MainWindow::setDateTime(QString datetime)
|
||||
{
|
||||
timeLabel->setText(datetime);
|
||||
}
|
||||
|
||||
void MainWindow::showAboutQt()
|
||||
{
|
||||
QMessageBox *aboutWindow = new QMessageBox(this);
|
||||
aboutWindow->aboutQt(this);
|
||||
}
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
#define MAINWINDOW_H
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QMenuBar>
|
||||
#include <QStatusBar>
|
||||
#include <QKeyEvent>
|
||||
#include <QMessageBox>
|
||||
#include <mainwidget.h>
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
@@ -17,11 +19,16 @@ signals:
|
||||
public slots:
|
||||
void setNewMsg(QString msg);
|
||||
void setDateTime(QString datetime);
|
||||
void showAboutQt();
|
||||
|
||||
private:
|
||||
MainWidget *widget;
|
||||
QMenuBar *menuBar;
|
||||
QMenu *helpMenu;
|
||||
QAction *aboutQtAct;
|
||||
QStatusBar *statusbar;
|
||||
QLabel *timeLabel;
|
||||
|
||||
};
|
||||
|
||||
#endif // MAINWINDOW_H
|
||||
|
||||
@@ -13,6 +13,7 @@ void SerialController::openSerial(QString name)
|
||||
{
|
||||
//串口打开成功
|
||||
serial->setDataTerminalReady(false);
|
||||
serial->setDataTerminalReady(false);
|
||||
emit openSuccess();
|
||||
}
|
||||
else
|
||||
@@ -80,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);
|
||||
}
|
||||
|
||||
@@ -27,6 +27,8 @@ public slots:
|
||||
void getParity(QString parity); //改变校验位
|
||||
void writeData(QString content); //发送数据
|
||||
void handleRecv(); //接受数据
|
||||
void contrloRTS(bool set);
|
||||
void controlDTR(bool set);
|
||||
|
||||
private:
|
||||
QSerialPort *serial;
|
||||
|
||||
Reference in New Issue
Block a user