Compare commits
2 Commits
7d71a40b15
...
3c0ba621ac
Author | SHA1 | Date | |
---|---|---|---|
3c0ba621ac | |||
15580116d1 |
58
main.cpp
Normal file
58
main.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
#include <QCoreApplication>
|
||||
#include <QThread>
|
||||
|
||||
#include "witmotiondll.h"
|
||||
#include "qtserialport.h"
|
||||
|
||||
void delay_tc(uint32_t millisecond)
|
||||
{
|
||||
QThread::sleep(millisecond/1000);
|
||||
std::cout<<"This is delay_tc!!!!!!!!!!"<<std::endl;
|
||||
}
|
||||
|
||||
void printf_tc(const char* text)
|
||||
{
|
||||
printf(text);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication a(argc, argv);
|
||||
|
||||
SerialPortBase * serialPort = new QtSerialport();
|
||||
// SerialPortBase * serialPort = new windowsSerialPort();
|
||||
|
||||
serialPort->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();
|
||||
}
|
115
qtserialport.cpp
Normal file
115
qtserialport.cpp
Normal file
@ -0,0 +1,115 @@
|
||||
#include "qtserialport.h"
|
||||
|
||||
int QtSerialport::OpenSerialPort(string portName, int baudrate)
|
||||
{
|
||||
QList<QSerialPortInfo> infos = QSerialPortInfo::availablePorts();
|
||||
|
||||
for (int i=0; i<infos.length();i++)
|
||||
{
|
||||
qDebug()<<infos[i].portName();
|
||||
// std::cout<<infos[i].portName()<<std::endl;
|
||||
}
|
||||
|
||||
//std::cout<<"number of availablePorts:"<<infos.size()<<std::endl;
|
||||
|
||||
//如果在构造函数中创建m_serial就会出现错误:
|
||||
//QObject: Cannot create children for a parent that is in a different thread.
|
||||
//(Parent is QSerialPort(0x2e31b20), parent's thread is QThread(0x2e2f130), current thread is QThread(0x2e31110)
|
||||
m_serial = new QSerialPort();
|
||||
|
||||
if(m_serial->isOpen())//如果串口已经打开了 先给他关闭了
|
||||
{
|
||||
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<<"波特率被成功设置为:"<<m_serial->baudRate()<<std::endl;
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout<<"波特率设置失败!"<<std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int QtSerialport::CloseSerialPort()
|
||||
{
|
||||
m_serial->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--------------:"<<std::endl;
|
||||
if(m_serial->waitForReadyRead())
|
||||
{
|
||||
//requestData.resize(m_serial->size());
|
||||
requestData = m_serial->readAll();
|
||||
std::cout<<"size: "<< requestData.size() <<std::endl;
|
||||
fwrite(requestData.data(),requestData.size(),1,fileHandle);
|
||||
// fflush(fileHandle);
|
||||
|
||||
// if(!m_bIsSbgReady)
|
||||
// {
|
||||
// parseSbgMessage(&requestData);
|
||||
// }
|
||||
// parseSbgMessage(&requestData);//边采集惯导数据边解析,并不会导致惯导漏帧
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout<<"SbgRecorder::startRecordSbg----:Wait write response timeout"<<std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
36
qtserialport.h
Normal file
36
qtserialport.h
Normal file
@ -0,0 +1,36 @@
|
||||
#ifndef QTSERIALPORT_H
|
||||
#define QTSERIALPORT_H
|
||||
|
||||
#include "serialportbase.h"
|
||||
|
||||
#include <QtSerialPort/QSerialPort>
|
||||
#include <QtSerialPort/QSerialPortInfo>
|
||||
#include <qdebug.h>
|
||||
#include <QDebug>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
|
||||
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
|
@ -17,7 +17,7 @@ public:
|
||||
virtual int SetBaudrate(int baudrate) = 0;
|
||||
virtual int SendData1(const char *data, const unsigned int len) = 0;
|
||||
virtual int SendData(const char chrSendBuffer[],const unsigned short usLen) = 0;
|
||||
virtual int ReadData() = 0;
|
||||
virtual int ReadData(char * receivedData) = 0;
|
||||
protected:
|
||||
private:
|
||||
};
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
@ -168,7 +168,8 @@ int WitmotionDll::setAngleReference()
|
||||
|
||||
void WitmotionDll::recordData()
|
||||
{
|
||||
m_SerialPort->ReadData();
|
||||
char * receivedData;
|
||||
m_SerialPort->ReadData(receivedData);
|
||||
}
|
||||
|
||||
int WitmotionDll::setTimeZone(TIMEZONE_ENUM timeZone)
|
||||
|
@ -1,8 +1,6 @@
|
||||
#ifndef WITMOTIONDLL_H
|
||||
#define WITMOTIONDLL_H
|
||||
|
||||
#include "witmotiondll_global.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
@ -13,7 +11,7 @@
|
||||
typedef void (*delay)(uint32_t millisecond);
|
||||
typedef void (*witPrintf)(const char* text);
|
||||
|
||||
class WITMOTIONDLLSHARED_EXPORT WitmotionDll
|
||||
class WitmotionDll
|
||||
{
|
||||
|
||||
public:
|
||||
|
@ -1,12 +0,0 @@
|
||||
#ifndef WITMOTIONDLL_GLOBAL_H
|
||||
#define WITMOTIONDLL_GLOBAL_H
|
||||
|
||||
#include <QtCore/qglobal.h>
|
||||
|
||||
#if defined(WITMOTIONDLL_LIBRARY)
|
||||
# define WITMOTIONDLLSHARED_EXPORT Q_DECL_EXPORT
|
||||
#else
|
||||
# define WITMOTIONDLLSHARED_EXPORT Q_DECL_IMPORT
|
||||
#endif
|
||||
|
||||
#endif // WITMOTIONDLL_GLOBAL_H
|
Reference in New Issue
Block a user