CAN Bus example▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the QtSerialBus module.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#include
"connectdialog.h"
#include
"ui_connectdialog.h"
#include <QCanBus>
ConnectDialog::
ConnectDialog(QWidget *
parent) :
QDialog(parent),
m_ui(new
Ui::
ConnectDialog)
{
m_ui-&
gt;setupUi(this
);
m_ui-&
gt;errorFilterEdit-&
gt;setValidator(new
QIntValidator(0
, 0x1FFFFFFF
U, this
));
m_ui-&
gt;loopbackBox-&
gt;addItem(tr("unspecified"
), QVariant());
m_ui-&
gt;loopbackBox-&
gt;addItem(tr("false"
), QVariant(false
));
m_ui-&
gt;loopbackBox-&
gt;addItem(tr("true"
), QVariant(true
));
m_ui-&
gt;receiveOwnBox-&
gt;addItem(tr("unspecified"
), QVariant());
m_ui-&
gt;receiveOwnBox-&
gt;addItem(tr("false"
), QVariant(false
));
m_ui-&
gt;receiveOwnBox-&
gt;addItem(tr("true"
), QVariant(true
));
m_ui-&
gt;canFdBox-&
gt;addItem(tr("false"
), QVariant(false
));
m_ui-&
gt;canFdBox-&
gt;addItem(tr("true"
), QVariant(true
));
m_ui-&
gt;dataBitrateBox-&
gt;setFlexibleDateRateEnabled(true
);
connect(m_ui-&
gt;okButton, &
amp;QPushButton::
clicked, this
, &
amp;ConnectDialog::
ok);
connect(m_ui-&
gt;cancelButton, &
amp;QPushButton::
clicked, this
, &
amp;ConnectDialog::
cancel);
connect(m_ui-&
gt;useConfigurationBox, &
amp;QCheckBox::
clicked,
m_ui-&
gt;configurationBox, &
amp;QGroupBox::
setEnabled);
connect(m_ui-&
gt;pluginListBox, &
amp;QComboBox::
currentTextChanged,
this
, &
amp;ConnectDialog::
pluginChanged);
connect(m_ui-&
gt;interfaceListBox, &
amp;QComboBox::
currentTextChanged,
this
, &
amp;ConnectDialog::
interfaceChanged);
m_ui-&
gt;rawFilterEdit-&
gt;hide();
m_ui-&
gt;rawFilterLabel-&
gt;hide();
m_ui-&
gt;pluginListBox-&
gt;addItems(QCanBus::
instance()-&
gt;plugins());
updateSettings();
}
ConnectDialog::
~
ConnectDialog()
{
delete
m_ui;
}
ConnectDialog::
Settings ConnectDialog::
settings() const
{
return
m_currentSettings;
}
void
ConnectDialog::
pluginChanged(const
QString &
amp;plugin)
{
m_ui-&
gt;interfaceListBox-&
gt;clear();
m_interfaces =
QCanBus::
instance()-&
gt;availableDevices(plugin);
for
(const
QCanBusDeviceInfo &
amp;info : qAsConst(m_interfaces))
m_ui-&
gt;interfaceListBox-&
gt;addItem(info.name());
}
void
ConnectDialog::
interfaceChanged(const
QString &
amp;interface)
{
m_ui-&
gt;isVirtual-&
gt;setChecked(false
);
m_ui-&
gt;isFlexibleDataRateCapable-&
gt;setChecked(false
);
for
(const
QCanBusDeviceInfo &
amp;info : qAsConst(m_interfaces)) {
if
(info.name() ==
interface) {
m_ui-&
gt;descriptionLabel-&
gt;setText(info.description());
QString serialNumber =
info.serialNumber();
if
(serialNumber.isEmpty())
serialNumber =
tr("n/a"
);
m_ui-&
gt;serialNumberLabel-&
gt;setText(tr("Serial: %1"
).arg(serialNumber));
m_ui-&
gt;channelLabel-&
gt;setText(tr("Channel: %1"
).arg(info.channel()));
m_ui-&
gt;isVirtual-&
gt;setChecked(info.isVirtual());
m_ui-&
gt;isFlexibleDataRateCapable-&
gt;setChecked(info.hasFlexibleDataRate());
break
;
}
}
}
void
ConnectDialog::
ok()
{
updateSettings();
accept();
}
void
ConnectDialog::
cancel()
{
revertSettings();
reject();
}
QString ConnectDialog::
configurationValue(QCanBusDevice::
ConfigurationKey key)
{
QVariant result;
for
(const
ConfigurationItem &
amp;item : qAsConst(m_currentSettings.configurations)) {
if
(item.first ==
key) {
result =
item.second;
break
;
}
}
if
(result.isNull() &
amp;&
amp; (
key ==
QCanBusDevice::
LoopbackKey ||
key ==
QCanBusDevice::
ReceiveOwnKey)) {
return
tr("unspecified"
);
}
return
result.toString();
}
void
ConnectDialog::
revertSettings()
{
m_ui-&
gt;pluginListBox-&
gt;setCurrentText(m_currentSettings.pluginName);
m_ui-&
gt;interfaceListBox-&
gt;setCurrentText(m_currentSettings.deviceInterfaceName);
m_ui-&
gt;useConfigurationBox-&
gt;setChecked(m_currentSettings.useConfigurationEnabled);
QString value =
configurationValue(QCanBusDevice::
LoopbackKey);
m_ui-&
gt;loopbackBox-&
gt;setCurrentText(value);
value =
configurationValue(QCanBusDevice::
ReceiveOwnKey);
m_ui-&
gt;receiveOwnBox-&
gt;setCurrentText(value);
value =
configurationValue(QCanBusDevice::
ErrorFilterKey);
m_ui-&
gt;errorFilterEdit-&
gt;setText(value);
value =
configurationValue(QCanBusDevice::
BitRateKey);
m_ui-&
gt;bitrateBox-&
gt;setCurrentText(value);
value =
configurationValue(QCanBusDevice::
CanFdKey);
m_ui-&
gt;canFdBox-&
gt;setCurrentText(value);
value =
configurationValue(QCanBusDevice::
DataBitRateKey);
m_ui-&
gt;dataBitrateBox-&
gt;setCurrentText(value);
}
void
ConnectDialog::
updateSettings()
{
m_currentSettings.pluginName =
m_ui-&
gt;pluginListBox-&
gt;currentText();
m_currentSettings.deviceInterfaceName =
m_ui-&
gt;interfaceListBox-&
gt;currentText();
m_currentSettings.useConfigurationEnabled =
m_ui-&
gt;useConfigurationBox-&
gt;isChecked();
if
(m_currentSettings.useConfigurationEnabled) {
m_currentSettings.configurations.clear();
// process LoopBack
if
(m_ui-&
gt;loopbackBox-&
gt;currentIndex() !=
0
) {
ConfigurationItem item;
item.first =
QCanBusDevice::
LoopbackKey;
item.second =
m_ui-&
gt;loopbackBox-&
gt;currentData();
m_currentSettings.configurations.append(item);
}
// process ReceiveOwnKey
if
(m_ui-&
gt;receiveOwnBox-&
gt;currentIndex() !=
0
) {
ConfigurationItem item;
item.first =
QCanBusDevice::
ReceiveOwnKey;
item.second =
m_ui-&
gt;receiveOwnBox-&
gt;currentData();
m_currentSettings.configurations.append(item);
}
// process error filter
if
(!
m_ui-&
gt;errorFilterEdit-&
gt;text().isEmpty()) {
QString value =
m_ui-&
gt;errorFilterEdit-&
gt;text();
bool
ok =
false
;
int
dec =
value.toInt(&
amp;ok);
if
(ok) {
ConfigurationItem item;
item.first =
QCanBusDevice::
ErrorFilterKey;
item.second =
QVariant::
fromValue(QCanBusFrame::
FrameErrors(dec));
m_currentSettings.configurations.append(item);
}
}
// process raw filter list
if
(!
m_ui-&
gt;rawFilterEdit-&
gt;text().isEmpty()) {
//TODO current ui not sfficient to reflect this param
}
// process bitrate
const
int
bitrate =
m_ui-&
gt;bitrateBox-&
gt;bitRate();
if
(bitrate &
gt; 0
) {
const
ConfigurationItem item(QCanBusDevice::
BitRateKey, QVariant(bitrate));
m_currentSettings.configurations.append(item);
}
// process CAN FD setting
ConfigurationItem fdItem;
fdItem.first =
QCanBusDevice::
CanFdKey;
fdItem.second =
m_ui-&
gt;canFdBox-&
gt;currentData();
m_currentSettings.configurations.append(fdItem);
// process data bitrate
const
int
dataBitrate =
m_ui-&
gt;dataBitrateBox-&
gt;bitRate();
if
(dataBitrate &
gt; 0
) {
const
ConfigurationItem item(QCanBusDevice::
DataBitRateKey, QVariant(dataBitrate));
m_currentSettings.configurations.append(item);
}
}
}