fix hex
This commit is contained in:
+6
-1
@@ -9,6 +9,9 @@ set(CMAKE_AUTORCC ON)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
|
||||
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
|
||||
|
||||
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
|
||||
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
|
||||
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS SerialPort)
|
||||
@@ -43,12 +46,14 @@ else()
|
||||
else()
|
||||
add_executable(SerialAsst
|
||||
${PROJECT_SOURCES}
|
||||
${app_icon_resource_windows}
|
||||
resource.qrc
|
||||
)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
target_link_libraries(SerialAsst PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
|
||||
target_link_libraries(SerialAsst PRIVATE Qt6::SerialPort)
|
||||
target_link_libraries(SerialAsst PRIVATE Qt${QT_VERSION_MAJOR}::SerialPort)
|
||||
|
||||
# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
|
||||
# If you are developing for iOS or macOS you should consider setting an
|
||||
|
||||
@@ -1,8 +1,16 @@
|
||||
#include <QApplication>
|
||||
#include <QOperatingSystemVersion>
|
||||
#include <QtGlobal>
|
||||
#include "mainwindow.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
||||
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
|
||||
#endif
|
||||
if (QOperatingSystemVersion::currentType() == QOperatingSystemVersion::Windows) {
|
||||
QApplication::setFont(QFont{"Segoe UI", 10});
|
||||
}
|
||||
QApplication a(argc, argv);
|
||||
a.setWindowIcon(QIcon(QString(":/pic/icon/icon.png")));
|
||||
a.setStyle("fusion");
|
||||
|
||||
+106
-46
@@ -26,9 +26,11 @@ MainWidget::MainWidget(QWidget *parent)
|
||||
<< "6"
|
||||
<< "5";
|
||||
ParityList << tr("无") << tr("奇校验") << tr("偶校验");
|
||||
FlowControlList << tr("无") << tr("硬件") << tr("软件");
|
||||
|
||||
//初始化ComboBox
|
||||
COMBox = new QComboBox();
|
||||
COMBox->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
|
||||
BaudrateBox = new QComboBox();
|
||||
BaudrateBox->addItems(BaudrateList);
|
||||
BaudrateBox->setCurrentIndex(settings.value("Baud rate", 0).toInt());
|
||||
@@ -41,6 +43,9 @@ MainWidget::MainWidget(QWidget *parent)
|
||||
ParityBox = new QComboBox();
|
||||
ParityBox->addItems(ParityList);
|
||||
ParityBox->setCurrentIndex(settings.value("Parity", 0).toInt());
|
||||
FlowControlBox = new QComboBox();
|
||||
FlowControlBox->addItems(FlowControlList);
|
||||
FlowControlBox->setCurrentIndex(settings.value("Flow Control", 0).toInt());
|
||||
|
||||
//初始化label
|
||||
COMLabel = new QLabel(tr("串口号"));
|
||||
@@ -48,17 +53,24 @@ MainWidget::MainWidget(QWidget *parent)
|
||||
StopbitsLabel = new QLabel(tr("停止位"));
|
||||
DatabitsLabel = new QLabel(tr("数据位"));
|
||||
ParityLabel = new QLabel(tr("校验位"));
|
||||
FlowControlLabel = new QLabel(tr("流控"));
|
||||
|
||||
//发送与接受区域
|
||||
RecvArea = new QTextEdit();
|
||||
RecvArea = new QPlainTextEdit();
|
||||
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 = new QPlainTextEdit();
|
||||
|
||||
QFont areaFont = SendArea->font();
|
||||
areaFont.setPointSize(11);
|
||||
SendArea->setFont(areaFont);
|
||||
areaFont.setPointSize(11);
|
||||
RecvArea->setFont(areaFont);
|
||||
// RecvArea->setFont(QFont(tr("Microsoft YaHei UI"), 10));
|
||||
// SendArea->setFont(QFont(tr("Microsoft YaHei UI Light"), 12));
|
||||
SendArea->installEventFilter(this);
|
||||
|
||||
//按钮
|
||||
@@ -80,9 +92,15 @@ MainWidget::MainWidget(QWidget *parent)
|
||||
HexSend = new QCheckBox(tr("发送16进制"));
|
||||
HexRecv = new QCheckBox(tr("接收16进制"));
|
||||
HexSend->setToolTip(tr("以空格作为间隔符, \n非法字符及其后面的字符将被忽略"));
|
||||
|
||||
// RTS和 DTR复选框
|
||||
RTSBox = new QCheckBox(tr("RTS"));
|
||||
DTRBox = new QCheckBox(tr("DTR"));
|
||||
|
||||
flowControlLayout = new QHBoxLayout();
|
||||
flowControlLayout->addWidget(RTSBox);
|
||||
flowControlLayout->addWidget(DTRBox);
|
||||
|
||||
//绑定复选框信号
|
||||
connect(NewLineBox, &QCheckBox::stateChanged, this, &MainWidget::detNewLine);
|
||||
connect(TimerBox, &QCheckBox::stateChanged, this, &MainWidget::ControlSendTimer);
|
||||
@@ -106,14 +124,16 @@ MainWidget::MainWidget(QWidget *parent)
|
||||
leftLlayout->addRow(StopbitsLabel, StopbitsBox);
|
||||
leftLlayout->addRow(DatabitsLabel, DatabitsBox);
|
||||
leftLlayout->addRow(ParityLabel, ParityBox);
|
||||
leftLlayout->addRow(FlowControlLabel, FlowControlBox);
|
||||
leftLlayout->addRow(flowControlLayout);
|
||||
leftLlayout->addRow(COMLabel, COMBox);
|
||||
leftLlayout->addRow(OpenButton);
|
||||
leftLlayout->setAlignment(OpenButton, Qt::AlignVCenter);
|
||||
leftLlayout->setContentsMargins(30, 30, 30, 30);
|
||||
leftLlayout->setContentsMargins(15, 25, 15, 25);
|
||||
leftLlayout->setHorizontalSpacing(20);
|
||||
leftLlayout->setVerticalSpacing(60);
|
||||
leftLlayout->setVerticalSpacing(20);
|
||||
|
||||
paramGroup = new QGroupBox();
|
||||
paramGroup = new QGroupBox(tr("串口参数"));
|
||||
paramGroup->setLayout(leftLlayout);
|
||||
paramGroup->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||||
|
||||
@@ -132,8 +152,6 @@ MainWidget::MainWidget(QWidget *parent)
|
||||
rightLayout = new QVBoxLayout();
|
||||
rightLayout->addWidget(ClearRecvButton, 0, Qt::AlignCenter);
|
||||
rightLayout->addWidget(HexRecv);
|
||||
rightLayout->addWidget(RTSBox);
|
||||
rightLayout->addWidget(DTRBox);
|
||||
|
||||
centralLayout = new QGridLayout(this);
|
||||
|
||||
@@ -163,8 +181,6 @@ MainWidget::MainWidget(QWidget *parent)
|
||||
|
||||
//开始串口进程
|
||||
serialController = new SerialController;
|
||||
serialController->moveToThread(&SerialThr);
|
||||
SerialThr.start();
|
||||
|
||||
//connect开关串口控制信号 以及是否成功的返回信号
|
||||
connect(this, &MainWidget::requestOpen, serialController, &SerialController::openSerial);
|
||||
@@ -177,7 +193,11 @@ MainWidget::MainWidget(QWidget *parent)
|
||||
connect(this, &MainWidget::setStopBits, serialController, &SerialController::getStopbits);
|
||||
connect(this, &MainWidget::setDataBits, serialController, &SerialController::getDatabits);
|
||||
connect(this, &MainWidget::setParity, serialController, &SerialController::getParity);
|
||||
connect(BaudrateBox, &QComboBox::currentTextChanged, serialController, &SerialController::getBaudrate);
|
||||
connect(this, &MainWidget::setFlowControl, serialController, &SerialController::getFlowControl);
|
||||
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);
|
||||
@@ -187,14 +207,14 @@ MainWidget::MainWidget(QWidget *parent)
|
||||
connect(serialController, &SerialController::recvData, this, &MainWidget::getRecv);
|
||||
}
|
||||
|
||||
void MainWidget::CheckSerials()
|
||||
void MainWidget::CheckSerials(bool firstCheck)
|
||||
{
|
||||
//不断检查可用串口列表,并与当前列表进行比较,若发生变化则重新生成列表
|
||||
emit sendDateTime(QDateTime::currentDateTime().toString(Qt::ISODate)); //更新状态栏时间
|
||||
|
||||
QList<QSerialPortInfo> SerialList = QSerialPortInfo::availablePorts();
|
||||
if (!SerialList.isEmpty())
|
||||
{
|
||||
|
||||
if (!SerialList.isEmpty()) {
|
||||
QStringList TmpComList, TmpPortNameList, TmpDesList;
|
||||
for (QSerialPortInfo &serial : SerialList) {
|
||||
TmpComList << serial.portName() + " " + serial.description();
|
||||
@@ -210,15 +230,16 @@ void MainWidget::CheckSerials()
|
||||
DescList = TmpDesList;
|
||||
COMBox->setDisabled(false);
|
||||
COMBox->clear();
|
||||
COMBox->addItems(PortNameList);
|
||||
// COMBox->addItems(PortNameList);
|
||||
COMBox->addItems(COMList);
|
||||
|
||||
OpenButton->setDisabled(false);
|
||||
for (int i = 0; i < COMList.count(); i++) //为串口列表增加ToolTip
|
||||
{
|
||||
COMBox->setItemData(i, DescList[i], Qt::ToolTipRole);
|
||||
}
|
||||
}
|
||||
}
|
||||
else //可用串口为空时发送关闭串口信号
|
||||
} else //可用串口为空时发送关闭串口信号
|
||||
{
|
||||
COMBox->clear();
|
||||
COMList.clear();
|
||||
@@ -232,11 +253,12 @@ void MainWidget::CheckSerials()
|
||||
|
||||
MainWidget::~MainWidget()
|
||||
{
|
||||
SerialThr.terminate();
|
||||
// SerialThr.terminate();
|
||||
settings.setValue("Baud rate", BaudrateBox->currentIndex());
|
||||
settings.setValue("Stop bits", StopbitsBox->currentIndex());
|
||||
settings.setValue("Data bits", DatabitsBox->currentIndex());
|
||||
settings.setValue("Parity", ParityBox->currentIndex());
|
||||
settings.setValue("Flow Control", FlowControlBox->currentIndex());
|
||||
}
|
||||
|
||||
void MainWidget::serialOpened()
|
||||
@@ -248,7 +270,7 @@ void MainWidget::serialOpened()
|
||||
emit RTSBox->stateChanged(RTSBox->checkState());
|
||||
emit DTRBox->stateChanged(DTRBox->checkState());
|
||||
//相应控件可用性做出改变(setDisabled)
|
||||
ACtionAttachToSerial(true);
|
||||
ActionAttachToSerial(true);
|
||||
}
|
||||
|
||||
void MainWidget::serialNotOpened()
|
||||
@@ -263,11 +285,11 @@ void MainWidget::serialClosed()
|
||||
{
|
||||
isOpened = false;
|
||||
//相应控件可用性做出改变(setDisabled)
|
||||
ACtionAttachToSerial(false);
|
||||
ActionAttachToSerial(false);
|
||||
emit sendStatus(tr("串口已关闭"));
|
||||
}
|
||||
|
||||
void MainWidget::getRecv(QByteArray recv)
|
||||
void MainWidget::getRecv(const QByteArray &recv)
|
||||
{
|
||||
RecvArea->moveCursor(QTextCursor::End);
|
||||
//需要时将受到的数据进行16进制转换
|
||||
@@ -276,18 +298,21 @@ void MainWidget::getRecv(QByteArray recv)
|
||||
RecvArea->textCursor().insertText(QString::fromLocal8Bit(recv));
|
||||
else
|
||||
{
|
||||
RecvArea->textCursor().insertText(QString(recv.toHex()) + tr(" "));
|
||||
if (!RecvArea->toPlainText().isEmpty())
|
||||
RecvArea->textCursor().insertText(QChar(HEX_SEPARATOR));
|
||||
RecvArea->textCursor().insertText(QString(recv.toHex(HEX_SEPARATOR)));
|
||||
}
|
||||
}
|
||||
|
||||
void MainWidget::OpenSerial()
|
||||
{
|
||||
QString portName = COMBox->currentText();
|
||||
emit requestOpen(portName);
|
||||
emit setBaudRate(BaudrateBox->currentText());
|
||||
emit setStopBits(StopbitsBox->currentText());
|
||||
emit setDataBits(DatabitsBox->currentText());
|
||||
emit setParity(ParityBox->currentText());
|
||||
emit setFlowControl(FlowControlBox->currentText());
|
||||
emit requestOpen(portName);
|
||||
}
|
||||
|
||||
void MainWidget::CloseSerial()
|
||||
@@ -332,14 +357,14 @@ void MainWidget::detHex(int state)
|
||||
if (state == 2)
|
||||
{
|
||||
isSendHex = true;
|
||||
SendArea->setText(SendArea->toPlainText().toLocal8Bit().toHex(' '));
|
||||
// SendArea->setPlainText(SendArea->toPlainText().toLocal8Bit().toHex(' '));
|
||||
}
|
||||
else if (state == 0)
|
||||
{
|
||||
isSendHex = false;
|
||||
QString tmpstr = SendArea->toPlainText();
|
||||
SendArea->clear();
|
||||
SendArea->setText(HexStringToString(tmpstr));
|
||||
// QString tmpstr = SendArea->toPlainText();
|
||||
// SendArea->clear();
|
||||
// SendArea->setPlainText(HexStringToString(tmpstr));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -348,16 +373,16 @@ void MainWidget::detRecvHex(int state)
|
||||
if (state == 2)
|
||||
{
|
||||
isRecvHex = true;
|
||||
RecvArea->setText(RecvArea->toPlainText().toLocal8Bit().toHex(' '));
|
||||
RecvArea->moveCursor(QTextCursor::End);
|
||||
RecvArea->insertPlainText(tr(" "));
|
||||
// RecvArea->setPlainText(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 tmpstr = RecvArea->toPlainText();
|
||||
// RecvArea->clear();
|
||||
// RecvArea->setPlainText(HexStringToString(tmpstr));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -383,20 +408,50 @@ void MainWidget::DTRControl(int state)
|
||||
}
|
||||
}
|
||||
|
||||
QString MainWidget::HexStringToString(QString hexstr)
|
||||
QByteArray MainWidget::HexStringToString(const QString &hexstr)
|
||||
{
|
||||
//将内容为16进制字符串的string转换成16进制对应的内容
|
||||
QStringList list = hexstr.split(' ');
|
||||
QByteArray bit;
|
||||
foreach (QString str, list)
|
||||
{
|
||||
if (!str.isEmpty())
|
||||
bit.append(str.toInt(nullptr, 16));
|
||||
QByteArray bytes;
|
||||
char currentByte{0};
|
||||
bool hasReadFirstChar = false;
|
||||
for (auto ch : hexstr) {
|
||||
auto ascii = ch.toLatin1();
|
||||
if (ch == ' ') { // 允许空格
|
||||
continue;
|
||||
}
|
||||
auto numHex = convertAsciiToHex(ascii);
|
||||
if (numHex < 0) {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
if (!hasReadFirstChar) {
|
||||
currentByte += (numHex - '0') << 4;
|
||||
hasReadFirstChar = true;
|
||||
} else {
|
||||
currentByte += numHex;
|
||||
bytes.append(currentByte);
|
||||
hasReadFirstChar = false;
|
||||
currentByte = 0;
|
||||
}
|
||||
}
|
||||
return QString::fromLocal8Bit(bit);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
void MainWidget::ACtionAttachToSerial(bool set)
|
||||
char MainWidget::convertAsciiToHex(char ascii)
|
||||
{
|
||||
if (ascii >= '0' && ascii <= '9') {
|
||||
return ascii - '0';
|
||||
} else if (ascii >= 'a' && ascii <= 'f') {
|
||||
return 10 + ascii - 'a';
|
||||
} else if (ascii >= 'A' && ascii <= 'F') {
|
||||
return 10 + ascii - 'A';
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
void MainWidget::ActionAttachToSerial(bool set)
|
||||
{
|
||||
//根据串口开关状态决定一些控件的可用性
|
||||
if(set)
|
||||
@@ -420,11 +475,16 @@ void MainWidget::ACtionAttachToSerial(bool set)
|
||||
void MainWidget::SendContent()
|
||||
{
|
||||
QString content = SendArea->toPlainText();
|
||||
if (isSendHex)
|
||||
content = HexStringToString(content);
|
||||
QByteArray bytes;
|
||||
if (isSendNewLine)
|
||||
content += "\r\n";
|
||||
emit sendData(content);
|
||||
content += "\n";
|
||||
if (isSendHex) {
|
||||
bytes = HexStringToString(content);
|
||||
} else {
|
||||
bytes = content.toLatin1();
|
||||
}
|
||||
|
||||
emit sendData(bytes);
|
||||
}
|
||||
|
||||
bool MainWidget::eventFilter(QObject *watched, QEvent *event)
|
||||
|
||||
+38
-35
@@ -1,26 +1,26 @@
|
||||
#ifndef MAINWIDGET_H
|
||||
#define MAINWIDGET_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QComboBox>
|
||||
#include <QGridLayout>
|
||||
#include <QApplication>
|
||||
#include <QVBoxLayout>
|
||||
#include <QByteArray>
|
||||
#include <QCheckBox>
|
||||
#include <QComboBox>
|
||||
#include <QDateTime>
|
||||
#include <QFormLayout>
|
||||
#include <QGridLayout>
|
||||
#include <QGroupBox>
|
||||
#include <QHBoxLayout>
|
||||
#include <QTextEdit>
|
||||
#include <QLabel>
|
||||
#include <QPlainTextEdit>
|
||||
#include <QPushButton>
|
||||
#include <QTimer>
|
||||
#include <QSerialPortInfo>
|
||||
#include <QLabel>
|
||||
#include <QCheckBox>
|
||||
#include <QSpinBox>
|
||||
#include <QThread>
|
||||
#include <QDateTime>
|
||||
#include <QByteArray>
|
||||
#include <QSettings>
|
||||
#include <QFormLayout>
|
||||
#include <QGroupBox>
|
||||
#include <QSpinBox>
|
||||
#include <QTextEdit>
|
||||
#include <QThread>
|
||||
#include <QTimer>
|
||||
#include <QVBoxLayout>
|
||||
#include <QWidget>
|
||||
#include "serialcontroller.h"
|
||||
|
||||
class MainWidget : public QWidget
|
||||
@@ -29,29 +29,30 @@ class MainWidget : public QWidget
|
||||
|
||||
public:
|
||||
MainWidget(QWidget *parent = 0);
|
||||
void CheckSerials();
|
||||
void CheckSerials(bool firstCheck = true);
|
||||
~MainWidget();
|
||||
void SendContent();
|
||||
bool eventFilter(QObject *watched, QEvent *event);
|
||||
|
||||
signals:
|
||||
void requestOpen(QString portName);
|
||||
signals:
|
||||
void requestOpen(const 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 setBaudRate(const QString &baudrate);
|
||||
void setStopBits(const QString &stopbits);
|
||||
void setDataBits(const QString &databits);
|
||||
void setParity(const QString &parity);
|
||||
void setFlowControl(const QString &flowControl);
|
||||
void sendData(const QByteArray &content);
|
||||
void sendStatus(const QString &status); //更新statusbar
|
||||
void sendDateTime(const QString &datetime); //更新stastatusbar中的时间
|
||||
void changeRTS(bool set);
|
||||
void changeDTR(bool set);
|
||||
|
||||
public slots:
|
||||
public slots:
|
||||
void serialOpened(); //串口打开成功
|
||||
void serialNotOpened(); //串口打开失败
|
||||
void serialClosed(); //串口关闭
|
||||
void getRecv(QByteArray recv); //串口接受数据
|
||||
void getRecv(const QByteArray &recv); //串口接受数据
|
||||
void OpenSerial();
|
||||
void CloseSerial();
|
||||
void ClearRecv();
|
||||
@@ -64,32 +65,34 @@ public slots:
|
||||
void RTSControl(int state);
|
||||
void DTRControl(int state);
|
||||
|
||||
private:
|
||||
QComboBox *COMBox, *BaudrateBox, *StopbitsBox, *DatabitsBox, *ParityBox;
|
||||
QStringList BaudrateList, StopbitsList, DatabitsList, ParityList;
|
||||
private:
|
||||
QComboBox *COMBox, *BaudrateBox, *StopbitsBox, *DatabitsBox, *ParityBox, *FlowControlBox;
|
||||
QStringList BaudrateList, StopbitsList, DatabitsList, ParityList, FlowControlList;
|
||||
QStringList COMList, PortNameList, DescList;
|
||||
QLabel *COMLabel, *BaudrateLabel, *StopbitsLabel, *DatabitsLabel, *ParityLabel;
|
||||
QLabel *COMLabel, *BaudrateLabel, *StopbitsLabel, *DatabitsLabel, *ParityLabel, *FlowControlLabel;
|
||||
QGridLayout *centralLayout;
|
||||
QVBoxLayout *cvlayout, *rightLayout;
|
||||
QHBoxLayout *bottomLayout;
|
||||
QHBoxLayout *bottomLayout, *flowControlLayout;
|
||||
QFormLayout *leftLlayout;
|
||||
QGroupBox *paramGroup;
|
||||
QTextEdit *RecvArea, *SendArea;
|
||||
QPlainTextEdit *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;
|
||||
|
||||
static const char HEX_SEPARATOR{':'};
|
||||
|
||||
bool isSendNewLine = false;
|
||||
bool isSendHex = false;
|
||||
bool isRecvHex = false;
|
||||
bool isOpened = false;
|
||||
|
||||
QString HexStringToString(QString hexstr); //解码16进制字符串
|
||||
void ACtionAttachToSerial(bool set);
|
||||
QByteArray HexStringToString(const QString &hexstr); //解码16进制字符串
|
||||
char convertAsciiToHex(char ascii);
|
||||
void ActionAttachToSerial(bool set);
|
||||
};
|
||||
|
||||
#endif // MAINWIDGET_H
|
||||
|
||||
+2
-2
@@ -8,8 +8,8 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
|
||||
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);
|
||||
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);
|
||||
|
||||
+21
-10
@@ -6,9 +6,9 @@ SerialController::SerialController(QObject *parent) : QObject(parent)
|
||||
connect(serial, &QSerialPort::readyRead, this, &SerialController::handleRecv);
|
||||
}
|
||||
|
||||
void SerialController::openSerial(QString name)
|
||||
void SerialController::openSerial(const QString &name)
|
||||
{
|
||||
serial->setPortName(name);
|
||||
serial->setPortName(name.split(" ").at(0));
|
||||
if (serial->open(QIODevice::ReadWrite))
|
||||
{
|
||||
//串口打开成功
|
||||
@@ -32,12 +32,12 @@ void SerialController::closeSerial()
|
||||
}
|
||||
}
|
||||
|
||||
void SerialController::getBaudrate(QString baudrate)
|
||||
void SerialController::getBaudrate(const QString &baudrate)
|
||||
{
|
||||
serial->setBaudRate(baudrate.toInt());
|
||||
}
|
||||
|
||||
void SerialController::getStopbits(QString stopbits)
|
||||
void SerialController::getStopbits(const QString &stopbits)
|
||||
{
|
||||
if (stopbits == "1")
|
||||
serial->setStopBits(QSerialPort::OneStop);
|
||||
@@ -47,12 +47,12 @@ void SerialController::getStopbits(QString stopbits)
|
||||
serial->setStopBits(QSerialPort::TwoStop);
|
||||
}
|
||||
|
||||
void SerialController::getDatabits(QString databits)
|
||||
void SerialController::getDatabits(const QString &databits)
|
||||
{
|
||||
serial->setDataBits(QSerialPort::DataBits(databits.toInt()));
|
||||
}
|
||||
|
||||
void SerialController::getParity(QString parity)
|
||||
void SerialController::getParity(const QString &parity)
|
||||
{
|
||||
if (parity == tr("无"))
|
||||
serial->setParity(QSerialPort::NoParity);
|
||||
@@ -64,11 +64,22 @@ void SerialController::getParity(QString parity)
|
||||
serial->setParity(QSerialPort::EvenParity);
|
||||
}
|
||||
|
||||
void SerialController::writeData(QString content)
|
||||
void SerialController::getFlowControl(const QString &flowControl)
|
||||
{
|
||||
if (serial->isWritable())
|
||||
{
|
||||
qint64 result = serial->write(content.toLocal8Bit().data());
|
||||
if (flowControl == tr("无")) {
|
||||
serial->setFlowControl(QSerialPort::NoFlowControl);
|
||||
} else if (flowControl == tr("硬件")) {
|
||||
serial->setFlowControl(QSerialPort::HardwareControl);
|
||||
|
||||
} else if (flowControl == tr("软件")) {
|
||||
serial->setFlowControl(QSerialPort::SoftwareControl);
|
||||
}
|
||||
}
|
||||
|
||||
void SerialController::writeData(const QByteArray &content)
|
||||
{
|
||||
if (serial->isWritable()) {
|
||||
qint64 result = serial->write(content);
|
||||
if (result == -1)
|
||||
emit writeFailed();
|
||||
else
|
||||
|
||||
+13
-12
@@ -16,21 +16,22 @@ signals:
|
||||
void closeSuccess(); //串口关闭成功
|
||||
void writeSuccess(int len); //串口发送成功 len为成功发送的字节
|
||||
void writeFailed(); //串口发送失败
|
||||
void recvData(QByteArray content); //发送接收到的数据
|
||||
void recvData(const QByteArray &content); //发送接收到的数据
|
||||
|
||||
public slots:
|
||||
void openSerial(QString name);
|
||||
public slots:
|
||||
void openSerial(const 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位
|
||||
void getBaudrate(const QString &baudrate); //改变波特率
|
||||
void getStopbits(const QString &stopbits); //改变停止位
|
||||
void getDatabits(const QString &databits); //改变数据为
|
||||
void getParity(const QString &parity); //改变校验位
|
||||
void getFlowControl(const QString &flowControl); //改变流控位
|
||||
void writeData(const QByteArray &content); //发送数据
|
||||
void handleRecv(); //接受数据
|
||||
void contrloRTS(bool set); //设置RTS位
|
||||
void controlDTR(bool set); //设置DTR位
|
||||
|
||||
private:
|
||||
private:
|
||||
QSerialPort *serial;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user