commit af5959946361733cbe3c726c7750fc27b4e9b8fd Author: zhangsan Date: Mon Oct 18 14:37:59 2021 +0800 Initial Commit. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7bc2e1c --- /dev/null +++ b/.gitignore @@ -0,0 +1,53 @@ +# ---> Qt +# C++ objects and libs +*.slo +*.lo +*.o +*.a +*.la +*.lai +*.so +*.so.* +*.dll +*.dylib + +# Qt-es +object_script.*.Release +object_script.*.Debug +*_plugin_import.cpp +/.qmake.cache +/.qmake.stash +*.pro.user +*.pro.user.* +*.qbs.user +*.qbs.user.* +*.moc +moc_*.cpp +moc_*.h +qrc_*.cpp +ui_*.h +*.qmlc +*.jsc +Makefile* +*build-* +*.qm +*.prl + +# Qt unit tests +target_wrapper.* + +# QtCreator +*.autosave + +# QtCreator Qml +*.qmlproject.user +*.qmlproject.user.* + +# QtCreator CMake +CMakeLists.txt.user* + +# QtCreator 4.8< compilation database +compile_commands.json + +# QtCreator local machine specific files for imported projects +*creator.user* diff --git a/QCPShow.pro b/QCPShow.pro new file mode 100644 index 0000000..5005623 --- /dev/null +++ b/QCPShow.pro @@ -0,0 +1,51 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2021-10-18T14:20:08 +# +#------------------------------------------------- + +QT += core gui +QT += serialport network + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = QCPShow +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which as 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 + + +SOURCES += main.cpp\ + mainwidget.cpp \ + serial/serialport.cpp \ + ccwidgets/cqlineedit.cpp \ + ccwidgets/cqspinbox.cpp \ + ccwidgets/cqtextedit.cpp \ + ccwidgets/cqwidgetspinbox.cpp \ + util/commonutil.cpp + +HEADERS += mainwidget.h \ + serial/serialport.h \ + ccwidgets/cqlineedit.h \ + ccwidgets/cqspinbox.h \ + ccwidgets/cqtextedit.h \ + ccwidgets/cqwidgetspinbox.h \ + util/commonutil.h + +FORMS += mainwidget.ui + +RESOURCES += \ + resource.qrc + +DISTFILES += \ + .gitignore \ + ReadMe.md diff --git a/QCPShow.pro.user b/QCPShow.pro.user new file mode 100644 index 0000000..aecb588 --- /dev/null +++ b/QCPShow.pro.user @@ -0,0 +1,318 @@ + + + + + + EnvironmentId + {fe03815e-74e2-457e-b60c-e66368cd4ce4} + + + 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 + + + + ProjectExplorer.Project.Target.0 + + Desktop Qt 5.8.0 MinGW 32bit + Desktop Qt 5.8.0 MinGW 32bit + qt.58.win32_mingw53_kit + 0 + 0 + 0 + + E:/Qt/QtSpace/build-QcpShow-Desktop_Qt_5_8_0_MinGW_32bit-Debug + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + false + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + + Qt4ProjectManager.Qt4BuildConfiguration + 2 + true + + + E:/Qt/QtSpace/build-QcpShow-Desktop_Qt_5_8_0_MinGW_32bit-Release + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + + false + false + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + + E:/Qt/QtSpace/build-QcpShow-Desktop_Qt_5_8_0_MinGW_32bit-Profile + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + true + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Profile + + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + 3 + + + 0 + Deploy + + ProjectExplorer.BuildSteps.Deploy + + 1 + Deploy locally + + 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 + + QcpShow + + Qt4ProjectManager.Qt4RunConfiguration:E:/Qt/QtSpace/QcpShow/QcpShow.pro + true + + QcpShow.pro + false + + + 3768 + false + true + false + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 18 + + + Version + 18 + + diff --git a/ReadMe.md b/ReadMe.md new file mode 100644 index 0000000..aff71c5 --- /dev/null +++ b/ReadMe.md @@ -0,0 +1,3 @@ +## 绿道窗口显示程序 + +//TODO \ No newline at end of file diff --git a/ccwidgets/cqlineedit.cpp b/ccwidgets/cqlineedit.cpp new file mode 100644 index 0000000..55b64b3 --- /dev/null +++ b/ccwidgets/cqlineedit.cpp @@ -0,0 +1,18 @@ +#include "cqlineedit.h" + +CQLineEdit::CQLineEdit(QWidget *parent) : QLineEdit(parent) +{ + this->setFocusPolicy(Qt::StrongFocus); +} + +void CQLineEdit::focusInEvent(QFocusEvent *event) +{ + QLineEdit::focusInEvent(event); + emit GetFocus(); +} + +void CQLineEdit::focusOutEvent(QFocusEvent *event) +{ + QLineEdit::focusOutEvent(event); + emit LostFocus(this->text()); +} diff --git a/ccwidgets/cqlineedit.h b/ccwidgets/cqlineedit.h new file mode 100644 index 0000000..57cd024 --- /dev/null +++ b/ccwidgets/cqlineedit.h @@ -0,0 +1,18 @@ +#ifndef CQLINEEDIT_H +#define CQLINEEDIT_H + +#include + +class CQLineEdit : public QLineEdit +{ + Q_OBJECT +public: + explicit CQLineEdit(QWidget *parent = 0); + void focusInEvent(QFocusEvent *event); //覆盖这两个函数 + void focusOutEvent(QFocusEvent *event); +signals: + void GetFocus(); //获得焦点信号 + void LostFocus(const QString &); //失去焦点信号 +}; + +#endif // CQLINEEDIT_H diff --git a/ccwidgets/cqspinbox.cpp b/ccwidgets/cqspinbox.cpp new file mode 100644 index 0000000..e08dc2a --- /dev/null +++ b/ccwidgets/cqspinbox.cpp @@ -0,0 +1,19 @@ +#include "cqspinbox.h" + +CQSpinBox::CQSpinBox(QWidget *parent) : QSpinBox(parent) +{ +// this->setFocusPolicy(Qt::StrongFocus); +} + +void CQSpinBox::focusInEvent(QFocusEvent *event) +{ + QSpinBox::focusInEvent(event); + emit GetFocus(); +} + +void CQSpinBox::focusOutEvent(QFocusEvent *event) +{ + QSpinBox::focusOutEvent(event); + emit LostFocus(this->text()); +} + diff --git a/ccwidgets/cqspinbox.h b/ccwidgets/cqspinbox.h new file mode 100644 index 0000000..5c25e59 --- /dev/null +++ b/ccwidgets/cqspinbox.h @@ -0,0 +1,20 @@ +#ifndef CQSPINBOX_H +#define CQSPINBOX_H + +#include +#include + +class CQSpinBox : public QSpinBox +{ + Q_OBJECT +public: + explicit CQSpinBox(QWidget *parent = 0); + void focusInEvent(QFocusEvent *event); //覆盖这两个函数 + void focusOutEvent(QFocusEvent *event); + +signals: + void GetFocus(); //获得焦点信号 + void LostFocus(const QString &); //失去焦点信号 +}; + +#endif // CQLINEEDIT_H diff --git a/ccwidgets/cqtextedit.cpp b/ccwidgets/cqtextedit.cpp new file mode 100644 index 0000000..16ee123 --- /dev/null +++ b/ccwidgets/cqtextedit.cpp @@ -0,0 +1,18 @@ +#include "cqtextedit.h" + +CQTextEdit::CQTextEdit(QWidget *parent) : QTextEdit(parent) +{ + this->setFocusPolicy(Qt::StrongFocus); +} + +void CQTextEdit::focusInEvent(QFocusEvent *event) +{ + QTextEdit::focusInEvent(event); + emit GetFocus(); +} + +void CQTextEdit::focusOutEvent(QFocusEvent *event) +{ + QTextEdit::focusOutEvent(event); + emit LostFocus(this->toPlainText()); +} diff --git a/ccwidgets/cqtextedit.h b/ccwidgets/cqtextedit.h new file mode 100644 index 0000000..42d9e81 --- /dev/null +++ b/ccwidgets/cqtextedit.h @@ -0,0 +1,18 @@ +#ifndef CQTEXTEDIT_H +#define CQTEXTEDIT_H + +#include + +class CQTextEdit : public QTextEdit +{ + Q_OBJECT +public: + explicit CQTextEdit(QWidget *parent = 0); + void focusInEvent(QFocusEvent *event); //覆盖这两个函数 + void focusOutEvent(QFocusEvent *event); +signals: + void GetFocus(); //获得焦点信号 + void LostFocus(const QString &); //失去焦点信号 +}; + +#endif // CQTEXTEDIT_H diff --git a/ccwidgets/cqwidgetspinbox.cpp b/ccwidgets/cqwidgetspinbox.cpp new file mode 100644 index 0000000..6e06afa --- /dev/null +++ b/ccwidgets/cqwidgetspinbox.cpp @@ -0,0 +1,44 @@ +#include "cqwidgetspinbox.h" + +CQWidgetSpinBox::CQWidgetSpinBox(QWidget *parent) : QWidget(parent) +{ +// this->setFocusPolicy(Qt::StrongFocus); +// this->setStyleSheet(QString::fromUtf8("border:1px solid")); + this->init(); +} + +void CQWidgetSpinBox::focusInEvent(QFocusEvent *event) +{ + QWidget::focusInEvent(event); + emit GetFocus(); +} + +void CQWidgetSpinBox::focusOutEvent(QFocusEvent *event) +{ + QWidget::focusOutEvent(event); + emit LostFocus(); +} + +void CQWidgetSpinBox::init() +{ + m_lineEdit = new QLineEdit; + m_toolButtonUp = new QToolButton; + m_toolButtonDown = new QToolButton; + m_hLayout = new QHBoxLayout; + m_vLayout = new QVBoxLayout; + +// m_lineEdit->setStyleSheet("color: rgb(255, 255, 255);font: 10pt 'Adobe 黑体 Std R'';"); +// m_lineEdit->setFixedSize(60,20);; + m_toolButtonUp->setFixedSize(15,11); + m_toolButtonDown->setFixedSize(15,11); + m_vLayout->addWidget(m_toolButtonUp); + m_vLayout->addWidget(m_toolButtonDown); + m_hLayout->addWidget(m_lineEdit); + m_hLayout->addLayout(m_vLayout); + setContentsMargins(0,0,0,0); + m_vLayout->setSpacing(0); + m_vLayout->setMargin(0); + m_hLayout->setSpacing(0); + m_hLayout->setMargin(0); + setLayout(m_hLayout); +} diff --git a/ccwidgets/cqwidgetspinbox.h b/ccwidgets/cqwidgetspinbox.h new file mode 100644 index 0000000..f6e2a4a --- /dev/null +++ b/ccwidgets/cqwidgetspinbox.h @@ -0,0 +1,36 @@ +#ifndef CQWIDGETSPINBOX_H +#define CQWIDGETSPINBOX_H + +#include +#include +#include +#include +#include + +class CQWidgetSpinBox : public QWidget +{ + Q_OBJECT +public: + explicit CQWidgetSpinBox(QWidget *parent = 0); + + void focusInEvent(QFocusEvent *event); //覆盖这两个函数 + void focusOutEvent(QFocusEvent *event); + void setText(QString s); + QString text(); + +private: + QLineEdit *m_lineEdit; + QToolButton *m_toolButtonUp, *m_toolButtonDown; + QHBoxLayout *m_hLayout; + QVBoxLayout *m_vLayout; + + void init(); + +signals: + void GetFocus(); //获得焦点信号 + void LostFocus(); //失去焦点信号 + void UpClicked(); + void DownClicked(); +}; + +#endif // CQWIDGETSPINBOX_H diff --git a/image/audio.png b/image/audio.png new file mode 100644 index 0000000..88006d5 Binary files /dev/null and b/image/audio.png differ diff --git a/image/back.png b/image/back.png new file mode 100644 index 0000000..c6d72d7 Binary files /dev/null and b/image/back.png differ diff --git a/image/back1.png b/image/back1.png new file mode 100644 index 0000000..278abb4 Binary files /dev/null and b/image/back1.png differ diff --git a/image/close_normal.png b/image/close_normal.png new file mode 100644 index 0000000..5248025 Binary files /dev/null and b/image/close_normal.png differ diff --git a/image/header.png b/image/header.png new file mode 100644 index 0000000..9547fe9 Binary files /dev/null and b/image/header.png differ diff --git a/image/open_focus.png b/image/open_focus.png new file mode 100644 index 0000000..af86019 Binary files /dev/null and b/image/open_focus.png differ diff --git a/image/open_normal.png b/image/open_normal.png new file mode 100644 index 0000000..ccbe2c5 Binary files /dev/null and b/image/open_normal.png differ diff --git a/image/pause1_normal.png b/image/pause1_normal.png new file mode 100644 index 0000000..365f320 Binary files /dev/null and b/image/pause1_normal.png differ diff --git a/image/pause_focus.png b/image/pause_focus.png new file mode 100644 index 0000000..38e35ac Binary files /dev/null and b/image/pause_focus.png differ diff --git a/image/pause_normal.png b/image/pause_normal.png new file mode 100644 index 0000000..0b22991 Binary files /dev/null and b/image/pause_normal.png differ diff --git a/image/showmaxsizebtn.png b/image/showmaxsizebtn.png new file mode 100644 index 0000000..1a9e9a3 Binary files /dev/null and b/image/showmaxsizebtn.png differ diff --git a/image/showminisizebtn.png b/image/showminisizebtn.png new file mode 100644 index 0000000..b0b1202 Binary files /dev/null and b/image/showminisizebtn.png differ diff --git a/image/shownormalbtn.png b/image/shownormalbtn.png new file mode 100644 index 0000000..a53bf08 Binary files /dev/null and b/image/shownormalbtn.png differ diff --git a/image/shutdown.png b/image/shutdown.png new file mode 100644 index 0000000..4927516 Binary files /dev/null and b/image/shutdown.png differ diff --git a/image/start1_normal.png b/image/start1_normal.png new file mode 100644 index 0000000..a64d1c7 Binary files /dev/null and b/image/start1_normal.png differ diff --git a/image/start_focus.png b/image/start_focus.png new file mode 100644 index 0000000..cdc71eb Binary files /dev/null and b/image/start_focus.png differ diff --git a/image/start_normal.png b/image/start_normal.png new file mode 100644 index 0000000..a87f25c Binary files /dev/null and b/image/start_normal.png differ diff --git a/image/stop1_normal.png b/image/stop1_normal.png new file mode 100644 index 0000000..baad7a0 Binary files /dev/null and b/image/stop1_normal.png differ diff --git a/image/stop_focus.png b/image/stop_focus.png new file mode 100644 index 0000000..25358d4 Binary files /dev/null and b/image/stop_focus.png differ diff --git a/image/stop_normal.png b/image/stop_normal.png new file mode 100644 index 0000000..7916b9e Binary files /dev/null and b/image/stop_normal.png differ diff --git a/image/volume.psd b/image/volume.psd new file mode 100644 index 0000000..b546b82 Binary files /dev/null and b/image/volume.psd differ diff --git a/image/volume_mute.png b/image/volume_mute.png new file mode 100644 index 0000000..1760319 Binary files /dev/null and b/image/volume_mute.png differ diff --git a/image/volume_normal.png b/image/volume_normal.png new file mode 100644 index 0000000..fbb5c2b Binary files /dev/null and b/image/volume_normal.png differ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..d93db8c --- /dev/null +++ b/main.cpp @@ -0,0 +1,11 @@ +#include "mainwidget.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWidget w; + w.show(); + + return a.exec(); +} diff --git a/mainwidget.cpp b/mainwidget.cpp new file mode 100644 index 0000000..4c35308 --- /dev/null +++ b/mainwidget.cpp @@ -0,0 +1,14 @@ +#include "mainwidget.h" +#include "ui_mainwidget.h" + +MainWidget::MainWidget(QWidget *parent) : + QWidget(parent), + ui(new Ui::MainWidget) +{ + ui->setupUi(this); +} + +MainWidget::~MainWidget() +{ + delete ui; +} diff --git a/mainwidget.h b/mainwidget.h new file mode 100644 index 0000000..bc6b7fa --- /dev/null +++ b/mainwidget.h @@ -0,0 +1,22 @@ +#ifndef MAINWIDGET_H +#define MAINWIDGET_H + +#include + +namespace Ui { +class MainWidget; +} + +class MainWidget : public QWidget +{ + Q_OBJECT + +public: + explicit MainWidget(QWidget *parent = 0); + ~MainWidget(); + +private: + Ui::MainWidget *ui; +}; + +#endif // MAINWIDGET_H diff --git a/mainwidget.ui b/mainwidget.ui new file mode 100644 index 0000000..f96e990 --- /dev/null +++ b/mainwidget.ui @@ -0,0 +1,20 @@ + + MainWidget + + + + 0 + 0 + 400 + 300 + + + + MainWidget + + + + + + + diff --git a/resource.qrc b/resource.qrc new file mode 100644 index 0000000..e137aa3 --- /dev/null +++ b/resource.qrc @@ -0,0 +1,5 @@ + + + image/header.png + + diff --git a/serial/serialport.cpp b/serial/serialport.cpp new file mode 100644 index 0000000..b635906 --- /dev/null +++ b/serial/serialport.cpp @@ -0,0 +1,149 @@ +#include "serialport.h" +#include "util/commonutil.h" +#include + +SerialPort::SerialPort(QObject *parent) + :QObject(parent) +{ + m_serial = Q_NULLPTR; + m_connected = false; + m_rbuffer.clear(); + m_wbuffer.clear(); + m_currentPortName = ""; + m_currentBaudRate = ""; +} + +SerialPort::~SerialPort(){} + +/** + * 8N1 (8个数据位,1个停止位,无校验) + * @brief SerialPort::openSerialPort + * @param serialName + * @param baudrate + */ + +#if 0 +bool SerialPort::openSerialPort(QString serialName, quint32 baudrate) +{ + qDebug() << "QSeriaport Attempting connect to " << serialName << "," << baudrate; + //如果已经打开,先执行关闭操作 + disConnected(); + + //记录当前正在使用的串口信息 + m_currentPortName = serialName; + m_currentBaudRate = QString::number(baudrate); + + //设置串口属性,并且打开 + m_serial = new QSerialPort(); + //connect(m_serial,SIGNAL(readyRead()),this,SLOT(readData)); + connect(m_serial,&QSerialPort::readyRead,this,&SerialPort::readData); + connect(m_serial,SIGNAL(error(QSerialPort::SerialPortError)),this, + SLOT(onError(QSerialPort::SerialPortError))); + + m_serial->setPortName((serialName)); + m_serial->setBaudRate(baudrate); + m_serial->setDataBits(QSerialPort::Data8); + m_serial->setParity(QSerialPort::NoParity); + m_serial->setStopBits(QSerialPort::OneStop); + m_serial->setFlowControl(QSerialPort::NoFlowControl); + + if(!m_serial->open(QIODevice::ReadWrite)){ + qCritical() << "Open Serial failed: " << m_serial->errorString(); + return false; + } + + m_connected = true; + qDebug() << "QSerialPort connected to " << serialName << "," << baudrate; + return true; +} +#else +bool SerialPort::openSerialPort(QString serialName, quint32 baudrate, QSerialPort::DataBits dataBits, QSerialPort::StopBits stopbits, QSerialPort::Parity parity, QSerialPort::FlowControl flowControl) +{ + qDebug() << "QSeriaport Attempting connect to " << serialName << "," << baudrate; + //如果已经打开,先执行关闭操作 + disConnected(); + + //记录当前正在使用的串口信息 + m_currentPortName = serialName; + m_currentBaudRate = QString::number(baudrate); + + //设置串口属性,并且打开 + m_serial = new QSerialPort(); + //connect(m_serial,SIGNAL(readyRead()),this,SLOT(readData)); + connect(m_serial,&QSerialPort::readyRead,this,&SerialPort::readData); + connect(m_serial,SIGNAL(error(QSerialPort::SerialPortError)),this, + SLOT(onError(QSerialPort::SerialPortError))); + + m_serial->setPortName((serialName)); + m_serial->setBaudRate(baudrate); + m_serial->setDataBits(dataBits); + m_serial->setParity(parity); + m_serial->setStopBits(stopbits); + m_serial->setFlowControl(flowControl); +// if(dataBits == QSerialPort::Data6){ +// qDebug()<<"Data6"; +// } + + if(!m_serial->open(QIODevice::ReadWrite)){ + qCritical() << "Open Serial failed: " << m_serial->errorString(); + return false; + } + + m_connected = true; + qDebug() << "QSerialPort connected to " << serialName << "," << baudrate; + return true; +} +#endif +void SerialPort::disConnected() +{ + m_connected = false; + if(m_serial != Q_NULLPTR) + { + if(m_serial->isOpen()){ + m_serial->close(); + } + m_serial->deleteLater(); + m_serial = Q_NULLPTR; + qDebug() << "disconnect from " << m_currentPortName << "," << m_currentBaudRate; + } +} + +bool SerialPort::isConnected() +{ + return m_connected; +} + +qint64 SerialPort::writeData(QByteArray &data) +{ + if(m_connected == false) + { + return 0; + } + //write本身并不保证将数据全部写入,它只返回实际写入的字节数 + qint64 total = data.size(), writed = 0,n = 0; + while(writed < total) + { + if((n = m_serial->write(data.mid(writed))) < 0){ + break; + } + writed += n; + } + qDebug() << QDateTime::currentMSecsSinceEpoch() << " " << "Serial send: " << writed << "," << CommonUtil::ByteArrayToHexString(data); + return writed; +} + +void SerialPort::readData() +{ + QByteArray ba = m_serial->readAll(); + emit serialReadData(ba); + //添加协议 + //qDebug() << ba; +} + +void SerialPort::onError(QSerialPort::SerialPortError error) +{ + if(error != QSerialPort::NoError) + { + qCritical() << "SerialError:" << m_serial->errorString(); + } +} diff --git a/serial/serialport.h b/serial/serialport.h new file mode 100644 index 0000000..19c8b5f --- /dev/null +++ b/serial/serialport.h @@ -0,0 +1,36 @@ +#ifndef SERIALPORT_H +#define SERIALPORT_H + +#include + +/** + * 串口通讯 + * @brief The SerialPort class + */ +class SerialPort : public QObject +{ + Q_OBJECT +public: + SerialPort(QObject *parent = 0); + ~SerialPort(); +private: + QString m_currentPortName; //当前串口名 + QString m_currentBaudRate; //当前串口波特率 + QSerialPort *m_serial; //Qt串口对象 + bool m_connected; //是否连接标志 + QByteArray m_rbuffer; //读缓冲区 + QByteArray m_wbuffer; //写缓冲区 +public: + bool openSerialPort(QString serialName,quint32 baudrate); + bool openSerialPort(QString serialName, quint32 baudrate, QSerialPort::DataBits dataBits, QSerialPort::StopBits stopbits, QSerialPort::Parity parity, QSerialPort::FlowControl flowControl); + void disConnected(); + bool isConnected(); +public slots: + void onError(QSerialPort::SerialPortError error); + void readData(); + qint64 writeData(QByteArray &data); +signals: + void serialReadData(QByteArray &data); +}; + +#endif // SERIALPORT_H diff --git a/util/commonutil.cpp b/util/commonutil.cpp new file mode 100644 index 0000000..08a409a --- /dev/null +++ b/util/commonutil.cpp @@ -0,0 +1,233 @@ +#include "commonutil.h" +#include +#include +#include + +CommonUtil::CommonUtil() +{ + +} + +QByteArray CommonUtil::HexStringToByteArray(QString HexString) +{ + bool ok; + QByteArray ret; + HexString = HexString.trimmed(); + HexString = HexString.simplified(); + QStringList sl = HexString.split(" "); + + foreach (QString s, sl) { + if(!s.isEmpty()) { + char c = s.toInt(&ok,16)&0xFF; + if(ok){ + ret.append(c); + }else{ + qDebug()<<"非法的16进制字符:"<> outChar; //每次一个字节的填充到 outchar + QString str = QString("%1").arg(outChar & 0xFF,2,16,QLatin1Char('0')).toUpper() + QString(" "); //2 字符宽度 + buf += str; + } + return buf; +} + +QString CommonUtil::ByteArrayToNonZeroString(const QByteArray &ba) +{ + QDataStream out(ba); //将str的数据 读到out里面去 + QString buf; + while(!out.atEnd()) + { + qint8 outChar = 0; + out >> outChar; //每次一个字节的填充到 outchar + QString str = outChar != 0 ? QString(QChar(outChar)) : "\\0"; + buf += str; + } + return buf; +} + + +QByteArray CommonUtil::quint8ArrToQByteArray(quint8 *arr,int lengths) +{ + QByteArray ba; + for(int i=0;i= I_1mA) + { + return QString("%1mA").arg(ipa * 100 / I_1mA / 100.0); + } + else if(ipa >= I_1uA) + { + return QString("%1uA").arg(ipa * 100 / I_1uA / 100.0); + } + else if(ipa >= I_1nA) + { + return QString("%1nA").arg(ipa * 100 / I_1nA / 100.0); + } + else if(ipa >= I_1pA) + { + return QString("%1pA").arg(ipa * 100 / I_1pA / 100.0); + } + return "0"; +} + +void CommonUtil::crc16(quint8 crc[], quint8 data[],int pos, int len) +{ + int wcrc = 0xFFFF;// 预置16位crc寄存器,初值全部为1 + int temp;// 定义中间变量 + int i = 0, j = 0, k = pos;// 定义计数 + + for (i = 0; i < len; i++)// 循环计算每个数据 + { + temp = (data[k] & 0xFF);// 将八位数据与crc寄存器亦或 + k++;// 指针地址增加,指向下个数据 + wcrc ^= temp;// 将数据存入crc寄存器 + for (j = 0; j < 8; j++)// 循环计算数据的 + { + if ((wcrc & 0x0001) == 1)// 判断右移出的是不是1,如果是1则与多项式进行异或。 + { + wcrc >>= 1;// 先将数据右移一位 + wcrc ^= 0XA001;// 与上面的多项式进行异或 + } else// 如果不是1,则直接移出 + { + wcrc >>= 1;// 直接移出 + } + } + } + temp = wcrc;// crc的值 + crc[0] = wcrc & 0xFF; // crc的低八位 + crc[1] = (wcrc >> 8) & 0xFF; // crc的高八位 +} + + +//判断是否大端字节序 +int CommonUtil::isBigEndianOrder() +{ + int iVal = 1; + char *pChar = (char*)(&iVal); + if(*pChar == 1) //低字节在低地址(小端) + return false; + return true; //(0x00000001) 低字节在高地址(大端) Aix采用的是大端法 +} + +#ifdef Q_OS_WIN +quint32 CommonUtil::Lc_ntohl(quint32 data) +{ + quint32 res = 0; + if(data == 0 || isBigEndianOrder()){ + res = data; + }else{ + res = (((data >> 0) & 0xFF) << 24) | (((data >> 8) & 0xFF) << 16) | (((data >> 16) & 0xFF) << 8) | (((data >> 24) & 0xFF) << 0); + } + return res; +} + +quint32 CommonUtil::Lc_htonl(quint32 data) +{ + quint32 res = 0; + if(data == 0 || isBigEndianOrder()){ + res = data; + }else{ + res = (((data >> 0) & 0xFF) << 24) | (((data >> 8) & 0xFF) << 16) | (((data >> 16) & 0xFF) << 8) | (((data >> 24) & 0xFF) << 0); + } + return res; +} + +quint16 CommonUtil::Lc_ntohs(quint16 data) +{ + quint16 res = 0; + if(data == 0 || isBigEndianOrder()){ + res = data; + }else{ + res = (((data >> 0) & 0xFF) << 8) | (((data >> 8) & 0xFF) << 0) ; + } + return res; +} + +quint16 CommonUtil::Lc_htons(quint16 data) +{ + quint16 res = 0; + if(data == 0 || isBigEndianOrder()){ + res = data; + }else{ + res = (((data >> 0) & 0xFF) << 8) | (((data >> 8) & 0xFF) << 0) ; + } + return res; +} +#endif + +void CommonUtil::Lc_buf_copy_uc(quint8 *d,const quint8 *s,quint32 num) +{ + quint32 i; + for(i = 0; i < num; i ++) + { + *(d+i) = *(s+i); + } +} + +void CommonUtil::delay(quint32 n) +{ + while(n--); +} + +QString CommonUtil::getHostIpAddress() +{ + QString strIpAddress; + QList ipAddressesList = QNetworkInterface::allAddresses(); + // 获取第一个本主机的IPv4地址 + int nListSize = ipAddressesList.size(); + for (int i = 0; i < nListSize; ++i) + { + if (ipAddressesList.at(i) != QHostAddress::LocalHost && + ipAddressesList.at(i).toIPv4Address()) { + strIpAddress = ipAddressesList.at(i).toString(); + break; + } + } + // 如果没有找到,则以本地IP地址为IP + if (strIpAddress.isEmpty()) + strIpAddress = QHostAddress(QHostAddress::LocalHost).toString(); + return strIpAddress; +} + +QString CommonUtil::getHostMacAddress() +{ + QList nets = QNetworkInterface::allInterfaces();// 获取所有网络接口列表 + int nCnt = nets.count(); + QString strMacAddr = ""; + for(int i = 0; i < nCnt; i ++) + { + // 如果此网络接口被激活并且正在运行并且不是回环地址,则就是我们需要找的Mac地址 + if(nets[i].flags().testFlag(QNetworkInterface::IsUp) && nets[i].flags().testFlag(QNetworkInterface::IsRunning) && !nets[i].flags().testFlag(QNetworkInterface::IsLoopBack)) + { + strMacAddr = nets[i].hardwareAddress(); + break; + } + } + return strMacAddr; +} diff --git a/util/commonutil.h b/util/commonutil.h new file mode 100644 index 0000000..812c624 --- /dev/null +++ b/util/commonutil.h @@ -0,0 +1,30 @@ +#ifndef COMMONUTIL_H +#define COMMONUTIL_H + +#include + +class CommonUtil +{ +public: + CommonUtil(); + static QByteArray quint8ArrToQByteArray(quint8 *arr,int lengths); + static void crc16(quint8 crc[], quint8 data[], int pos,int len); + static int isBigEndianOrder(); +#ifdef Q_OS_WIN + static quint32 Lc_ntohl(quint32 data); + static quint32 Lc_htonl(quint32 data); + static quint16 Lc_ntohs(quint16 data); + static quint16 Lc_htons(quint16 data); +#endif + static void Lc_buf_copy_uc(quint8 *d,const quint8 *s,quint32 num); + static void delay(quint32 n); + static QString getHostIpAddress(); + static QString getHostMacAddress(); + static QString ByteArrayToHexString(const QByteArray &ba); + static QByteArray HexStringToByteArray(QString HexString); + + static QString ByteArrayToNonZeroString(const QByteArray &ba); + static QString i_pA_2_text(unsigned int iPA); +}; + +#endif // COMMONUTIL_H