1.采用QChart实现Nm状态转移的监控,包括各个模式的时间。
标准auosar Nm状态机:
下图显示了一个被动唤醒和一个自动唤醒的状态转移,其中每个模式的所用的时间在对应的X轴上。
2.实现代码:
dialogcannmstateanalyze.h
#ifndef DIALOGCANNMSTATEANALYZE_H
#define DIALOGCANNMSTATEANALYZE_H
#include
#include
#include
//#include
#include
QT_CHARTS_USE_NAMESPACE
namespace Ui {
class DialogCanNmStateAnalyze;
}
class DialogCanNmStateAnalyze : public QDialog
{
Q_OBJECT
public:
explicit DialogCanNmStateAnalyze(QWidget *parent = 0);
~DialogCanNmStateAnalyze();
void setCanNetWorkStates(uint8_t states,uint32_t timestamp);
public slots:
private:
uint32_t counter;
QListdata_CanNmStates;
QListdata_TimeStamp;
QChart *chart_CanNmStates;
QCategoryAxis *axisX_CanNmStates;
QCategoryAxis *axisY_CanNmStates;
QLineSeries *series_CanNmStates;
Ui::DialogCanNmStateAnalyze *ui;
QString timeFormatConvert(uint32_t time);
protected:
void closeEvent(QCloseEvent *event);
};
#endif // DIALOGCANNMSTATEANALYZE_H
dialogcannmstateanalyze.cpp
#include "dialogcannmstateanalyze.h"
#include "ui_dialogcannmstateanalyze.h"
#include "Dialogs/MainWindow/xcan.h"
#include
#include
#include
#include
#include"qdebug.h"
QT_CHARTS_USE_NAMESPACE
DialogCanNmStateAnalyze::DialogCanNmStateAnalyze(QWidget *parent) :
QDialog(parent),
ui(new Ui::DialogCanNmStateAnalyze)
{
ui->setupUi(this);
counter = 0;
/******************************************************************************************/
//![1]
series_CanNmStates = new QLineSeries();
chart_CanNmStates = new QChart();
chart_CanNmStates->legend()->hide();
chart_CanNmStates->addSeries(series_CanNmStates);
//![1]
//![2]
// Customize series
QPen pen_CanNmStates(QRgb(0xfdb157));
pen_CanNmStates.setWidth(2);
series_CanNmStates->setPen(pen_CanNmStates);
// series_CanNmStates->setPointsVisible(true);
// series_CanNmStates->setPointLabelsVisible(true);
// series_CanNmStates->setPointLabelsFormat("@xPoint");
// Customize chart title
QFont font_CanNmStates;
font_CanNmStates.setPixelSize(18);
chart_CanNmStates->setTitleFont(font_CanNmStates);
chart_CanNmStates->setTitleBrush(QBrush(Qt::white));
chart_CanNmStates->setTitle("CanNmStates");
// Customize chart background
QLinearGradient backgroundGradient_CanNmStates;
backgroundGradient_CanNmStates.setStart(QPointF(0, 0));
backgroundGradient_CanNmStates.setFinalStop(QPointF(0, 1));
backgroundGradient_CanNmStates.setColorAt(0.0, QRgb(0xd2d0d1));
backgroundGradient_CanNmStates.setColorAt(1.0, QRgb(0x4c4547));
backgroundGradient_CanNmStates.setCoordinateMode(QGradient::ObjectBoundingMode);
chart_CanNmStates->setBackgroundBrush(backgroundGradient_CanNmStates);
// Customize plot area background
QLinearGradient plotAreaGradient_CanNmStates;
plotAreaGradient_CanNmStates.setStart(QPointF(0, 1));
plotAreaGradient_CanNmStates.setFinalStop(QPointF(1, 0));
plotAreaGradient_CanNmStates.setColorAt(0.0, QRgb(0x555555));
plotAreaGradient_CanNmStates.setColorAt(1.0, QRgb(0x55aa55));
plotAreaGradient_CanNmStates.setCoordinateMode(QGradient::ObjectBoundingMode);
chart_CanNmStates->setPlotAreaBackgroundBrush(plotAreaGradient_CanNmStates);
chart_CanNmStates->setPlotAreaBackgroundVisible(true);
//![2]
//![3]
axisX_CanNmStates = new QCategoryAxis();
axisY_CanNmStates = new QCategoryAxis();
// Customize axis label font
QFont labelsFont_CanNmStates;
labelsFont_CanNmStates.setPixelSize(12);
// axisX_CanNmStates->setLabelsFont(labelsFont_CanNmStates);
axisY_CanNmStates->setLabelsFont(labelsFont_CanNmStates);
// Customize axis colors
QPen axisPen_CanNmStates(QRgb(0xd18952));
axisPen_CanNmStates.setWidth(2);
// axisX_CanNmStates->setLinePen(axisPen_CanNmStates);
axisY_CanNmStates->setLinePen(axisPen_CanNmStates);
// Customize axis label colors
QBrush axisBrush_CanNmStates(Qt::white);
// axisX_CanNmStates->setLabelsBrush(axisBrush_CanNmStates);
axisY_CanNmStates->setLabelsBrush(axisBrush_CanNmStates);
// Customize grid lines and shades
// axisX_CanNmStates->setGridLineVisible(false);
axisY_CanNmStates->setGridLineVisible(false);
axisY_CanNmStates->setShadesPen(Qt::NoPen);
axisY_CanNmStates->setShadesBrush(QBrush(QColor(0x99, 0xcc, 0xcc, 0x55)));
axisY_CanNmStates->setShadesVisible(true);
//![3]
//![4]
// axisX_CanNmStates->append("", 10);
// axisX_CanNmStates->append("", 20);
// axisX_CanNmStates->append("", 30);
// axisX_CanNmStates->setRange(0, 30);
axisY_CanNmStates->append(" NM_STATE_UNINIT", 2);
axisY_CanNmStates->append(" NM_STATE_BUS_SLEEP", 4);
axisY_CanNmStates->append("NM_STATE_PREPARE_BUS_SLEEP", 6);
axisY_CanNmStates->append(" NM_STATE_READY_SLEEP", 8);
axisY_CanNmStates->append(" NM_STATE_NORMAL_OPERATION", 10);
axisY_CanNmStates->append(" NM_STATE_REPEAT_MESSAGE", 12);
axisY_CanNmStates->setRange(0, 12);
//下边这一句代码的作用是让刻度线和刻度的label对齐
// axisX_CanNmStates->setLabelsPosition(QCategoryAxis::AxisLabelsPositionOnValue);
chart_CanNmStates->setAxisX(axisX_CanNmStates, series_CanNmStates);
chart_CanNmStates->setAxisY(axisY_CanNmStates, series_CanNmStates);
//![4]
ui->graphicsView_CanNmStates->setChart(chart_CanNmStates);
}
DialogCanNmStateAnalyze::~DialogCanNmStateAnalyze()
{
delete ui;
}
void DialogCanNmStateAnalyze::closeEvent(QCloseEvent *event)
{
//|窗口关闭之前需要的操作~
XCAN::isOpen_dialogCanNmStateAnalyze_ui = false;
}
QString DialogCanNmStateAnalyze::timeFormatConvert(uint32_t time)
{
QString timeStr;
int ss = 1000;
int mi = ss * 60;
int hh = mi * 60;
int dd = hh * 24;
long day = time / dd;
long hour = (time - day * dd) / hh;
long minute = (time - day * dd - hour * hh) / mi;
long second = (time - day * dd - hour * hh - minute * mi) / ss;
long milliSecond = time - day * dd - hour * hh - minute * mi - second * ss;
QString hou = QString::number(hour,10);
QString min = QString::number(minute,10);
QString sec = QString::number(second,10);
QString msec = QString::number(milliSecond,10);
//qDebug() << "minute:" << min << "second" << sec << "ms" << msec < if(hour>0){timeStr = hou + "h" + min + "m" + sec+ "s" + msec + "ms";}else if(minute>0){timeStr = min + "m" + sec+ "s" + msec + "ms";}else if(second>0){timeStr = sec+ "s" + msec + "ms";}else{timeStr = msec + "ms";}return timeStr;// ————————————————// 版权声明:本文为CSDN博主「ZONG_XP」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。// 原文链接:https://blog.csdn.net/zong596568821xp/article/details/79053491}void DialogCanNmStateAnalyze::setCanNetWorkStates(uint8_t states,uint32_t timestamp){int i = 0;QString dataTimeStampStr;uint8_t nmPreviousState = ((states>>4)&0x0f)*2+1;uint8_t nmCurrentState = (states&0x0f)*2+1;data_CanNmStates< data_TimeStamp< // 数据个数超过了最大数量,则删除最先接收到的数据,实现曲线向前移动while(data_CanNmStates.size() > 30){data_CanNmStates.removeFirst();data_TimeStamp.removeFirst();}series_CanNmStates->clear();axisX_CanNmStates->setRange(0, data_CanNmStates.size()+1);series_CanNmStates->append(0,(((data_CanNmStates.at(0)>>4)&0x0f)*2+1));for (i=0; i < data_CanNmStates.size(); ++i){if(i>0){dataTimeStampStr = timeFormatConvert(data_TimeStamp.at(i)-data_TimeStamp.at(i-1));}else{dataTimeStampStr = timeFormatConvert(data_TimeStamp.at(i));}nmPreviousState = ((data_CanNmStates.at(i)>>4)&0x0f)*2+1;nmCurrentState = (data_CanNmStates.at(i)&0x0f)*2+1;series_CanNmStates->append(i+1,nmPreviousState);series_CanNmStates->append(i+1,nmCurrentState);axisX_CanNmStates->append("T"+QString::number(i,10)+":"+dataTimeStampStr,i+1);qDebug()<<"dataTimeStampStr:"< }series_CanNmStates->append(i+1,nmCurrentState);}