构建新的cmakelist工程

修改了  delay_tc函数
修返回频率为20hz
修改波特率修改后先重新设置通讯波特率在保存
This commit is contained in:
xin
2023-02-13 09:15:17 +08:00
parent d870c42486
commit 277e347e56
12 changed files with 61 additions and 7 deletions

View File

@ -0,0 +1,652 @@
#include "commandlineparser.h"
CommandLineParseResult parseCommandLine2(QCommandLineParser &parser, TcQuery *query, QString *errorMessage)
{
//添加命令
parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
QCommandLineOption helpOption = parser.addHelpOption();//Adds the help option (-h, --help and -? on Windows) This option is handled automatically by QCommandLineParser.
QCommandLineOption versionOption = parser.addVersionOption();//This option is handled automatically by QCommandLineParser.
QCommandLineOption serialPort("serialPort", "Serial port.", "serialPort");
parser.addOption(serialPort);
QCommandLineOption connectBaudrate("connectBaudrate", "ConnectBaudrate of wit motion.", "connectBaudrate");
parser.addOption(connectBaudrate);
QCommandLineOption algorithm("algorithm", "Algorithm of wit motion.", "algorithm");
parser.addOption(algorithm);
QCommandLineOption installationOrientation("installationOrientation", "InstallationOrientation of wit motion.", "installationOrientation");
parser.addOption(installationOrientation);
QCommandLineOption timeZone("timeZone", "TimeZone of wit motion.", "timeZone");
parser.addOption(timeZone);
// A boolean option with multiple names (-r, --record)
QCommandLineOption isSetDefaultReturnContent(QStringList() << "d" << "isSetDefaultReturnContent", "isSetDefaultReturnContent");
parser.addOption(isSetDefaultReturnContent);
QCommandLineOption baudrate("baudrate", "Baudrate of wit motion.", "baudrate");
parser.addOption(baudrate);
QCommandLineOption returnRate("returnRate", "ReturnRate of wit motion.", "returnRate");
parser.addOption(returnRate);
QCommandLineOption deviceAddress("deviceAddress", "DeviceAddress of wit motion.", "deviceAddress");
parser.addOption(deviceAddress);
QCommandLineOption modelForInterface_D0("modelForInterface_D0", "modelForInterface_D0 of interface number.", "modelForInterface_D0");
parser.addOption(modelForInterface_D0);
QCommandLineOption pulseWidth_D0("pulseWidth_D0", "PulseWidth_D0 of interface number.", "pulseWidth_D0");
pulseWidth_D0.setDefaultValue("0");//设置默认参数
parser.addOption(pulseWidth_D0);
QCommandLineOption period_D0("period_D0", "Period_D0 of wit interface number.", "period_D0");
period_D0.setDefaultValue("0");//设置默认参数
parser.addOption(period_D0);
QCommandLineOption modelForInterface_D1("modelForInterface_D1", "modelForInterface_D1 of interface number.", "modelForInterface_D1");
parser.addOption(modelForInterface_D1);
QCommandLineOption pulseWidth_D1("pulseWidth_D1", "PulseWidth_D1 of interface number.", "pulseWidth_D1");
pulseWidth_D1.setDefaultValue("0");//设置默认参数
parser.addOption(pulseWidth_D1);
QCommandLineOption period_D1("period_D1", "Period_D1 of wit interface number.", "period_D1");
period_D1.setDefaultValue("0");//设置默认参数
parser.addOption(period_D1);
QCommandLineOption modelForInterface_D2("modelForInterface_D2", "modelForInterface_D2 of interface number.", "modelForInterface_D2");
parser.addOption(modelForInterface_D2);
QCommandLineOption pulseWidth_D2("pulseWidth_D2", "PulseWidth_D2 of interface number.", "pulseWidth_D2");
pulseWidth_D2.setDefaultValue("0");//设置默认参数
parser.addOption(pulseWidth_D2);
QCommandLineOption period_D2("period_D2", "Period_D2 of wit interface number.", "period_D2");
period_D2.setDefaultValue("0");//设置默认参数
parser.addOption(period_D2);
QCommandLineOption modelForInterface_D3("modelForInterface_D3", "modelForInterface_D3 of interface number.", "modelForInterface_D3");
parser.addOption(modelForInterface_D3);
QCommandLineOption pulseWidth_D3("pulseWidth_D3", "PulseWidth_D3 of interface number.", "pulseWidth_D3");
pulseWidth_D3.setDefaultValue("0");//设置默认参数
parser.addOption(pulseWidth_D3);
QCommandLineOption period_D3("period_D3", "Period_D3 of wit interface number.", "period_D3");
period_D3.setDefaultValue("0");//设置默认参数
parser.addOption(period_D3);
// QCommandLineOption rlx1("rlx1", "rlx1 parm of wit motion.", "rlx1");
// parser.addOption(rlx1);
QCommandLineOption rlx1(QStringList() << "rlx1" << "rlx1 long", "rlx1 parm of wit motion.");
parser.addOption(rlx1);
//解析命令
if (!parser.parse(QCoreApplication::arguments()))//Process the actual command line arguments given by the user
{
*errorMessage = parser.errorText();
return CommandLineError;
}
if (parser.isSet(versionOption))
return CommandLineVersionRequested;
if (parser.isSet(helpOption))
return CommandLineHelpRequested;
if (parser.isSet(serialPort))
{
const QString serialPortTmp = parser.value(serialPort);
query->serialPort = serialPortTmp;
}
else//默认参数
{
*errorMessage = "No serialPort set.";
return CommandLineError;
}
if (parser.isSet(connectBaudrate))
{
const QString connectBaudrateTmp = parser.value(connectBaudrate);
query->connectBaudrate = connectBaudrateTmp.toInt();
}
else//默认参数
{
*errorMessage = "No connectBaudrate set.";
return CommandLineError;
}
if (parser.isSet(algorithm))
{
const QString algorithmTmp1 = parser.value(algorithm);
int algorithmTmp2 = algorithmTmp1.toInt();
switch (algorithmTmp2)
{
case 9:
query->algorithm = ALGROITHM9;
query->isSetAlgorithm = true;
break;
case 6:
query->algorithm = ALGROITHM6;
query->isSetAlgorithm = true;
break;
default:
*errorMessage = "Algorithm set error.";
return CommandLineError;
break;
}
}
else//默认参数
{
query->isSetAlgorithm = false;
}
if (parser.isSet(installationOrientation))
{
const QString installationOrientationTmp1 = parser.value(installationOrientation);
int installationOrientationTmp2 = installationOrientationTmp1.toInt();
switch (installationOrientationTmp2)
{
case 0:
query->installationOrientation = ORIENT_HORIZONTAL;
query->isSetInstallationOrientation = true;
break;
case 1:
query->installationOrientation = ORIENT_VERTICAL;
query->isSetInstallationOrientation = true;
break;
default:
*errorMessage = "InstallationOrientation set error.";
return CommandLineError;
break;
}
}
else//默认参数
{
query->isSetInstallationOrientation = false;
}
if (parser.isSet(timeZone))
{
const QString timeZoneTmp1 = parser.value(timeZone);
int timeZoneTmp2 = timeZoneTmp1.toInt();
switch (timeZoneTmp2)
{
case 0:
query->timeZone = UTC_N12;
query->isSetTimeZone = true;
break;
case 1:
query->timeZone = UTC_N11;
query->isSetTimeZone = true;
break;
case 2:
query->timeZone = UTC_N10;
query->isSetTimeZone = true;
break;
case 3:
query->timeZone = UTC_N9;
query->isSetTimeZone = true;
break;
case 4:
query->timeZone = UTC_N8;
query->isSetTimeZone = true;
break;
case 5:
query->timeZone = UTC_N7;
query->isSetTimeZone = true;
break;
case 6:
query->timeZone = UTC_N6;
query->isSetTimeZone = true;
break;
case 7:
query->timeZone = UTC_N5;
query->isSetTimeZone = true;
break;
case 8:
query->timeZone = UTC_N4;
query->isSetTimeZone = true;
break;
case 9:
query->timeZone = UTC_N3;
query->isSetTimeZone = true;
break;
case 10:
query->timeZone = UTC_N2;
query->isSetTimeZone = true;
break;
case 11:
query->timeZone = UTC_N1;
query->isSetTimeZone = true;
break;
case 12:
query->timeZone = UTC;
query->isSetTimeZone = true;
break;
case 13:
query->timeZone = UTC_N1;
query->isSetTimeZone = true;
break;
case 14:
query->timeZone = UTC_N2;
query->isSetTimeZone = true;
break;
case 15:
query->timeZone = UTC_N3;
query->isSetTimeZone = true;
break;
case 16:
query->timeZone = UTC_N4;
query->isSetTimeZone = true;
break;
case 17:
query->timeZone = UTC_N5;
query->isSetTimeZone = true;
break;
case 18:
query->timeZone = UTC_N6;
query->isSetTimeZone = true;
break;
case 19:
query->timeZone = UTC_N7;
query->isSetTimeZone = true;
break;
case 20:
query->timeZone = UTC_N8;
query->isSetTimeZone = true;
break;
case 21:
query->timeZone = UTC_N9;
query->isSetTimeZone = true;
break;
case 22:
query->timeZone = UTC_N10;
query->isSetTimeZone = true;
break;
case 23:
query->timeZone = UTC_N11;
query->isSetTimeZone = true;
break;
case 24:
query->timeZone = UTC_N12;
query->isSetTimeZone = true;
break;
default:
*errorMessage = "timeZone set error.";
return CommandLineError;
break;
}
}
else//默认参数
{
query->isSetTimeZone = false;
}
query->isSetDefaultReturnContent = parser.isSet(isSetDefaultReturnContent);
if (parser.isSet(baudrate))
{
const QString baudrateTmp1 = parser.value(baudrate);
int baudrateTmp2 = baudrateTmp1.toInt();
switch (baudrateTmp2)
{
case 2400:
query->baudrate = WIT_BAUD_2400;
query->isSetBaudrate = true;
break;
case 4800:
query->baudrate = WIT_BAUD_4800;
query->isSetBaudrate = true;
break;
case 9600:
query->baudrate = WIT_BAUD_9600;
query->isSetBaudrate = true;
break;
case 19200:
query->baudrate = WIT_BAUD_19200;
query->isSetBaudrate = true;
break;
case 38400:
query->baudrate = WIT_BAUD_38400;
query->isSetBaudrate = true;
break;
case 57600:
query->baudrate = WIT_BAUD_57600;
query->isSetBaudrate = true;
break;
case 115200:
query->baudrate = WIT_BAUD_115200;
query->isSetBaudrate = true;
break;
case 230400:
query->baudrate = WIT_BAUD_230400;
query->isSetBaudrate = true;
break;
case 460800:
query->baudrate = WIT_BAUD_460800;
query->isSetBaudrate = true;
break;
case 921600:
query->baudrate = WIT_BAUD_921600;
query->isSetBaudrate = true;
break;
default:
*errorMessage = "baudrate set error.";
return CommandLineError;
break;
}
}
else//默认参数
{
query->isSetBaudrate = false;
}
if (parser.isSet(returnRate))
{
const QString returnRateTmp1 = parser.value(returnRate);
int returnRateTmp2 = returnRateTmp1.toInt();
switch (returnRateTmp2)
{
// case 0.1:
// query->returnRate = RRATE_01HZ;
// query->isSetReturnRate = true;
// break;
// case 0.5:
// query->returnRate = RRATE_05HZ;
// query->isSetReturnRate = true;
// break;
case 1:
query->returnRate = RRATE_1HZ;
query->isSetReturnRate = true;
break;
case 2:
query->returnRate = RRATE_2HZ;
query->isSetReturnRate = true;
break;
case 5:
query->returnRate = RRATE_5HZ;
query->isSetReturnRate = true;
break;
case 10:
query->returnRate = RRATE_10HZ;
query->isSetReturnRate = true;
break;
case 20:
query->returnRate = RRATE_20HZ;
query->isSetReturnRate = true;
break;
case 50:
query->returnRate = RRATE_50HZ;
query->isSetReturnRate = true;
break;
case 100:
query->returnRate = RRATE_100HZ;
query->isSetReturnRate = true;
break;
// case "none":
// query->returnRate = RRATE_NONE;
// query->isSetReturnRate = true;
// break;
case 200:
query->returnRate = RRATE_200HZ;
query->isSetReturnRate = true;
break;
// case "once":
// query->returnRate = RRATE_ONCE;
// query->isSetReturnRate = true;
// break;
default:
*errorMessage = "timeZone set error.";
return CommandLineError;
break;
}
}
else//默认参数
{
query->isSetReturnRate = false;
}
if (parser.isSet(deviceAddress))
{
const QString deviceAddressTmp = parser.value(deviceAddress);
query->deviceAddress = deviceAddressTmp.toInt();
query->isSetDeviceAddress = true;
}
else//默认参数
{
query->isSetDeviceAddress = false;
}
bool ret;
ret=processModelParm(parser,modelForInterface_D0,query->modelForInterface_D0,query->isSetInterface_D0);
if(!ret)
{
*errorMessage = "modelForInterface_D0 set error.";
return CommandLineError;
}
ret=processPulseWidthParm(parser,pulseWidth_D0, query->pulseWidth_D0, query->isSetInterface_D0);
ret=processPeriodParm(parser,period_D0, query->period_D0, query->isSetInterface_D0);
ret=processModelParm_D1(parser,modelForInterface_D1,query->modelForInterface_D1,query->isSetInterface_D1);
if(!ret)
{
*errorMessage = "modelForInterface_D1 set error.";
return CommandLineError;
}
ret=processPulseWidthParm(parser,pulseWidth_D1, query->pulseWidth_D1, query->isSetInterface_D1);
ret=processPeriodParm(parser,period_D1, query->period_D1, query->isSetInterface_D1);
ret=processModelParm(parser,modelForInterface_D2,query->modelForInterface_D2,query->isSetInterface_D2);
if(!ret)
{
*errorMessage = "modelForInterface_D2 set error.";
return CommandLineError;
}
ret=processPulseWidthParm(parser,pulseWidth_D2, query->pulseWidth_D2, query->isSetInterface_D2);
ret=processPeriodParm(parser,period_D2, query->period_D2, query->isSetInterface_D2);
ret=processModelParm(parser,modelForInterface_D3,query->modelForInterface_D3,query->isSetInterface_D3);
if(!ret)
{
*errorMessage = "modelForInterface_D3 set error.";
return CommandLineError;
}
ret=processPulseWidthParm(parser,pulseWidth_D3, query->pulseWidth_D3, query->isSetInterface_D3);
ret=processPeriodParm(parser,period_D3, query->period_D3, query->isSetInterface_D3);
if (parser.isSet(rlx1))
{
query->isSetAlgorithm = true;
query->algorithm = ALGROITHM9;
query->isSetInstallationOrientation = true;
query->installationOrientation = ORIENT_HORIZONTAL;
query->isSetDefaultReturnContent = true;
query->isSetBaudrate = true;
query->baudrate = WIT_BAUD_115200;
query->isSetReturnRate = true;
query->returnRate = RRATE_10HZ;
query->isSetInterface_D1 = true;
query->modelForInterface_D1 = AIN_D1;
query->pulseWidth_D1 = 0;
query->period_D1 = 0;
query->isSetInterface_D2 = true;
query->modelForInterface_D2 = PWM;
query->pulseWidth_D2 = 1500;
query->period_D2 = 20000;
query->isSetInterface_D3 = true;
query->modelForInterface_D3 = PWM;
query->pulseWidth_D3 = 1500;
query->period_D3 = 20000;
query->isSetReturnRate= true;
query->returnRate=RRATE_20HZ;
}
return CommandLineOk;
}
bool getModel(int modelIndex, MODEL_D0_D2_D3_ENUM & model)
{
switch (modelIndex)
{
case 0:
model = AIN;
return true;
case 1:
model = DIN;
return true;
case 2:
model = DOH;
return true;
case 3:
model = DOL;
return true;
case 4:
model = PWM;
return true;
default:
return false;
}
}
bool getModel_D1(int modelIndex, MODEL_D1_ENUM & model)
{
switch (modelIndex)
{
case 0:
model = AIN_D1;
return true;
case 1:
model = DIN_D1;
return true;
case 2:
model = DOH_D1;
return true;
case 3:
model = DOL_D1;
return true;
case 4:
model = PWM_D1;
return true;
case 5:
model = CLR_D1;
return true;
default:
return false;
}
}
bool processModelParm(QCommandLineParser &parser, QCommandLineOption commandLineOption, MODEL_D0_D2_D3_ENUM & model, bool &isSetInterface)
{
if(parser.isSet(commandLineOption))
{
const QString modelForInterface_D0Tmp1 = parser.value(commandLineOption);
int modelForInterface_D0Tmp2 = modelForInterface_D0Tmp1.toInt();
bool ret = getModel(modelForInterface_D0Tmp2, model);
if(ret)
{
isSetInterface = true;
}
else
{
return false;
}
}
else
{
isSetInterface = false;
}
return true;
}
bool processModelParm_D1(QCommandLineParser &parser, QCommandLineOption commandLineOption, MODEL_D1_ENUM & model, bool &isSetInterface)
{
if(parser.isSet(commandLineOption))
{
const QString modelForInterface_D0Tmp1 = parser.value(commandLineOption);
int modelForInterface_D0Tmp2 = modelForInterface_D0Tmp1.toInt();
bool ret = getModel_D1(modelForInterface_D0Tmp2, model);
if(ret)
{
isSetInterface = true;
}
else
{
return false;
}
}
else
{
isSetInterface = false;
}
return true;
}
bool processPulseWidthParm(QCommandLineParser &parser, QCommandLineOption commandLineOption, int & pulseWidth, bool &isSetInterface)
{
if (isSetInterface && parser.isSet(commandLineOption))
{
const QString pulseWidth_D0Tmp = parser.value(commandLineOption);
pulseWidth = pulseWidth_D0Tmp.toInt();
isSetInterface = true;
}
else//默认参数
{
QStringList tmp = commandLineOption.defaultValues();
pulseWidth = tmp[0].toInt();
isSetInterface = false;
}
return true;
}
bool processPeriodParm(QCommandLineParser &parser, QCommandLineOption commandLineOption, int & period, bool &isSetInterface)
{
if (isSetInterface && parser.isSet(commandLineOption))
{
const QString period_Tmp = parser.value(commandLineOption);
period = period_Tmp.toInt();
}
else//默认参数
{
QStringList tmp = commandLineOption.defaultValues();
period = tmp[0].toInt();
isSetInterface = false;
}
return true;
}

View File

@ -0,0 +1,73 @@
#ifndef COMMANDLINEPARSER_H
#define COMMANDLINEPARSER_H
#include <QCommandLineParser>
#include <QDir>
#include <string>
#include "witcommand.h"
enum CommandLineParseResult
{
CommandLineOk,
CommandLineError,
CommandLineVersionRequested,
CommandLineHelpRequested
};
struct TcQuery
{
QString serialPort;
int connectBaudrate;
bool isSetAlgorithm;
ALGROITHM_ENUM algorithm;
bool isSetInstallationOrientation;
ORIENT_ENUM installationOrientation;
bool isSetTimeZone;
TIMEZONE_ENUM timeZone;
bool isSetDefaultReturnContent;//wit默认 + 经纬度
RETURN_CONTENT_STRUCT defaultReturnContent;
RETURN_CONTENT_STRUCT returnContent;
bool isSetBaudrate;
BAUD_ENUM baudrate;
bool isSetReturnRate;
RRATE_ENUM returnRate;
bool isSetDeviceAddress;
int deviceAddress;
bool isSetInterface_D0;
MODEL_D0_D2_D3_ENUM modelForInterface_D0;//模式
int pulseWidth_D0;//脉宽
int period_D0;//周期
bool isSetInterface_D1;
MODEL_D1_ENUM modelForInterface_D1;//模式
int pulseWidth_D1;//脉宽
int period_D1;//周期
bool isSetInterface_D2;
MODEL_D0_D2_D3_ENUM modelForInterface_D2;//模式
int pulseWidth_D2;//脉宽
int period_D2;//周期
bool isSetInterface_D3;
MODEL_D0_D2_D3_ENUM modelForInterface_D3;//模式
int pulseWidth_D3;//脉宽
int period_D3;//周期
};
CommandLineParseResult parseCommandLine2(QCommandLineParser &parser, TcQuery *query, QString *errorMessage);
bool getModel(int modelIndex, MODEL_D0_D2_D3_ENUM & model);
bool getModel_D1(int modelIndex, MODEL_D1_ENUM & model);
bool processModelParm(QCommandLineParser &parser, QCommandLineOption commandLineOption, MODEL_D0_D2_D3_ENUM & model, bool &isSetInterface);
bool processModelParm_D1(QCommandLineParser &parser, QCommandLineOption commandLineOption, MODEL_D1_ENUM & model, bool &isSetInterface);
bool processPulseWidthParm(QCommandLineParser &parser, QCommandLineOption commandLineOption, int & pulseWidth, bool &isSetInterface);
bool processPeriodParm(QCommandLineParser &parser, QCommandLineOption commandLineOption, int & period, bool &isSetInterface);
#endif // COMMANDLINEPARSER_H

142
source/main.cpp Normal file
View File

@ -0,0 +1,142 @@
#include <QCoreApplication>
#include <QThread>
#include "witmotiondll.h"
#include "qtserialport.h"
#include "commandlineparser.h"
void delay_tc(uint32_t millisecond)
{
QThread::msleep(millisecond);
// 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);
// 解析命令行参数
QCommandLineParser parser;
parser.setApplicationDescription("This software is used for config parameter for wit inertial navigation.");
TcQuery query;
QString errorMessage;
switch (parseCommandLine2(parser, &query, &errorMessage))
{
case CommandLineOk:
break;
case CommandLineError:
fputs(qPrintable(errorMessage), stderr);
fputs("\n\n", stderr);
fputs(qPrintable(parser.helpText()), stderr);
return 1;
case CommandLineVersionRequested:
printf("%s %s\n", qPrintable(QCoreApplication::applicationName()),
qPrintable(QCoreApplication::applicationVersion()));
return 0;
case CommandLineHelpRequested:
parser.showHelp();
Q_UNREACHABLE();
}
SerialPortBase * serialPort = new QtSerialport();
int ret = serialPort->OpenSerialPort(query.serialPort.toStdString(), query.connectBaudrate);//"COM15"
if(ret)
{
std::cout<<"Serial port open failed!!"<<std::endl;
return 1;
}
WitmotionDll * witmotion = new WitmotionDll(serialPort);
witmotion->delayMsRegister(delay_tc);
witmotion->printfRegister(printf_tc);
witmotion->setDelayTimeMs(200);
if(query.isSetAlgorithm)
{
witmotion->algorithm(query.algorithm);
}
if(query.isSetInstallationOrientation)
{
witmotion->installationOrientation(query.installationOrientation);
}
if(query.isSetTimeZone)
{
witmotion->setTimeZone(query.timeZone);
}
if(query.isSetDefaultReturnContent)
{
witmotion->setContent(query.defaultReturnContent);
}
if(query.isSetReturnRate)
{
witmotion->SetReturnRate(query.returnRate);
}
if(query.isSetDeviceAddress)
{
witmotion->SetDeviceAddress(query.deviceAddress);
}
if(query.isSetInterface_D0)
{
witmotion->setD0Model(query.modelForInterface_D0);
witmotion->setD0HighLevelPulseWidth(query.pulseWidth_D0);
witmotion->setD0Period(query.period_D0);
}
if(query.isSetInterface_D1)
{
witmotion->setD1Model(query.modelForInterface_D1);
witmotion->setD1HighLevelPulseWidth(query.pulseWidth_D1);
witmotion->setD1Period(query.period_D1);
}
if(query.isSetInterface_D2)
{
witmotion->setD2Model(query.modelForInterface_D2);
witmotion->setD2HighLevelPulseWidth(query.pulseWidth_D2);
witmotion->setD2Period(query.period_D2);
}
if(query.isSetInterface_D3)
{
witmotion->setD3Model(query.modelForInterface_D3);
witmotion->setD3HighLevelPulseWidth(query.pulseWidth_D3);
witmotion->setD3Period(query.period_D3);
}
if(query.isSetReturnRate)//设置witMotion的通讯速率要放在最后它后面的设置都会失败
{
witmotion->SetReturnRate(query.returnRate);
}
if(query.isSetBaudrate)//设置witMotion的通讯速率要放在最后它后面的设置都会失败
{
witmotion->SetBaudrate(query.baudrate);
// serialPort->CloseSerialPort();//设置完波特率 已经无法通讯 需要重新简历连接
serialPort->SetBaudrate(115200);
//ret = serialPort->OpenSerialPort(query.serialPort.toStdString(), query.baudrate);//重新建立连接
witmotion->saveInstruction();
}
// witmotion->recordData();
std::cout<<"Set IMU OK"<<std::flush;
serialPort->CloseSerialPort();
// return a.exec();
}

122
source/qtserialport.cpp Normal file
View File

@ -0,0 +1,122 @@
#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);
int x=SetBaudrate(baudrate);
if(x == 0)
{
return 0;
}
else
{
return 1;
}
}
int QtSerialport::CloseSerialPort()
{
m_serial->close();
return 0;
}
int QtSerialport::SetBaudrate(int baudrate)
{
bool x=m_serial->setBaudRate(baudrate);//qt的串口类设置波特率时任何数字都能成功这是什么鬼
int tmp = m_serial->baudRate();
if(x)
{
return 0;//成功
}
else
{
return 1;//失败
}
}
//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(1000);
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(1000))
{
//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
source/qtserialport.h Normal file
View 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

58
source/register.h Normal file
View File

@ -0,0 +1,58 @@
#ifndef REGISTER_H
#define REGISTER_H
#define SAVE 0x00
#define CALSW 0x01
#define RSW 0x02
#define RRATE 0x03
#define BAUD 0x04
#define AXOFFSET 0x05
#define AYOFFSET 0x06
#define AZOFFSET 0x07
#define GXOFFSET 0x08
#define GYOFFSET 0x09
#define GZOFFSET 0x0a
#define HXOFFSET 0x0b
#define HYOFFSET 0x0c
#define HZOFFSET 0x0d
#define D0MODE 0x0e
#define D1MODE 0x0f
#define D2MODE 0x10
#define D3MODE 0x11
#define D0PWMH 0x12
#define D1PWMH 0x13
#define D2PWMH 0x14
#define D3PWMH 0x15
#define D0PWMT 0x16
#define D1PWMT 0x17
#define D2PWMT 0x18
#define D3PWMT 0x19
#define IICADDR 0x1a
#define LEDOFF 0x1b
#define MAGRANGX 0x1c
#define MAGRANGY 0x1d
#define MAGRANGZ 0x1e
#define BANDWIDTH 0x1f
#define GYRORANGE 0x20
#define ACCRANGE 0x21
#define SLEEP 0x22
#define ORIENT 0x23
#define AXIS6 0x24
#define FILTK 0x25
#define GPSBAUD 0x26
#define READADDR 0x27
#define BWSCALE 0x28
#define MOVETHR 0x28
#define MOVESTA 0x29
#define ACCFILT 0x2A
#define GYROFILT 0x2b
#define MAGFILT 0x2c
#define POWONSEND 0x2d
#define VERSION 0x2e
#define KEY 0x69
#define TIMEZONE 0x6B
#endif // REGISTER_H

25
source/serialportbase.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef SERIALPORTBASE_H
#define SERIALPORTBASE_H
#include <iostream>
using namespace std;
class SerialPortBase
{
public:
// SerialPortBase();
// ~SerialPortBase();
virtual int OpenSerialPort(string portName, int baudrate) = 0;
virtual int CloseSerialPort() = 0;
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(char * receivedData) = 0;
protected:
private:
};
#endif // SERIALPORTBASE_H

161
source/witcommand.h Normal file
View File

@ -0,0 +1,161 @@
#ifndef WITCOMMAND_H
#define WITCOMMAND_H
/* KEY */
#define KEY_UNLOCK 0xB588
/* SAVE */
#define SAVE_PARAM 0x00
#define SAVE_SWRST 0xFF
/* algorithm */
enum ALGROITHM_ENUM {
ALGROITHM9 = 0,
ALGROITHM6 = 1
};
/* ORIENT */
enum ORIENT_ENUM {
ORIENT_HORIZONTAL = 0,
ORIENT_VERTICAL
};
/* ORIENT */
enum POWONSEND_ENUM {
CLOSE = 0,
OPEN
};
/* CALSW */
enum CAL_ENUM {
EXIT = 0,
GYROACC,
MAGNETIC,
ALTITUDE,
ALANGLEZ
};
#define EXITCAL 0x00
#define CALGYROACC 0x01
#define CALMAG 0x02
#define CALALTITUDE 0x03
#define CALANGLEZ 0x04
/* time zone */
enum TIMEZONE_ENUM {
UTC_N12 = 0,
UTC_N11,
UTC_N10,
UTC_N9,
UTC_N8,
UTC_N7,
UTC_N6,
UTC_N5,
UTC_N4,
UTC_N3,
UTC_N2,
UTC_N1,
UTC,
UTC_P1,
UTC_P2,
UTC_P3,
UTC_P4,
UTC_P5,
UTC_P6,
UTC_P7,
UTC_P8,//default
UTC_P9,
UTC_P10,
UTC_P11,
UTC_P12
};
/* RETURN CONTENT */
#define SetBit(VAR, Place) (VAR |= (1 << Place))
#define ClrBit(VAR, Place) (VAR &= ((1 << Place) ^ 255))
enum RETURN_CONTENT_ENUM {
//低位
TIME = 0,
ACCELERATION,
ANGULAR_VELOCITY,
EULER_ANGLE,
MAGNETIC_FIELD,
PORT_STATUS,
ATMOSPHERIC_PRESSURE_ALTITUDE,
LATITUDE_LONGITUDE,
//高位
GROUND_VELOCITY,
QUATERNION,//四元数
SATELLITE_ACCURACY
};
struct RETURN_CONTENT_STRUCT
{
//低位
bool time = false;
bool acceleration = true;
bool angular_velocity = true;
bool euler_angle = true;
bool magnetic_field = true;
bool port_status = false;
bool atmospheric_pressure_altitude = false;
bool latitude_longitude = true;
//高位
bool ground_velocity = false;
bool quaternion = false;
bool satellite_accuracy = false;
};
/* BAUD */
enum BAUD_ENUM {
WIT_BAUD_2400 = 0,
WIT_BAUD_4800,
WIT_BAUD_9600,//default
WIT_BAUD_19200,
WIT_BAUD_38400,
WIT_BAUD_57600,
WIT_BAUD_115200,
WIT_BAUD_230400,
WIT_BAUD_460800,
WIT_BAUD_921600
};
/**RRATE*****/
enum RRATE_ENUM {
RRATE_01HZ = 1,
RRATE_05HZ,
RRATE_1HZ,
RRATE_2HZ,
RRATE_5HZ,
RRATE_10HZ,//default
RRATE_20HZ,
RRATE_50HZ,
RRATE_100HZ,
RRATE_NONE,//保留
RRATE_200HZ,
RRATE_ONCE
};
/* 端口模式 */
enum MODEL_D0_D2_D3_ENUM {
AIN = 0,//default
DIN,
DOH,
DOL,
PWM
};
enum MODEL_D1_ENUM {
AIN_D1 = 0,//default
DIN_D1,
DOH_D1,
DOL_D1,
PWM_D1,
CLR_D1
};
#endif // WITCOMMAND_H

35
source/witmotionDll.pro Normal file
View File

@ -0,0 +1,35 @@
QT += core
QT -= gui
QT += serialport
CONFIG += c++11
TARGET = witmotionDll
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
witmotiondll.cpp \
qtserialport.cpp \
commandlineparser.cpp
# The following define makes your compiler emit warnings if you use
# 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
# 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
HEADERS += \
register.h \
serialportbase.h \
witmotiondll.h \
qtserialport.h \
commandlineparser.h \
witcommand.h

407
source/witmotiondll.cpp Normal file
View File

@ -0,0 +1,407 @@
#include "witmotiondll.h"
//当没有注册 延时函数m_delayFunction运行会停止在调用延时函数m_delayFunction
//此函数就是为了解决上面说的问题
void delay_default(uint32_t millisecond)
{
;
}
void printf_default(const char* text)
{
;
}
WitmotionDll::WitmotionDll(SerialPortBase * serialPort)
{
m_SerialPort = serialPort;
m_delayFunction = delay_default;
m_witPrintf = printf_default;
}
int WitmotionDll::delayMsRegister(delay delayFunction)
{
m_delayFunction = delayFunction;
return 0;
}
int WitmotionDll::printfRegister(witPrintf printfFunction)
{
m_witPrintf = printfFunction;
return 0;
}
void WitmotionDll::setDelayTimeMs(uint32_t millisecond)
{
TIME_TO_SLEEP = millisecond;
}
int WitmotionDll::constructAndSendInstruction(int registerAddress, int registerContent)
{
uint8_t unBuffer[8];
unBuffer[0]=0xFF;
unBuffer[1]=0xAA;
unBuffer[2]=registerAddress & 0xFF;
unBuffer[3]=registerContent & 0xff;
unBuffer[4]=registerContent >> 8;
int num = m_SerialPort->SendData1((char *)unBuffer,5);
m_delayFunction(TIME_TO_SLEEP);
return num;
}
int WitmotionDll::unlockInstruction()
{
int numOfSend = constructAndSendInstruction(KEY, KEY_UNLOCK);
m_witPrintf("WitmotionDll::unlockInstruction: \n");
return 0;
}
int WitmotionDll::saveInstruction()
{
int numOfSend = constructAndSendInstruction(SAVE, SAVE_PARAM);
m_witPrintf("WitmotionDll::saveInstruction: \n");
return 0;
}
int WitmotionDll::algorithm(ALGROITHM_ENUM algorithm)
{
unlockInstruction();
int numOfSend = constructAndSendInstruction(AXIS6, algorithm);
m_witPrintf("WitmotionDll::algorithm: \n");
saveInstruction();
return 0;
}
int WitmotionDll::installationOrientation(ORIENT_ENUM orient)
{
unlockInstruction();
int numOfSend = constructAndSendInstruction(ORIENT, orient);
m_witPrintf("WitmotionDll::installationOrientation:\n");
saveInstruction();
return 0;
}
int WitmotionDll::instructStart(POWONSEND_ENUM command)
{
unlockInstruction();
int numOfSend = constructAndSendInstruction(POWONSEND, command);
m_witPrintf("WitmotionDll::instructStart: \n");
saveInstruction();
return 0;
}
int WitmotionDll::exitCalibration()
{
unlockInstruction();
int numOfSend = constructAndSendInstruction(CALSW, EXITCAL);
m_witPrintf("WitmotionDll::exitCalibration: \n");
saveInstruction();
return 0;
}
int WitmotionDll::magneticCalibration()
{
unlockInstruction();
int numOfSend = constructAndSendInstruction(CALSW, CALMAG);
m_witPrintf("WitmotionDll::magneticCalibration: \n");
saveInstruction();
return 0;
}
int WitmotionDll::setHeightToZero()
{
unlockInstruction();
int numOfSend = constructAndSendInstruction(CALSW, CALALTITUDE);
m_witPrintf("WitmotionDll::setHeightToZero: \n");
saveInstruction();
return 0;
}
int WitmotionDll::setZAxisAngleToZero()
{
//需要读取参数确保处于算法ALGROITHM6下
unlockInstruction();
int numOfSend = constructAndSendInstruction(CALSW, CALANGLEZ);
m_witPrintf("WitmotionDll::setZAxisAngleToZero: \n");
saveInstruction();
return 0;
}
int WitmotionDll::setAngleReference()
{
unlockInstruction();
uint16_t registerContent = 0x08;//询问技术支持才得到的命令
int numOfSend = constructAndSendInstruction(CALSW, registerContent);
m_witPrintf("WitmotionDll::setAngleReference: \n");
saveInstruction();
return 0;
}
void WitmotionDll::recordData()
{
char * receivedData;
m_SerialPort->ReadData(receivedData);
}
int WitmotionDll::setTimeZone(TIMEZONE_ENUM timeZone)
{
unlockInstruction();
int numOfSend = constructAndSendInstruction(TIMEZONE, timeZone);
m_witPrintf("WitmotionDll::setTimeZone: \n");
saveInstruction();
return 0;
}
int WitmotionDll::setContent(RETURN_CONTENT_STRUCT content)
{
unlockInstruction();
uint16_t registerContent = 0;
if(content.time)
SetBit(registerContent, TIME);
if(content.acceleration)
SetBit(registerContent, ACCELERATION);
if(content.angular_velocity)
SetBit(registerContent, ANGULAR_VELOCITY);
if(content.euler_angle)
SetBit(registerContent, EULER_ANGLE);
if(content.magnetic_field)
SetBit(registerContent, MAGNETIC_FIELD);
if(content.port_status)
SetBit(registerContent, PORT_STATUS);
if(content.atmospheric_pressure_altitude)
SetBit(registerContent, ATMOSPHERIC_PRESSURE_ALTITUDE);
if(content.latitude_longitude)
SetBit(registerContent, LATITUDE_LONGITUDE);
if(content.ground_velocity)
SetBit(registerContent, GROUND_VELOCITY);
if(content.quaternion)
SetBit(registerContent, QUATERNION);
if(content.satellite_accuracy)
SetBit(registerContent, SATELLITE_ACCURACY);
int numOfSend = constructAndSendInstruction(RSW, registerContent);
m_witPrintf("WitmotionDll::setContent: \n");
saveInstruction();
return 0;
}
int WitmotionDll::clearContent(RETURN_CONTENT_STRUCT content)
{
return 0;
}
int WitmotionDll::SetBaudrate(BAUD_ENUM baudrate)
{
unlockInstruction();
int numOfSend = constructAndSendInstruction(BAUD, baudrate);
m_witPrintf("WitmotionDll::SetBaudrate: \n");
// saveInstruction(); //设置完波特率 已经无法通讯 需要重新简历连接
return 0;
}
int WitmotionDll::SetReturnRate(RRATE_ENUM returnRate)
{
unlockInstruction();
int numOfSend = constructAndSendInstruction(RRATE, returnRate);
m_witPrintf("WitmotionDll::SetReturnRate: \n");
saveInstruction();
return 0;
}
int WitmotionDll::SetDeviceAddress(int deviceAddress)
{
unlockInstruction();
int numOfSend = constructAndSendInstruction(IICADDR, deviceAddress);
m_witPrintf("WitmotionDll::SetDeviceAddress: \n");
saveInstruction();
return 0;
}
int WitmotionDll::setD0Model(MODEL_D0_D2_D3_ENUM model)
{
unlockInstruction();
int numOfSend = constructAndSendInstruction(D0MODE, model);
m_witPrintf("WitmotionDll::setD0Model: \n");
saveInstruction();
return 0;
}
int WitmotionDll::setD1Model(MODEL_D1_ENUM model)
{
unlockInstruction();
int numOfSend = constructAndSendInstruction(D1MODE, model);
m_witPrintf("WitmotionDll::setD1Model: \n");
saveInstruction();
return 0;
}
int WitmotionDll::setD2Model(MODEL_D0_D2_D3_ENUM model)
{
unlockInstruction();
int numOfSend = constructAndSendInstruction(D2MODE, model);
m_witPrintf("WitmotionDll::setD2Model: \n");
saveInstruction();
return 0;
}
int WitmotionDll::setD3Model(MODEL_D0_D2_D3_ENUM model)
{
unlockInstruction();
int numOfSend = constructAndSendInstruction(D3MODE, model);
m_witPrintf("WitmotionDll::setD3Model: \n");
saveInstruction();
return 0;
}
int WitmotionDll::setD0HighLevelPulseWidth(int PWMH)
{
unlockInstruction();
int numOfSend = constructAndSendInstruction(D0PWMH, PWMH);
m_witPrintf("WitmotionDll::setD0HighLevelPulseWidth: \n");
saveInstruction();
return 0;
}
int WitmotionDll::setD1HighLevelPulseWidth(int PWMH)
{
unlockInstruction();
int numOfSend = constructAndSendInstruction(D1PWMH, PWMH);
m_witPrintf("WitmotionDll::setD1HighLevelPulseWidth: \n");
saveInstruction();
return 0;
}
int WitmotionDll::setD2HighLevelPulseWidth(int PWMH)
{
unlockInstruction();
int numOfSend = constructAndSendInstruction(D2PWMH, PWMH);
m_witPrintf("WitmotionDll::setD2HighLevelPulseWidth: \n");
saveInstruction();
return 0;
}
int WitmotionDll::setD3HighLevelPulseWidth(int PWMH)
{
unlockInstruction();
int numOfSend = constructAndSendInstruction(D3PWMH, PWMH);
m_witPrintf("WitmotionDll::setD3HighLevelPulseWidth: \n");
saveInstruction();
return 0;
}
int WitmotionDll::setD0Period(int period)
{
unlockInstruction();
int numOfSend = constructAndSendInstruction(D0PWMT, period);
m_witPrintf("WitmotionDll::setD0Period: \n");
saveInstruction();
return 0;
}
int WitmotionDll::setD1Period(int period)
{
unlockInstruction();
int numOfSend = constructAndSendInstruction(D1PWMT, period);
m_witPrintf("WitmotionDll::setD1Period: \n");
saveInstruction();
return 0;
}
int WitmotionDll::setD2Period(int period)
{
unlockInstruction();
int numOfSend = constructAndSendInstruction(D2PWMT, period);
m_witPrintf("WitmotionDll::setD2Period: \n");
saveInstruction();
return 0;
}
int WitmotionDll::setD3Period(int period)
{
unlockInstruction();
int numOfSend = constructAndSendInstruction(D3PWMT, period);
m_witPrintf("WitmotionDll::setD3Period: \n");
saveInstruction();
return 0;
}

85
source/witmotiondll.h Normal file
View File

@ -0,0 +1,85 @@
#ifndef WITMOTIONDLL_H
#define WITMOTIONDLL_H
#include <iostream>
#include <string>
#include <thread>
#include "serialportbase.h"
#include "register.h"
#include "witcommand.h"
typedef void (*delay)(uint32_t millisecond);
typedef void (*witPrintf)(const char* text);
class WitmotionDll
{
public:
WitmotionDll(SerialPortBase * serialPort);
int delayMsRegister(delay delayFunction);
int printfRegister(witPrintf printfFunction);
void setDelayTimeMs(uint32_t millisecond);
int constructAndSendInstruction(int registerAddress, int registerContent);
int unlockInstruction();
int saveInstruction();
void recordData();
//系统
int algorithm(ALGROITHM_ENUM algorithm);
int installationOrientation(ORIENT_ENUM orient);
int instructStart(POWONSEND_ENUM command);//是否上电输出数据,可以防止鼠标乱跳
//校准:完成后,需要退出校准模式( 调用exitCalibration
int exitCalibration();//退出校准模式
int magneticCalibration();
int setHeightToZero();//需要JY901B带有高度数据输出的模块才能进行高度校准此为相对高度短时间内有效
int setZAxisAngleToZero();//Z轴角度归零只在切换成 6 轴算法下才能成功置零;
int setAngleReference();//询问技术支持,文档没有,不建议使用,设置后只能在静态下运动,即原点旋转单轴,其他方式旋转出来会有比较大的误差
//范围
int setTimeZone(TIMEZONE_ENUM timeZone);
//设置回传内容
int setContent(RETURN_CONTENT_STRUCT content);
int clearContent(RETURN_CONTENT_STRUCT content);
//通讯
int SetBaudrate(BAUD_ENUM baudrate);
int SetReturnRate(RRATE_ENUM returnRate);
int SetDeviceAddress(int deviceAddress);//1只用于IIC协议串口不用2设置、保存后重新上电生效3最大为0x7f(124)
//接口
int setD0Model(MODEL_D0_D2_D3_ENUM model);
int setD1Model(MODEL_D1_ENUM model);
int setD2Model(MODEL_D0_D2_D3_ENUM model);
int setD3Model(MODEL_D0_D2_D3_ENUM model);
int setD0HighLevelPulseWidth(int PWMH);
int setD1HighLevelPulseWidth(int PWMH);
int setD2HighLevelPulseWidth(int PWMH);
int setD3HighLevelPulseWidth(int PWMH);
int setD0Period(int period);
int setD1Period(int period);
int setD2Period(int period);
int setD3Period(int period);
private:
SerialPortBase * m_SerialPort;
uint32_t TIME_TO_SLEEP = 500;//毫秒
delay m_delayFunction;
witPrintf m_witPrintf;
};
#endif // WITMOTIONDLL_H