diff --git a/src/candiagnostictool.cpp b/src/candiagnostictool.cpp index ff9dee2..83b68fc 100644 --- a/src/candiagnostictool.cpp +++ b/src/candiagnostictool.cpp @@ -35,7 +35,13 @@ CanDiagnosticTool::CanDiagnosticTool(QWidget* parent) : QMainWindow(parent) { canBusView = new CanBusView(leftColumn); leftColumnLayout->addWidget(canBusView); + options.SetRequestCanId(0); + options.SetResponseCanId(1); + options.SetTimeout(1000); + options.SetMaxRetries(5); + diagnosticView = new DiagnosticView(central); + diagnosticView->SetProtocolOptions(options); mainLayout->addWidget(diagnosticView, 0, 1); connect(canBusView, &CanBusView::BusConnected, diagnosticView, @@ -52,7 +58,9 @@ CanDiagnosticTool::CanDiagnosticTool(QWidget* parent) : QMainWindow(parent) { void CanDiagnosticTool::ExecOptionsDialog() { OptionsDialog dialog(this); + dialog.SetOptions(options); connect(&dialog, &OptionsDialog::OptionsConfigured, diagnosticView, &DiagnosticView::SetProtocolOptions); dialog.exec(); + options = dialog.GetOptions(); } diff --git a/src/candiagnostictool.h b/src/candiagnostictool.h index 096dcdb..dcc1f47 100644 --- a/src/candiagnostictool.h +++ b/src/candiagnostictool.h @@ -8,6 +8,7 @@ #include #include +#include #include class CanDiagnosticTool : public QMainWindow { @@ -20,6 +21,7 @@ class CanDiagnosticTool : public QMainWindow { void ExecOptionsDialog(); private: + DiagnosticProtocolOptions options; QAction* readAction; QAction* resetAction; QAction* searchAction; diff --git a/src/diagnosticprotocol.cpp b/src/diagnosticprotocol.cpp index 1ee5698..b6462d2 100644 --- a/src/diagnosticprotocol.cpp +++ b/src/diagnosticprotocol.cpp @@ -10,15 +10,15 @@ DiagnosticProtocol::DiagnosticProtocol( void DiagnosticProtocol::SetCanFilter() { if (!canBusDevicePtr_) return; - // Установка фильтра по идентификатору сообщений протокола + QList filterList; QCanBusDevice::Filter filter; filter.frameId = options_.GetResponseCanId(); - filter.frameIdMask = 0x7fffffff; - filter.format = QCanBusDevice::Filter::MatchBaseFormat; + filter.frameIdMask = 0x1fffffffu; + filter.format = QCanBusDevice::Filter::MatchBaseAndExtendedFormat; filter.type = QCanBusFrame::DataFrame; - canBusDevicePtr_->setConfigurationParameter( - QCanBusDevice::RawFilterKey, - QVariant::fromValue(QList{filter})); + filterList.append(filter); + canBusDevicePtr_->setConfigurationParameter(QCanBusDevice::RawFilterKey, + QVariant::fromValue(filterList)); } QByteArray DiagnosticProtocol::SendRequest(DiagnosticRequestCode code) const { @@ -34,8 +34,11 @@ QByteArray DiagnosticProtocol::SendRequest(DiagnosticRequestCode code) const { QCanBusFrame(options_.GetRequestCanId(), QByteArray(1, currentCode))); if (!canBusDevicePtr_->waitForFramesReceived(options_.GetTimeout())) break; while (canBusDevicePtr_->framesAvailable() > 0) { - auto payload = canBusDevicePtr_->readFrame().payload(); - if (payload.startsWith(currentCode)) return payload.remove(0, 1); + auto frame = canBusDevicePtr_->readFrame(); + if (frame.frameId() == options_.GetResponseCanId()) { + auto payload = frame.payload(); + if (payload.startsWith(currentCode)) return payload.remove(0, 1); + } } ++retries; } while (retries <= options_.GetMaxRetries()); diff --git a/src/diagnosticprotocol.h b/src/diagnosticprotocol.h index 7da426d..894b401 100644 --- a/src/diagnosticprotocol.h +++ b/src/diagnosticprotocol.h @@ -2,7 +2,6 @@ #include #include - #include #include @@ -11,15 +10,15 @@ class DiagnosticProtocolOptions { public: - std::uint32_t GetRequestCanId() const noexcept { return requestCanId_; } + quint32 GetRequestCanId() const noexcept { return requestCanId_; } - void SetRequestCanId(std::uint32_t requestCanId) noexcept { + void SetRequestCanId(quint32 requestCanId) noexcept { requestCanId_ = requestCanId; } - std::uint32_t GetResponseCanId() const noexcept { return responseCanId_; } + quint32 GetResponseCanId() const noexcept { return responseCanId_; } - void SetResponseCanId(std::uint32_t responseCanId) noexcept { + void SetResponseCanId(quint32 responseCanId) noexcept { responseCanId_ = responseCanId; } @@ -32,8 +31,8 @@ class DiagnosticProtocolOptions { void SetMaxRetries(int maxRetries) noexcept { maxRetries_ = maxRetries; } private: - std::uint32_t requestCanId_; - std::uint32_t responseCanId_; + quint32 requestCanId_; + quint32 responseCanId_; int timeout_; int maxRetries_; }; @@ -71,13 +70,12 @@ class DiagnosticProtocol { void ResetErrors() const; - void SetCanBusDevice( - std::shared_ptr canBusDevicePtr) noexcept { + void SetCanBusDevice(std::shared_ptr canBusDevicePtr) { canBusDevicePtr_ = canBusDevicePtr; SetCanFilter(); } - void SetOptions(const DiagnosticProtocolOptions& options) noexcept { + void SetOptions(const DiagnosticProtocolOptions& options) { options_ = options; SetCanFilter(); } diff --git a/src/diagnosticview.cpp b/src/diagnosticview.cpp index df73cc5..14d50aa 100644 --- a/src/diagnosticview.cpp +++ b/src/diagnosticview.cpp @@ -32,12 +32,6 @@ DiagnosticView::DiagnosticView(QWidget* parent) : QWidget(parent) { errorsAreaLayout->setAlignment(Qt::Alignment(Qt::AlignTop)); errorsAreaContents->setLayout(errorsAreaLayout); - DiagnosticProtocolOptions options; - options.SetRequestCanId(0x0); - options.SetResponseCanId(0x1); - options.SetMaxRetries(5); - options.SetTimeout(1000); - protocol.SetOptions(options); protocol.SetErrorsList(JsonErrorsReader().ReadErrorsList(QStringLiteral("device_errors.json"))); } @@ -50,8 +44,6 @@ void DiagnosticView::SetCanBusDevice( void DiagnosticView::SetProtocolOptions(const DiagnosticProtocolOptions& options) { protocol.SetOptions(options); - SearchDevice(); - ReadErrors(); } void DiagnosticView::SearchDevice() { @@ -68,8 +60,8 @@ void DiagnosticView::SearchDevice() { void DiagnosticView::ReadErrors() { try { - auto errors = protocol.ReadErrors(); ClearErrorsArea(); + auto errors = protocol.ReadErrors(); for (const auto& error : errors) { errorsAreaLayout->addWidget( new DeviceErrorView(error, errorsAreaContents)); diff --git a/src/optionsdialog.cpp b/src/optionsdialog.cpp index cea0b70..aaa3772 100644 --- a/src/optionsdialog.cpp +++ b/src/optionsdialog.cpp @@ -43,12 +43,20 @@ OptionsDialog::OptionsDialog(QWidget* parent) : QDialog(parent) { connect(buttonOk, &QPushButton::clicked, this, &OptionsDialog::accept); } +void OptionsDialog::SetOptions( + const DiagnosticProtocolOptions& options) noexcept { + options_ = options; + editRequestId->setText(QString::number(options_.GetRequestCanId(), 16)); + editResponseId->setText(QString::number(options_.GetResponseCanId(), 16)); + spinTimeout->setValue(options_.GetTimeout()); + spinMaxRetries->setValue(options_.GetMaxRetries()); +} + void OptionsDialog::accept() { - DiagnosticProtocolOptions options; - options.SetRequestCanId(editRequestId->text().toUInt()); - options.SetResponseCanId(editResponseId->text().toUInt()); - options.SetTimeout(spinTimeout->value()); - options.SetMaxRetries(spinMaxRetries->value()); - emit OptionsConfigured(options); + options_.SetRequestCanId(editRequestId->text().toUInt(nullptr, 16)); + options_.SetResponseCanId(editResponseId->text().toUInt(nullptr, 16)); + options_.SetTimeout(spinTimeout->value()); + options_.SetMaxRetries(spinMaxRetries->value()); + emit OptionsConfigured(options_); QDialog::accept(); } diff --git a/src/optionsdialog.h b/src/optionsdialog.h index 8e3d116..dc0d204 100644 --- a/src/optionsdialog.h +++ b/src/optionsdialog.h @@ -16,12 +16,17 @@ class OptionsDialog : public QDialog { public: OptionsDialog(QWidget *parent = Q_NULLPTR); + void SetOptions(const DiagnosticProtocolOptions& options) noexcept; + + DiagnosticProtocolOptions GetOptions() const noexcept { return options_; } + void accept() override; signals: void OptionsConfigured(const DiagnosticProtocolOptions&); private: + DiagnosticProtocolOptions options_; HexValidator* hexValidator; QFormLayout* formLayout; QLineEdit* editRequestId;