#include "connectdialog.h"
#include "ui_connectdialog.h"
#include <QCanBus>
ConnectDialog::ConnectDialog(QWidget *parent) :
QDialog(parent),
m_ui(new Ui::ConnectDialog)
{
m_ui->setupUi(this);
m_ui->errorFilterEdit->setValidator(new QIntValidator(0, 0x1FFFFFFFU, this));
m_ui->loopbackBox->addItem(tr("unspecified"), QVariant());
m_ui->loopbackBox->addItem(tr("false"), QVariant(false));
m_ui->loopbackBox->addItem(tr("true"), QVariant(true));
m_ui->receiveOwnBox->addItem(tr("unspecified"), QVariant());
m_ui->receiveOwnBox->addItem(tr("false"), QVariant(false));
m_ui->receiveOwnBox->addItem(tr("true"), QVariant(true));
m_ui->canFdBox->addItem(tr("false"), QVariant(false));
m_ui->canFdBox->addItem(tr("true"), QVariant(true));
m_ui->dataBitrateBox->setFlexibleDateRateEnabled(true);
connect(m_ui->ok_Button, &QPushButton::clicked, this, &ConnectDialog::ok);
connect(m_ui->cancelButton, &QPushButton::clicked, this, &ConnectDialog::cancel);
connect(m_ui->useConfigurationBox, &QCheckBox::clicked,
m_ui->configurationBox, &QGroupBox::setEnabled);
connect(m_ui->pluginListBox, &QComboBox::currentTextChanged,
this, &ConnectDialog::pluginChanged);
connect(m_ui->interfaceListBox, &QComboBox::currentTextChanged,
this, &ConnectDialog::interfaceChanged);
m_ui->rawFilterEdit->hide();
m_ui->rawFilterLabel->hide();
m_ui->pluginListBox->addItems(QCanBus::instance()->plugins());
updateSettings();
}
ConnectDialog::~ConnectDialog()
{
delete m_ui;
}
ConnectDialog::Settings ConnectDialog::settings() const
{
return m_currentSettings;
}
void ConnectDialog::pluginChanged(const QString &plugin)
{
m_ui->interfaceListBox->clear();
m_interfaces = QCanBus::instance()->availableDevices(plugin);
for (const QCanBusDeviceInfo &info : qAsConst(m_interfaces))
m_ui->interfaceListBox->addItem(info.name());
}
void ConnectDialog::interfaceChanged(const QString &interface)
{
m_ui->isVirtual->setChecked(false);
m_ui->isFlexibleDataRateCapable->setChecked(false);
for (const QCanBusDeviceInfo &info : qAsConst(m_interfaces)) {
if (info.name() == interface) {
m_ui->descriptionLabel->setText(info.description());
QString serialNumber = info.serialNumber();
if (serialNumber.isEmpty())
serialNumber = tr("n/a");
m_ui->serialNumberLabel->setText(tr("Serial: %1").arg(serialNumber));
m_ui->channelLabel->setText(tr("Channel: %1").arg(info.channel()));
m_ui->isVirtual->setChecked(info.isVirtual());
m_ui->isFlexibleDataRateCapable->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 &item : qAsConst(m_currentSettings.configurations)) {
if (item.first == key) {
result = item.second;
break;
}
}
if (result.isNull() && (
key == QCanBusDevice::LoopbackKey ||
key == QCanBusDevice::ReceiveOwnKey)) {
return tr("unspecified");
}
return result.toString();
}
void ConnectDialog::revertSettings()
{
m_ui->pluginListBox->setCurrentText(m_currentSettings.pluginName);
m_ui->interfaceListBox->setCurrentText(m_currentSettings.deviceInterfaceName);
m_ui->useConfigurationBox->setChecked(m_currentSettings.useConfigurationEnabled);
QString value = configurationValue(QCanBusDevice::LoopbackKey);
m_ui->loopbackBox->setCurrentText(value);
value = configurationValue(QCanBusDevice::ReceiveOwnKey);
m_ui->receiveOwnBox->setCurrentText(value);
value = configurationValue(QCanBusDevice::ErrorFilterKey);
m_ui->errorFilterEdit->setText(value);
value = configurationValue(QCanBusDevice::BitRateKey);
m_ui->bitrateBox->setCurrentText(value);
value = configurationValue(QCanBusDevice::CanFdKey);
m_ui->canFdBox->setCurrentText(value);
value = configurationValue(QCanBusDevice::DataBitRateKey);
m_ui->dataBitrateBox->setCurrentText(value);
}
void ConnectDialog::updateSettings()
{
m_currentSettings.pluginName = m_ui->pluginListBox->currentText();
m_currentSettings.deviceInterfaceName = m_ui->interfaceListBox->currentText();
m_currentSettings.useConfigurationEnabled = m_ui->useConfigurationBox->isChecked();
if (m_currentSettings.useConfigurationEnabled) {
m_currentSettings.configurations.clear();
// process LoopBack
if (m_ui->loopbackBox->currentIndex() != 0) {
ConfigurationItem item;
item.first = QCanBusDevice::LoopbackKey;
item.second = m_ui->loopbackBox->currentData();
m_currentSettings.configurations.append(item);
}
// process ReceiveOwnKey
if (m_ui->receiveOwnBox->currentIndex() != 0) {
ConfigurationItem item;
item.first = QCanBusDevice::ReceiveOwnKey;
item.second = m_ui->receiveOwnBox->currentData();
m_currentSettings.configurations.append(item);
}
// process error filter
if (!m_ui->errorFilterEdit->text().isEmpty()) {
QString value = m_ui->errorFilterEdit->text();
bool ok = false;
int dec = value.toInt(&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->rawFilterEdit->text().isEmpty()) {
//TODO current ui not sfficient to reflect this param
}
// process bitrate
const int bitrate = m_ui->bitrateBox->bitRate();
if (bitrate > 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->canFdBox->currentData();
m_currentSettings.configurations.append(fdItem);
// process data bitrate
const int dataBitrate = m_ui->dataBitrateBox->bitRate();
if (dataBitrate > 0) {
const ConfigurationItem item(QCanBusDevice::DataBitRateKey, QVariant(dataBitrate));
m_currentSettings.configurations.append(item);
}
}
}
QT实现CAN通信
精选 原创
©著作权归作者所有:来自51CTO博客作者LazyUpdate的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:Qt代码实现上下左右布局
下一篇:什么是数字量模拟量
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
详解CAN总线:CAN总线通信优先级机制
本篇文章将讲解仲裁段的另一个重要功能:通信优先级。
1024程序员节 CAN总线 CAN总线通信优先级机制 详解CAN总线 汽车电子 -
详解CAN总线:高速CAN总线和低速CAN总线的特性
在ISO 11898-2和ISO 11898-3中分别规定了两种CAN总线结构:高速
ISO 11898-2 ISO 11898-3 CAN总线 汽车电子 嵌入式开发 -
can总线介绍
can 协议报文;硬件信号,读写 ;过滤。
can 知乎 Acceptance linux -
详解CAN总线:CAN总线故障界定与管理
CAN总线故障界定的目标是实现数据传输系统即使在节点发生故障的情况下也能维持很高的可用性。
CAN总线 CAN总线故障界定与管理 CAN错误类型 汽车电子 通信协议 -
can总线资料
应知识星球的同学要求,整理了一些can总线资料。在公众号后台回复 【can总线】获取资料截图推荐阅读:专辑|Linux文章汇总专辑|程序人生专辑|C语言我的知识小密圈 ...
portlet https 软件开发 ssl vim -
浅谈CAN总线
在前面的《浅谈I2C总线》和《
CAN 总线 协议 数据帧 数据