From 3c0ba621ac3903bdab41beabdf280185d38de11c Mon Sep 17 00:00:00 2001 From: tangchao0503 <735056338@qq.com> Date: Mon, 20 Jun 2022 15:30:16 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=B0=86=E5=B7=A5=E7=A8=8B=E4=BB=8E?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E5=BA=93=E5=8F=98=E4=B8=BA=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=A1=8C=E7=A8=8B=E5=BA=8F=EF=BC=9A=E4=BF=AE=E6=94=B9pro?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E3=80=81=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6?= =?UTF-8?q?witmotiondll=5Fglobal.h=EF=BC=9B=202=E3=80=81=E7=BB=A7=E6=89=BF?= =?UTF-8?q?=E4=B8=B2=E5=8F=A3=E5=9F=BA=E7=B1=BBSerialPortBase=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E4=B8=B2=E5=8F=A3=E7=B1=BBQtSerialport=EF=BC=9B=203?= =?UTF-8?q?=E3=80=81=E5=86=99=E7=BB=B4=E7=89=B9=E6=83=AF=E5=AF=BC=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E7=A4=BA=E4=BE=8B=E7=A8=8B=E5=BA=8Fmain.cpp=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 58 +++++++++++++++++++++ qtserialport.cpp | 115 ++++++++++++++++++++++++++++++++++++++++++ qtserialport.h | 36 +++++++++++++ witmotionDll.pro | 35 ++++++------- witmotiondll.cpp | 8 +-- witmotiondll.h | 4 +- witmotiondll_global.h | 12 ----- 7 files changed, 230 insertions(+), 38 deletions(-) create mode 100644 main.cpp create mode 100644 qtserialport.cpp create mode 100644 qtserialport.h delete mode 100644 witmotiondll_global.h diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..b7e5d1e --- /dev/null +++ b/main.cpp @@ -0,0 +1,58 @@ +#include +#include + +#include "witmotiondll.h" +#include "qtserialport.h" + +void delay_tc(uint32_t millisecond) +{ + QThread::sleep(millisecond/1000); + std::cout<<"This is delay_tc!!!!!!!!!!"<OpenSerialPort("COM15", 9600); + + WitmotionDll * tmp = new WitmotionDll(serialPort); + tmp->delayMsRegister(delay_tc); + tmp->printfRegister(printf_tc); + tmp->setDelayTimeMs(1000); + + tmp->installationOrientation(ORIENT_VERTICAL); +// tmp->algorithm(ALGROITHM9); +// tmp->setTimeZone(UTC_P12); +// tmp->SetBaudrate(WIT_BAUD_115200); +// tmp->SetReturnRate(RRATE_20HZ); + +// tmp->SetDeviceAddress(124); + +// tmp->setD0Model(DOL); +// tmp->setD0HighLevelPulseWidth(20); +// tmp->setD0Period(5); + +// RETURN_CONTENT_STRUCT content; +// content.time = true; +// content.angular_velocity = true; +// content.euler_angle = true; +// content.ground_velocity = true; +// content.quaternion = true; +// tmp->setContent(content); + + +// tmp->recordData(); + + serialPort->CloseSerialPort(); + +// return a.exec(); +} diff --git a/qtserialport.cpp b/qtserialport.cpp new file mode 100644 index 0000000..0b2d119 --- /dev/null +++ b/qtserialport.cpp @@ -0,0 +1,115 @@ +#include "qtserialport.h" + +int QtSerialport::OpenSerialPort(string portName, int baudrate) +{ + QList infos = QSerialPortInfo::availablePorts(); + + for (int i=0; iisOpen())//如果串口已经打开了 先给他关闭了 + { + m_serial->clear(); + m_serial->close(); + } + + m_serial->setPortName(QString::fromStdString(portName)); + m_serial->open(QIODevice::ReadWrite); + + + bool x=SetBaudrate(baudrate); + if(x) + { + std::cout<<"波特率被成功设置为:"<baudRate()<close(); + return 0; +} + +int QtSerialport::SetBaudrate(int baudrate) +{ + bool x=m_serial->setBaudRate(baudrate); + return 0; +} + +//qint64 write(const char *data, qint64 len); +int QtSerialport::SendData1(const char *data, const unsigned int len) +{ + QByteArray tmp(data, len); + +// QByteArray tmp2 = tmp.toHex(); + + + //QIODevice::write(const char *data, qint64 maxSize) + int num = m_serial->write(tmp); + + bool re = m_serial->waitForBytesWritten(); + + if(re) + { + return num; + } + else + { + return -1; + } + +} + +int QtSerialport::SendData(const char chrSendBuffer[],const unsigned short usLen) +{ + int num = m_serial->write(chrSendBuffer, usLen); + return num; +} + +int QtSerialport::ReadData(char * receivedData) +{ + FILE * fileHandle=fopen("D:\\cpp_qtcreator\\witmotionDll_use-build\\debug\\test.dat","w+b"); + QByteArray requestData; + + while (true) + { + //std::cout<<"SbgRecorder::startRecordSbg--------------:"<waitForReadyRead()) + { + //requestData.resize(m_serial->size()); + requestData = m_serial->readAll(); + std::cout<<"size: "<< requestData.size() < +#include +#include +#include + +#include + + +class QtSerialport :public SerialPortBase +{ +// Q_OBJECT + +public: +// QtSerialport(); +// ~QtSerialport(); + + + int OpenSerialPort(string portName, int baudrate); + int CloseSerialPort(); + int SetBaudrate(int baudrate); + int SendData(const char chrSendBuffer[],const unsigned short usLen); + int SendData1(const char *data, const unsigned int len); + int ReadData(char * receivedData); + +protected: +private: + QSerialPort * m_serial; +}; + + +#endif // QTSERIALPORT_H diff --git a/witmotionDll.pro b/witmotionDll.pro index 6c23056..c1967d6 100644 --- a/witmotionDll.pro +++ b/witmotionDll.pro @@ -1,16 +1,21 @@ -#------------------------------------------------- -# -# Project created by QtCreator 2022-05-25T15:16:09 -# -#------------------------------------------------- +QT += core +QT -= gui +QT += serialport + +CONFIG += c++11 TARGET = witmotionDll -TEMPLATE = lib +CONFIG += console +CONFIG -= app_bundle -DEFINES += WITMOTIONDLL_LIBRARY +TEMPLATE = app + +SOURCES += main.cpp \ + witmotiondll.cpp \ + qtserialport.cpp # The following define makes your compiler emit warnings if you use -# any feature of Qt which as been marked as deprecated (the exact warnings +# any feature of Qt which as been marked 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 @@ -20,16 +25,8 @@ DEFINES += QT_DEPRECATED_WARNINGS # 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 += \ - witmotiondll.cpp - HEADERS += \ - witmotiondll.h \ - witmotiondll_global.h \ + register.h \ serialportbase.h \ - register.h - -unix { - target.path = /usr/lib - INSTALLS += target -} + witmotiondll.h \ + qtserialport.h diff --git a/witmotiondll.cpp b/witmotiondll.cpp index 0a8bb7d..4a4700c 100644 --- a/witmotiondll.cpp +++ b/witmotiondll.cpp @@ -2,12 +2,12 @@ //当没有注册 延时函数(m_delayFunction)时,运行会停止在调用延时函数(m_delayFunction)处; //此函数就是为了解决上面说的问题 -void delay_tc(uint32_t millisecond) +void delay_default(uint32_t millisecond) { ; } -void printf_tc(const char* text) +void printf_default(const char* text) { ; } @@ -16,9 +16,9 @@ WitmotionDll::WitmotionDll(SerialPortBase * serialPort) { m_SerialPort = serialPort; - m_delayFunction = delay_tc; + m_delayFunction = delay_default; - m_witPrintf = printf_tc; + m_witPrintf = printf_default; } int WitmotionDll::delayMsRegister(delay delayFunction) diff --git a/witmotiondll.h b/witmotiondll.h index b6779fd..b83924b 100644 --- a/witmotiondll.h +++ b/witmotiondll.h @@ -1,8 +1,6 @@ #ifndef WITMOTIONDLL_H #define WITMOTIONDLL_H -#include "witmotiondll_global.h" - #include #include #include @@ -13,7 +11,7 @@ typedef void (*delay)(uint32_t millisecond); typedef void (*witPrintf)(const char* text); -class WITMOTIONDLLSHARED_EXPORT WitmotionDll +class WitmotionDll { public: diff --git a/witmotiondll_global.h b/witmotiondll_global.h deleted file mode 100644 index 8177b25..0000000 --- a/witmotiondll_global.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef WITMOTIONDLL_GLOBAL_H -#define WITMOTIONDLL_GLOBAL_H - -#include - -#if defined(WITMOTIONDLL_LIBRARY) -# define WITMOTIONDLLSHARED_EXPORT Q_DECL_EXPORT -#else -# define WITMOTIONDLLSHARED_EXPORT Q_DECL_IMPORT -#endif - -#endif // WITMOTIONDLL_GLOBAL_H