From 8d2eeb6ae7e72d035c0f319a86247d61c3053c1f Mon Sep 17 00:00:00 2001 From: c-my Date: Sun, 21 Oct 2018 18:42:57 +0800 Subject: [PATCH] initial commit --- Radar.pro | 41 +++ Radar.pro.user | 602 +++++++++++++++++++++++++++++++++++++++++++ Rader.pro.user | 338 ++++++++++++++++++++++++ main.cpp | 20 ++ radar.cpp | 91 +++++++ radar.h | 45 ++++ serialcontroller.cpp | 99 +++++++ serialcontroller.h | 42 +++ serialwidget.cpp | 263 +++++++++++++++++++ serialwidget.h | 84 ++++++ 10 files changed, 1625 insertions(+) create mode 100644 Radar.pro create mode 100644 Radar.pro.user create mode 100644 Rader.pro.user create mode 100644 main.cpp create mode 100644 radar.cpp create mode 100644 radar.h create mode 100644 serialcontroller.cpp create mode 100644 serialcontroller.h create mode 100644 serialwidget.cpp create mode 100644 serialwidget.h diff --git a/Radar.pro b/Radar.pro new file mode 100644 index 0000000..855e84b --- /dev/null +++ b/Radar.pro @@ -0,0 +1,41 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2018-10-21T00:25:30 +# +#------------------------------------------------- + +QT += core gui serialport + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = Rader +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which has been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +CONFIG += c++11 + +SOURCES += \ + main.cpp \ + radar.cpp \ + serialcontroller.cpp \ + serialwidget.cpp + +HEADERS += \ + radar.h \ + serialcontroller.h \ + serialwidget.h + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/Radar.pro.user b/Radar.pro.user new file mode 100644 index 0000000..0d9d3c4 --- /dev/null +++ b/Radar.pro.user @@ -0,0 +1,602 @@ + + + + + + EnvironmentId + {2c3e6c03-a454-4a53-bc1b-c224563b9344} + + + ProjectExplorer.Project.ActiveTarget + 1 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + true + false + 0 + true + true + 0 + 8 + true + 1 + true + true + true + false + + + + ProjectExplorer.Project.PluginSettings + + + true + + + + ProjectExplorer.Project.Target.0 + + Desktop Qt 5.11.2 GCC 64bit + Desktop Qt 5.11.2 GCC 64bit + qt.qt5.5112.gcc_64_kit + 1 + 0 + 0 + + /home/cmy/Projects/build-Radar-Desktop_Qt_5_11_2_GCC_64bit-Debug + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + false + false + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + true + + + /home/cmy/Projects/build-Radar-Desktop_Qt_5_11_2_GCC_64bit-Release + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + + false + false + true + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + + /home/cmy/Projects/build-Radar-Desktop_Qt_5_11_2_GCC_64bit-Profile + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + true + true + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Profile + Profile + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + 3 + + + 0 + 部署 + + ProjectExplorer.BuildSteps.Deploy + + 1 + Deploy Configuration + + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + false + false + 1000 + + true + + false + false + false + false + true + 0.01 + 10 + true + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + + Radar + + Qt4ProjectManager.Qt4RunConfiguration:/home/cmy/Projects/Radar/Radar.pro + true + + Radar.pro + + /home/cmy/Projects/build-Radar-Desktop_Qt_5_11_2_GCC_64bit-Release + 3768 + false + true + false + false + true + + 1 + + + + ProjectExplorer.Project.Target.1 + + Desktop Qt 5.11.2 GCC 64bit_static + Desktop Qt 5.11.2 GCC 64bit_static + {e0a85e13-9414-41c1-b1e3-1b96941c2dc8} + 1 + 0 + 0 + + /home/cmy/Projects/build-Radar-Desktop_Qt_5_11_2_GCC_64bit_static-Debug + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + false + false + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + true + + + /home/cmy/Projects/build-Radar-Desktop_Qt_5_11_2_GCC_64bit_static-Release + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + + false + false + true + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + + /home/cmy/Projects/build-Radar-Desktop_Qt_5_11_2_GCC_64bit_static-Profile + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + true + true + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Profile + Profile + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + 3 + + + 0 + 部署 + + ProjectExplorer.BuildSteps.Deploy + + 1 + Deploy Configuration + + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + false + false + 1000 + + true + + false + false + false + false + true + 0.01 + 10 + true + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + + Radar + + Qt4ProjectManager.Qt4RunConfiguration:/home/cmy/Projects/Radar/Radar.pro + true + + Radar.pro + + /home/cmy/Projects/build-Radar-Desktop_Qt_5_11_2_GCC_64bit_static-Release + 3768 + false + true + false + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 2 + + + ProjectExplorer.Project.Updater.FileVersion + 18 + + + Version + 18 + + diff --git a/Rader.pro.user b/Rader.pro.user new file mode 100644 index 0000000..2459603 --- /dev/null +++ b/Rader.pro.user @@ -0,0 +1,338 @@ + + + + + + EnvironmentId + {2c3e6c03-a454-4a53-bc1b-c224563b9344} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + true + false + 0 + true + true + 0 + 8 + true + 1 + true + true + true + false + + + + ProjectExplorer.Project.PluginSettings + + + true + + + + ProjectExplorer.Project.Target.0 + + Desktop Qt 5.11.2 GCC 64bit + Desktop Qt 5.11.2 GCC 64bit + qt.qt5.5112.gcc_64_kit + 0 + 0 + 0 + + /home/cmy/Projects/build-Rader-Desktop_Qt_5_11_2_GCC_64bit-Debug + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + false + false + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + true + + + /home/cmy/Projects/build-Rader-Desktop_Qt_5_11_2_GCC_64bit-Release + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + + false + false + true + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + + /home/cmy/Projects/build-Rader-Desktop_Qt_5_11_2_GCC_64bit-Profile + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + true + true + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Profile + Profile + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + 3 + + + 0 + 部署 + + ProjectExplorer.BuildSteps.Deploy + + 1 + Deploy Configuration + + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + false + false + 1000 + + true + + false + false + false + false + true + 0.01 + 10 + true + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + + Rader + + Qt4ProjectManager.Qt4RunConfiguration:/home/cmy/Projects/Rader/Rader.pro + true + + Rader.pro + + /home/cmy/Projects/build-Rader-Desktop_Qt_5_11_2_GCC_64bit-Debug + 3768 + false + true + false + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 18 + + + Version + 18 + + diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..f12c6f4 --- /dev/null +++ b/main.cpp @@ -0,0 +1,20 @@ +#include "serialwidget.h" +#include "radar.h" +#include +#include + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + Radar r; + + SerialWidget sw; + + QObject::connect(&sw, &SerialWidget::deliverPos, &r, &Radar::getPos); + r.show(); + sw.show(); + + return a.exec(); +} diff --git a/radar.cpp b/radar.cpp new file mode 100644 index 0000000..63df382 --- /dev/null +++ b/radar.cpp @@ -0,0 +1,91 @@ +#include "radar.h" +#include + +Radar::Radar(QWidget *parent) : QWidget(parent) +{ + setWindowTitle(tr("Radar")); + + setAutoFillBackground(true); + setPalette(QPalette(Qt::black)); + lefttop = 20; + interval = 150; + scanAngle = 358; + + lastAngle = 0; + lastDistant = 0; + + // test + currentAngle = 180; + isClock = true; + Point p; + p.angle = 45; + p.dis = 20; + pointList.push_back(p); + pointList.push_back({46,30}); + pointList.push_back({47,30}); + pointList.push_back({90,40}); + pointList.push_back({135,50}); + + + baseRadius = 500; + +// serial = new SerialController; +// serial->moveToThread(&SerialThr); +// SerialThr.start(); + connect(&scanTimer,&QTimer::timeout, this, [this](){ + currentAngle++; + this->update(); + }); + scanTimer.start(1); +} + +void Radar::paintEvent(QPaintEvent *) +{ + // radar background + QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing, true); + painter.setPen(QPen(Qt::green,1)); + + painter.drawLine(lefttop, lefttop + baseRadius/2,lefttop + baseRadius,lefttop + baseRadius/2); + painter.drawLine(lefttop + baseRadius/2,lefttop, lefttop + baseRadius/2,lefttop + baseRadius); + + for(int i=0;i<5;++i){ + painter.drawArc(lefttop+i*interval/2,lefttop+i*interval/2,baseRadius-i*interval,baseRadius-i*interval,0,360*16); + } + + // scan area + QConicalGradient conicalGradient(lefttop+baseRadius/2, lefttop+baseRadius/2, currentAngle); + + if(isClock){ + conicalGradient.setAngle(currentAngle-scanAngle-1); + conicalGradient.setColorAt(0, QColor(0,0,0,120)); + conicalGradient.setColorAt((scanAngle)/360.0, QColor(0,255,0,120)); + painter.setPen(QPen(QColor(0,0,0,0),1)); + painter.setBrush(QBrush(conicalGradient)); + painter.drawPie(lefttop,lefttop,baseRadius,baseRadius,(currentAngle-scanAngle)*16,scanAngle*16); + } else{ + conicalGradient.setColorAt(0, QColor(0,255,0,120)); + conicalGradient.setColorAt(scanAngle/360.0, QColor(0,0,0,120)); + painter.setPen(QPen(QColor(0,0,0,0),1)); + painter.setBrush(QBrush(conicalGradient)); + painter.drawPie(lefttop,lefttop,baseRadius,baseRadius,currentAngle*16,scanAngle*16); + } +// painter.drawRect(250,250,20,20); + + // draw target + painter.setPen(QPen(Qt::red)); + painter.setBrush(QBrush(Qt::red)); + for(auto p : pointList) + { + double x = lefttop + baseRadius/2 + p.dis*qCos(qDegreesToRadians(p.angle)); + double y = lefttop + baseRadius/2 - p.dis*qSin(qDegreesToRadians(p.angle)); + painter.drawPoint(int(x), int(y)); + painter.drawEllipse(int(x), int(y), 5, 5); + } +} + +void Radar::getPos(double ang, double dis) +{ + pointList.push_back({ang,dis}); + update(); +} diff --git a/radar.h b/radar.h new file mode 100644 index 0000000..fd95b32 --- /dev/null +++ b/radar.h @@ -0,0 +1,45 @@ +#ifndef RADAR_H +#define RADAR_H + +#include +#include +#include +#include +#include +#include +#include + +class Radar : public QWidget +{ + Q_OBJECT +public: + explicit Radar(QWidget *parent = nullptr); + void paintEvent(QPaintEvent *); +private: + struct Point{ + double angle; + double dis; + }; + + QVector pointList; + + QTimer scanTimer; + int lefttop; + int interval; + int scanAngle; + + int currentAngle, lastAngle; + int currentDistant, lastDistant; + bool isClock; + int baseRadius; + + + +signals: + void sendPos(double ang, double dis); + +public slots: + void getPos(double ang, double dis); +}; + +#endif // RADAR_H diff --git a/serialcontroller.cpp b/serialcontroller.cpp new file mode 100644 index 0000000..88291cc --- /dev/null +++ b/serialcontroller.cpp @@ -0,0 +1,99 @@ +#include "serialcontroller.h" + +SerialController::SerialController(QObject *parent) : QObject(parent) +{ + serial = new QSerialPort(this); + connect(serial, SIGNAL(readyRead()), this,SLOT(handleRecv())); + + QList SerialList = QSerialPortInfo::availablePorts(); + if(SerialList.size()!=0){ + openSerial(SerialList[0].portName()); + } +// testAng = 0; +// testWise= false; + +// connect(&testTimer,&QTimer::timeout, this,[this](){ +// emit sendPos(testAng-=2,0); +// }); +// testTimer.setInterval(1); +// testTimer.start(); + + +} + +void SerialController::openSerial(QString name) +{ + serial->setPortName(name); + if (serial->open(QIODevice::ReadWrite)) + { + //串口打开成功 + serial->setDataTerminalReady(false); + serial->setDataTerminalReady(false); + emit openSuccess(); + + } + else + { + //串口打开失败 + emit openFailed(); + + } +} + +void SerialController::getBaudrate(QString baudrate) +{ + serial->setBaudRate(baudrate.toInt()); +} + +void SerialController::getStopbits(QString stopbits) +{ + if (stopbits == "1") + serial->setStopBits(QSerialPort::OneStop); + else if (stopbits == "1.5") + serial->setStopBits(QSerialPort::OneAndHalfStop); + else if (stopbits == "2") + serial->setStopBits(QSerialPort::TwoStop); +} + +void SerialController::getDatabits(QString databits) +{ + serial->setDataBits(QSerialPort::DataBits(databits.toInt())); +} + +void SerialController::getParity(QString parity) +{ + if (parity == tr("无")) + serial->setParity(QSerialPort::NoParity); + + else if (parity == tr("奇校验")) + serial->setParity(QSerialPort::OddParity); + + else if (parity == tr("偶校验")) + serial->setParity(QSerialPort::EvenParity); +} + +void SerialController::closeSerial() +{ + if (serial->isOpen()) + { + serial->close(); + emit closeSuccess(); + } +} + +void SerialController::handleRecv() +{ + QByteArray data = serial->readAll(); + QString str = data; + emit sendPos(str.split("|")[0].toDouble(),str.split("|")[1].toDouble()); +} + +void SerialController::contrloRTS(bool set) +{ + serial->setRequestToSend(set); +} + +void SerialController::controlDTR(bool set) +{ + serial->setDataTerminalReady(set); +} diff --git a/serialcontroller.h b/serialcontroller.h new file mode 100644 index 0000000..5446e56 --- /dev/null +++ b/serialcontroller.h @@ -0,0 +1,42 @@ +#ifndef SERIALCONTROLLER_H +#define SERIALCONTROLLER_H + +#include +#include +#include +#include +#include + +class SerialController : public QObject +{ + Q_OBJECT +public: + explicit SerialController(QObject *parent = nullptr); + void openSerial(QString name); + + void closeSerial(); +private: + QSerialPort *serial; + QTimer testTimer; + int testAng; + int testWise; + + +signals: + void sendPos(double angle, double dis); + void openSuccess(); + void openFailed(); + void closeSuccess(); + + +public slots: + void handleRecv(); + void getBaudrate(QString baudrate); + void getStopbits(QString stopbits); + void getDatabits(QString databits); + void getParity(QString parity); + void contrloRTS(bool set); + void controlDTR(bool set); +}; + +#endif // SERIALCONTROLLER_H diff --git a/serialwidget.cpp b/serialwidget.cpp new file mode 100644 index 0000000..90b93d5 --- /dev/null +++ b/serialwidget.cpp @@ -0,0 +1,263 @@ +#include "serialwidget.h" + +SerialWidget::SerialWidget(QWidget *parent) : QWidget(parent) +{ + + setWindowTitle(tr("Control panel")); + + serialController = new SerialController; + serialController->moveToThread(&SerialThr); + SerialThr.start(); + + 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); + // BaudrateBox->setCurrentIndex(settings.value("Baud rate", 0).toInt()); + StopbitsBox = new QComboBox(); + StopbitsBox->addItems(StopbitsList); + // StopbitsBox->setCurrentIndex(settings.value("Stop bits", 0).toInt()); + DatabitsBox = new QComboBox(); + DatabitsBox->addItems(DatabitsList); + // DatabitsBox->setCurrentIndex(settings.value("Data bits", 0).toInt()); + ParityBox = new QComboBox(); + ParityBox->addItems(ParityList); + + //初始化label + COMLabel = new QLabel(tr("串口号")); + BaudrateLabel = new QLabel(tr("波特率")); + StopbitsLabel = new QLabel(tr("停止位")); + DatabitsLabel = new QLabel(tr("数据位")); + ParityLabel = new QLabel(tr("校验位")); + + //按钮 + OpenButton = new QPushButton(tr("打开串口")); + + //checkbox + RTSBox = new QCheckBox(tr("RTS")); + DTRBox = new QCheckBox(tr("DTR")); + + connect(RTSBox, &QCheckBox::stateChanged, this, [this](int state){ + if(isOpened) + { + if(state == 2) + emit changeRTS(true); + else if(state == 0) + emit changeRTS(false); + } + }); + connect(DTRBox, &QCheckBox::stateChanged, this, [this](int state){ + if(isOpened) + { + if(state == 2) + emit changeDTR(true); + else if(state == 0) + emit changeDTR(false); + } + }); + + + connect(OpenButton, &QPushButton::clicked, this, &SerialWidget::OpenSerial); + connect(this, &SerialWidget::requestClose, serialController, &SerialController::closeSerial); + + + + //初始化布局 + + bottomLayout = new QHBoxLayout(); + bottomLayout->addWidget(RTSBox); + bottomLayout->addWidget(DTRBox); + + 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); + + cvlayout = new QVBoxLayout(); + cvlayout->addLayout(bottomLayout); + cvlayout->addLayout(leftLlayout); + + + + setLayout(cvlayout); + + CheckTimer = new QTimer(this); + CheckTimer->start(1000); + connect(CheckTimer, &QTimer::timeout, this, &SerialWidget::CheckSerials); + + connect(this, &SerialWidget::requestOpen, serialController, &SerialController::openSerial); + connect(this, &SerialWidget::setBaudRate, serialController, &SerialController::getBaudrate); + connect(this, &SerialWidget::setStopBits, serialController, &SerialController::getStopbits); + connect(this, &SerialWidget::setDataBits, serialController, &SerialController::getDatabits); + connect(this, &SerialWidget::setParity, serialController, &SerialController::getParity); + + connect(serialController, &SerialController::openSuccess, this, &SerialWidget::serialOpened); + connect(serialController, &SerialController::openFailed, this, &SerialWidget::serialNotOpened); + connect(serialController, &SerialController::closeSuccess, this, &SerialWidget::serialClosed); + connect(serialController, &SerialController::sendPos, this, &SerialWidget::getRecv); + + + + connect(RTSBox, &QCheckBox::stateChanged, this, [this](int state){ + if(isOpened) + { + if(state == 2) + emit changeRTS(true); + else if(state == 0) + emit changeRTS(false); + }}); + connect(DTRBox, &QCheckBox::stateChanged, this, [this](int state){ + if(isOpened) + { + if(state == 2) + emit changeDTR(true); + else if(state == 0) + emit changeDTR(false); + } + }); + + connect(this, &SerialWidget::changeRTS, serialController, &SerialController::contrloRTS); + connect(this, &SerialWidget::changeDTR, serialController, &SerialController::controlDTR); + + + +} + +void SerialWidget::CheckSerials() +{ + QList SerialList = QSerialPortInfo::availablePorts(); + if (!SerialList.isEmpty()) + { + QStringList TmpComList, TmpPortNameList, TmpDesList; + for (QSerialPortInfo serial : SerialList) + { + TmpComList << serial.portName() + " " + serial.description(); + TmpPortNameList << serial.portName(); + TmpDesList << serial.description(); + } + if (COMList != TmpComList) + { + //TODO: 可用串口发生改变却不为空时,保持串口打开状态,及其他细节的处理 + //只在串口发生变化时刷新ComboBox + COMList = TmpComList; + PortNameList = TmpPortNameList; + DescList = TmpDesList; + COMBox->setDisabled(false); + COMBox->clear(); + COMBox->addItems(PortNameList); + OpenButton->setDisabled(false); + for (int i = 0; i < COMList.count(); i++) //为串口列表增加ToolTip + { + COMBox->setItemData(i, DescList[i], Qt::ToolTipRole); + } + } + } + else //可用串口为空时发送关闭串口信号 + { + COMBox->clear(); + COMList.clear(); + COMBox->addItem(tr("(空)")); + COMBox->setDisabled(true); + OpenButton->setDisabled(true); + // emit CloseSerial(); + isOpened = false; + } + +} + +SerialWidget::~SerialWidget() +{ + SerialThr.exit(); +} + + +void SerialWidget::serialOpened() +{ + isOpened = true; + //串口关闭时无法设置RTS,DTR信号(但此时checkbox仍然是可操作的),串口打开时发送stateChanged信号来应用改变 + emit RTSBox->stateChanged(RTSBox->checkState()); + emit DTRBox->stateChanged(DTRBox->checkState()); + //相应控件可用性做出改变(setDisabled) + ACtionAttachToSerial(true); +} +void SerialWidget::serialNotOpened() +{ + QMessageBox::warning(this,tr("Warning"),tr("Failed to open serial port!")); +} + +void SerialWidget::serialClosed() +{ + +} + +void SerialWidget::getRecv(double ang, double dis) +{ + emit deliverPos(ang, dis); +} + +void SerialWidget::OpenSerial() +{ + QString portName = COMBox->currentText(); + emit requestOpen(portName); + emit setBaudRate(BaudrateBox->currentText()); + emit setStopBits(StopbitsBox->currentText()); + emit setDataBits(DatabitsBox->currentText()); + emit setParity(ParityBox->currentText()); +} + +void SerialWidget::CloseSerial() +{ + emit requestClose(); + +} + +void SerialWidget::ACtionAttachToSerial(bool set) +{ + //根据串口开关状态决定一些控件的可用性 + if(set) + { + OpenButton->setText(tr("关闭串口")); + COMBox->setDisabled(true); + SendButton->setDisabled(false); + disconnect(OpenButton, &QPushButton::clicked, this, &SerialWidget::OpenSerial); + connect(OpenButton, &QPushButton::clicked, this, &SerialWidget::CloseSerial); + } + else + { + OpenButton->setText(tr("打开串口")); + COMBox->setDisabled(false); + SendButton->setDisabled(true); + disconnect(OpenButton, &QPushButton::clicked, this, &SerialWidget::CloseSerial); + connect(OpenButton, &QPushButton::clicked, this, &SerialWidget::OpenSerial); + } +} diff --git a/serialwidget.h b/serialwidget.h new file mode 100644 index 0000000..fa13613 --- /dev/null +++ b/serialwidget.h @@ -0,0 +1,84 @@ +#ifndef SERIALWIDGET_H +#define SERIALWIDGET_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "serialcontroller.h" + +class SerialWidget : public QWidget +{ + Q_OBJECT +public: + SerialWidget(QWidget *parent = 0); + void CheckSerials(); + ~SerialWidget(); + // 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 changeRTS(bool set); + void changeDTR(bool set); + + void deliverPos(double ang,double dis); + +public slots: + void serialOpened(); //串口打开成功 + void serialNotOpened(); //串口打开失败 + void serialClosed(); //串口关闭 + void getRecv(double ang,double dis); //串口接受数据 + 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 BaudrateList, StopbitsList, DatabitsList, ParityList; + QStringList COMList, PortNameList, DescList; + QLabel *COMLabel, *BaudrateLabel, *StopbitsLabel, *DatabitsLabel, *ParityLabel; + QCheckBox *RTSBox, *DTRBox; + // QGridLayout *centralLayout; + QVBoxLayout *cvlayout; + QHBoxLayout *bottomLayout; + QFormLayout *leftLlayout; + // QTextEdit *RecvArea, *SendArea; + QPushButton *OpenButton, *SendButton; + QTimer *CheckTimer; + SerialController *serialController; + QThread SerialThr; + + bool isSendNewLine = false; + bool isSendHex = false; + bool isRecvHex = false; + bool isOpened = false; + + // QString HexStringToString(QString hexstr); //解码16进制字符串 + void ACtionAttachToSerial(bool set); +}; + +#endif // SERIALWIDGET_H