diff options
-rw-r--r-- | examples/serialbus/can/mainwindow.cpp | 18 | ||||
-rw-r--r-- | examples/serialbus/can/mainwindow.h | 3 | ||||
-rw-r--r-- | examples/serialbus/can/mainwindow.ui | 24 | ||||
-rw-r--r-- | src/plugins/canbus/peakcan/peakcanbackend.cpp | 34 | ||||
-rw-r--r-- | src/plugins/canbus/tinycan/tinycanbackend.cpp | 49 | ||||
-rw-r--r-- | src/serialbus/doc/src/peakcan.qdoc | 17 | ||||
-rw-r--r-- | src/serialbus/doc/src/qtcanbus-backends.qdoc | 4 | ||||
-rw-r--r-- | src/serialbus/doc/src/socketcan.qdoc | 16 | ||||
-rw-r--r-- | src/serialbus/doc/src/tinycan.qdoc | 17 | ||||
-rw-r--r-- | src/serialbus/qcanbusdevice.cpp | 18 |
10 files changed, 123 insertions, 77 deletions
diff --git a/examples/serialbus/can/mainwindow.cpp b/examples/serialbus/can/mainwindow.cpp index 3266d79..198ee33 100644 --- a/examples/serialbus/can/mainwindow.cpp +++ b/examples/serialbus/can/mainwindow.cpp @@ -44,6 +44,7 @@ #include <QCanBusFrame> #include <QCanBus> +#include <QCloseEvent> #include <QTimer> #include <QtCore/qbytearray.h> @@ -146,8 +147,15 @@ void MainWindow::connectDevice() m_ui->sendMessagesBox->setEnabled(true); - showStatusMessage(tr("Backend: %1, Connected to: %2") - .arg(p.backendName).arg(p.deviceInterfaceName)); + QVariant bitRate = m_canDevice->configurationParameter(QCanBusDevice::BitRateKey); + if (bitRate.isValid()) { + showStatusMessage(tr("Backend: %1, connected to %2 at %3 kBit/s") + .arg(p.backendName).arg(p.deviceInterfaceName) + .arg(bitRate.toInt() / 1000)); + } else { + showStatusMessage(tr("Backend: %1, connected to %2") + .arg(p.backendName).arg(p.deviceInterfaceName)); + } } } @@ -173,6 +181,12 @@ void MainWindow::framesWritten(qint64 count) qDebug() << "Number of frames written:" << count; } +void MainWindow::closeEvent(QCloseEvent *event) +{ + m_connectDialog->close(); + event->accept(); +} + void MainWindow::checkMessages() { if (!m_canDevice) diff --git a/examples/serialbus/can/mainwindow.h b/examples/serialbus/can/mainwindow.h index aff6c94..de4d442 100644 --- a/examples/serialbus/can/mainwindow.h +++ b/examples/serialbus/can/mainwindow.h @@ -74,6 +74,9 @@ private Q_SLOTS: void disconnectDevice(); void framesWritten(qint64); +protected: + void closeEvent(QCloseEvent *event); + private: void showStatusMessage(const QString &message); void initActionsConnections(); diff --git a/examples/serialbus/can/mainwindow.ui b/examples/serialbus/can/mainwindow.ui index 8080ab4..f9d5781 100644 --- a/examples/serialbus/can/mainwindow.ui +++ b/examples/serialbus/can/mainwindow.ui @@ -26,7 +26,10 @@ <item row="0" column="0"> <widget class="QLabel" name="label"> <property name="text"> - <string>ID (hex)</string> + <string>&ID (hex)</string> + </property> + <property name="buddy"> + <cstring>idEdit</cstring> </property> </widget> </item> @@ -49,7 +52,7 @@ <item> <widget class="QRadioButton" name="dataFrame"> <property name="text"> - <string>Data</string> + <string>D&ata</string> </property> <property name="checked"> <bool>true</bool> @@ -66,7 +69,7 @@ <item> <widget class="QRadioButton" name="errorFrame"> <property name="text"> - <string>Error</string> + <string>&Error</string> </property> </widget> </item> @@ -76,7 +79,7 @@ <item row="0" column="3"> <widget class="QCheckBox" name="fdBox"> <property name="text"> - <string>Flexible Data-Rate + <string>&Flexible Data-Rate (64 byte payload)</string> </property> </widget> @@ -88,7 +91,7 @@ Otherwise, the standard format with 11 bit idenfier is used.</string> </property> <property name="text"> - <string>Extended Frame + <string>E&xtended Frame (29 bit identifier)</string> </property> </widget> @@ -96,7 +99,7 @@ Otherwise, the standard format with 11 bit idenfier is used.</string> <item row="2" column="3"> <widget class="QPushButton" name="sendButton"> <property name="text"> - <string>send</string> + <string>&Send</string> </property> </widget> </item> @@ -110,7 +113,10 @@ Otherwise, the standard format with 11 bit idenfier is used.</string> <item row="1" column="0"> <widget class="QLabel" name="label_2"> <property name="text"> - <string>Data (hex)</string> + <string>&Data (hex)</string> + </property> + <property name="buddy"> + <cstring>lineEdit</cstring> </property> </widget> </item> @@ -171,7 +177,7 @@ Otherwise, the standard format with 11 bit idenfier is used.</string> <x>0</x> <y>0</y> <width>551</width> - <height>25</height> + <height>21</height> </rect> </property> <widget class="QMenu" name="menuCalls"> @@ -185,7 +191,7 @@ Otherwise, the standard format with 11 bit idenfier is used.</string> </widget> <widget class="QMenu" name="menuHelp"> <property name="title"> - <string>Help</string> + <string>&Help</string> </property> <addaction name="actionAboutQt"/> </widget> diff --git a/src/plugins/canbus/peakcan/peakcanbackend.cpp b/src/plugins/canbus/peakcan/peakcanbackend.cpp index 4e733fc..06a2d4b 100644 --- a/src/plugins/canbus/peakcan/peakcanbackend.cpp +++ b/src/plugins/canbus/peakcan/peakcanbackend.cpp @@ -208,7 +208,8 @@ bool PeakCanBackendPrivate::open() const int bitrate = q->configurationParameter(QCanBusDevice::BitRateKey).toInt(); const int bitrateCode = bitrateCodeFromBitrate(bitrate); - if (TPCANStatus st = ::CAN_Initialize(channelIndex, bitrateCode, 0, 0, 0) != PCAN_ERROR_OK) { + const TPCANStatus st = ::CAN_Initialize(channelIndex, bitrateCode, 0, 0, 0); + if (st != PCAN_ERROR_OK) { q->setError(systemErrorString(st), QCanBusDevice::ConnectionError); return false; } @@ -223,9 +224,9 @@ bool PeakCanBackendPrivate::open() } #endif - if (TPCANStatus st = ::CAN_SetValue(channelIndex, PCAN_RECEIVE_EVENT, &readHandle, sizeof(readHandle)) - != PCAN_ERROR_OK) { - q->setError(systemErrorString(st), QCanBusDevice::ConnectionError); + const TPCANStatus err = ::CAN_SetValue(channelIndex, PCAN_RECEIVE_EVENT, &readHandle, sizeof(readHandle)); + if (err != PCAN_ERROR_OK) { + q->setError(systemErrorString(err), QCanBusDevice::ConnectionError); return false; } @@ -250,13 +251,13 @@ void PeakCanBackendPrivate::close() writeNotifier = nullptr; quint32 value = 0; - if (TPCANStatus st = ::CAN_SetValue(channelIndex, PCAN_RECEIVE_EVENT, &value, sizeof(value)) - != PCAN_ERROR_OK) { - emit q->setError(systemErrorString(st), QCanBusDevice::ConnectionError); - } + const TPCANStatus err = ::CAN_SetValue(channelIndex, PCAN_RECEIVE_EVENT, &value, sizeof(value)); + if (err != PCAN_ERROR_OK) + emit q->setError(systemErrorString(err), QCanBusDevice::ConnectionError); - if (TPCANStatus st = ::CAN_Uninitialize(channelIndex) != PCAN_ERROR_OK) - emit q->setError(systemErrorString(st), QCanBusDevice::ConnectionError); + const TPCANStatus st = ::CAN_Uninitialize(channelIndex); + if (st != PCAN_ERROR_OK) + q->setError(systemErrorString(st), QCanBusDevice::ConnectionError); #if defined(Q_OS_WIN32) if (readHandle && (readHandle != INVALID_HANDLE_VALUE)) { @@ -279,7 +280,8 @@ bool PeakCanBackendPrivate::setConfigurationParameter(int key, const QVariant &v case QCanBusDevice::BitRateKey: return verifyBitRate(value.toInt()); default: - q->setError(PeakCanBackend::tr("Unsupported configuration key"), QCanBusDevice::ConfigurationError); + q->setError(PeakCanBackend::tr("Unsupported configuration key: %1").arg(key), + QCanBusDevice::ConfigurationError); return false; } } @@ -367,7 +369,8 @@ void PeakCanBackendPrivate::startWrite() else ::memcpy(message.DATA, payload.constData(), sizeof(message.DATA)); - if (TPCANStatus st = ::CAN_Write(channelIndex, &message) != PCAN_ERROR_OK) + const TPCANStatus st = ::CAN_Write(channelIndex, &message); + if (st != PCAN_ERROR_OK) q->setError(systemErrorString(st), QCanBusDevice::WriteError); else emit q->framesWritten(qint64(1)); @@ -388,7 +391,8 @@ void PeakCanBackendPrivate::startRead() TPCANTimestamp timestamp; ::memset(×tamp, 0, sizeof(timestamp)); - if (TPCANStatus st = ::CAN_Read(channelIndex, &message, ×tamp) != PCAN_ERROR_OK) { + const TPCANStatus st = ::CAN_Read(channelIndex, &message, ×tamp); + if (st != PCAN_ERROR_OK) { if (st != PCAN_ERROR_XMTFULL) q->setError(systemErrorString(st), QCanBusDevice::ReadError); break; @@ -449,10 +453,8 @@ bool PeakCanBackend::open() Q_D(PeakCanBackend); if (!d->isOpen) { - if (!d->open()) { - close(); // sets UnconnectedState + if (!d->open()) return false; - } // apply all stored configurations except bitrate, because // the bitrate can not be applied after opening of device diff --git a/src/plugins/canbus/tinycan/tinycanbackend.cpp b/src/plugins/canbus/tinycan/tinycanbackend.cpp index 4a36918..2f9c191 100644 --- a/src/plugins/canbus/tinycan/tinycanbackend.cpp +++ b/src/plugins/canbus/tinycan/tinycanbackend.cpp @@ -171,21 +171,30 @@ bool TinyCanBackendPrivate::open() { Q_Q(TinyCanBackend); - char options[] = "AutoConnect=1;AutoReopen=0"; - if (int ret = ::CanSetOptions(options) < 0) { - q->setError(systemErrorString(ret), QCanBusDevice::CanBusError::ConnectionError); - return false; + { + char options[] = "AutoConnect=1;AutoReopen=0"; + const int ret = ::CanSetOptions(options); + if (ret < 0) { + q->setError(systemErrorString(ret), QCanBusDevice::CanBusError::ConnectionError); + return false; + } } - if (int ret = ::CanDeviceOpen(channelIndex, nullptr) < 0) { - q->setError(systemErrorString(ret), QCanBusDevice::CanBusError::ConnectionError); - return false; + { + const int ret = ::CanDeviceOpen(channelIndex, nullptr); + if (ret < 0) { + q->setError(systemErrorString(ret), QCanBusDevice::CanBusError::ConnectionError); + return false; + } } - if (int ret = ::CanSetMode(channelIndex, OP_CAN_START, CAN_CMD_ALL_CLEAR) < 0) { - q->setError(systemErrorString(ret), QCanBusDevice::CanBusError::ConnectionError); - ::CanDeviceClose(channelIndex); - return false; + { + const int ret = ::CanSetMode(channelIndex, OP_CAN_START, CAN_CMD_ALL_CLEAR); + if (ret < 0) { + q->setError(systemErrorString(ret), QCanBusDevice::CanBusError::ConnectionError); + ::CanDeviceClose(channelIndex); + return false; + } } writeNotifier = new WriteNotifier(this, q); @@ -202,7 +211,8 @@ void TinyCanBackendPrivate::close() delete writeNotifier; writeNotifier = nullptr; - if (int ret = ::CanDeviceClose(channelIndex) < 0) + const int ret = ::CanDeviceClose(channelIndex); + if (ret < 0) q->setError(systemErrorString(ret), QCanBusDevice::CanBusError::ConnectionError); isOpen = false; @@ -216,7 +226,8 @@ bool TinyCanBackendPrivate::setConfigurationParameter(int key, const QVariant &v case QCanBusDevice::BitRateKey: return setBitRate(value.toInt()); default: - q->setError(TinyCanBackend::tr("Unsupported configuration key"), QCanBusDevice::ConfigurationError); + q->setError(TinyCanBackend::tr("Unsupported configuration key: %1").arg(key), + QCanBusDevice::ConfigurationError); return false; } } @@ -342,7 +353,8 @@ void TinyCanBackendPrivate::startWrite() const qint32 messagesToWrite = 1; ::memcpy(message.Data.Bytes, payload.constData(), sizeof(message.Data.Bytes)); - if (int ret = ::CanTransmit(channelIndex, &message, messagesToWrite) < 0) + const int ret = ::CanTransmit(channelIndex, &message, messagesToWrite); + if (ret < 0) q->setError(systemErrorString(ret), QCanBusDevice::CanBusError::WriteError); else emit q->framesWritten(messagesToWrite); @@ -367,7 +379,8 @@ void TinyCanBackendPrivate::startRead() ::memset(&message, 0, sizeof(message)); const int messagesToRead = 1; - if (int ret = ::CanReceive(channelIndex, &message, messagesToRead) < 0) { + const int ret = ::CanReceive(channelIndex, &message, messagesToRead); + if (ret < 0) { q->setError(systemErrorString(ret), QCanBusDevice::CanBusError::ReadError); TDeviceStatus status; @@ -409,7 +422,8 @@ void TinyCanBackendPrivate::startupDriver() Q_Q(TinyCanBackend); if (driverRefCount == 0) { - if (int ret = ::CanInitDriver(nullptr) < 0) { + const int ret = ::CanInitDriver(nullptr); + if (ret < 0) { q->setError(systemErrorString(ret), QCanBusDevice::CanBusError::ConnectionError); return; } @@ -450,7 +464,8 @@ bool TinyCanBackendPrivate::setBitRate(int bitrate) } if (isOpen) { - if (int ret = ::CanSetSpeed(channelIndex, bitrateCode) < 0) { + const int ret = ::CanSetSpeed(channelIndex, bitrateCode); + if (ret < 0) { q->setError(systemErrorString(ret), QCanBusDevice::CanBusError::ConfigurationError); return false; } diff --git a/src/serialbus/doc/src/peakcan.qdoc b/src/serialbus/doc/src/peakcan.qdoc index 0a7ece3..8a973cb 100644 --- a/src/serialbus/doc/src/peakcan.qdoc +++ b/src/serialbus/doc/src/peakcan.qdoc @@ -31,7 +31,7 @@ \brief Overview of how to use the PeakCAN backend. The PeakCAN backend encapsulates the low-level API to work with the - \l{http://www.peak-system.com/}{PCAN} adapters. + \l{http://www.peak-system.com/}{PEAK-System} CAN adapters. \section1 Creating CAN Bus Devices @@ -43,10 +43,9 @@ } \endcode - Where, \e peakcan is the backend name. + Where \e peakcan is the backend name. - Further, it is necessary to request from QCanBus the device with the active PeakCan - interface: + Next, a connection to a specific interface can be established: \code QCanBusDevice *device = QCanBus::instance()->createDevice( @@ -54,13 +53,13 @@ device->connectDevice(); \endcode - Where, \e usbbus1 is the active CAN interface name. The PCAN API support eight USB - interfaces from \e usbbus1 to \e usbbus8 and eight PCI interfaces from \e pcibus1 to - \e pcibus8. + Where \e usbbus1 is the active CAN interface name. The PeakCAN backend supports + eight USB interfaces from \e usbbus1 to \e usbbus8 and eight PCI interfaces from + \e pcibus1 to \e pcibus8. \note Only the USB and PCI adapters are currently supported by this backend. - The device is now open and can be written and read: + The device is now open for writing and reading CAN frames: \code QCanBusFrame frame; @@ -86,7 +85,7 @@ \li Configuration parameter key \li Description \row - \li QDCanBusDevice::BitRateKey + \li QCanBusDevice::BitRateKey \li Determines the bit rate of the CAN bus connection. The following bit rates are supported: 5000, 10000, 20000, 33000, 47000, 50000, 83000, 95000, 100000, 125000, 250000, 500000, 800000, 1000000. Note that this configuration diff --git a/src/serialbus/doc/src/qtcanbus-backends.qdoc b/src/serialbus/doc/src/qtcanbus-backends.qdoc index fdb1cc6..0dca721 100644 --- a/src/serialbus/doc/src/qtcanbus-backends.qdoc +++ b/src/serialbus/doc/src/qtcanbus-backends.qdoc @@ -60,11 +60,11 @@ \row \li PEAK-System \li \l {Using PeakCAN Backend}{PeakCAN} (\c peakcan) - \li CAN bus backend using the PCAN adapters. + \li CAN bus backend using the PEAK CAN adapters. \row \li MHS Elektronik \li \l {Using TinyCAN Backend}{TinyCAN} (\c tinycan) - \li CAN bus backend using the Tiny-CAN adapters. + \li CAN bus backend using the MHS CAN adapters. \row \li Vector Informatik \li \l {Using VectorCAN Backend}{VectorCAN} (\c vectorcan) diff --git a/src/serialbus/doc/src/socketcan.qdoc b/src/serialbus/doc/src/socketcan.qdoc index a8c28bd..1b1f959 100644 --- a/src/serialbus/doc/src/socketcan.qdoc +++ b/src/serialbus/doc/src/socketcan.qdoc @@ -44,10 +44,9 @@ } \endcode - Where, \e socketcan is the backend name. + Where \e socketcan is the backend name. - Further, it is necessary to request from QCanBus the device with the active SocketCAN - interface: + Next, a connection to a specific interface can be established: \code QCanBusDevice *device = QCanBus::instance()->createDevice( @@ -55,10 +54,10 @@ device->connectDevice(); \endcode - Where, \e can0 is the active CAN interface name. CAN interfaces act like regular network interfaces - on Linux systems and can be discovered using \c ifconfig. + Where \e can0 is the active CAN interface name. CAN interfaces act like regular + network interfaces on Linux systems and can be discovered using \c ifconfig. - The device is now open and can be written and read: + The device is now open for writing and reading CAN frames: \code QCanBusFrame frame; @@ -104,7 +103,8 @@ \row \li QCanBusDevice::BitRateKey \li This configuration is not supported by the socketcan backend. However - it is possible to set the rate when configuring the CAN network interface using the \c {ip link} command. + it is possible to set the rate when configuring the CAN network interface using + the \c {ip link} command. \row \li QCanBusDevice::CanFdKey \li This configuration option determines whether CANFD frames may be sent or received. @@ -116,5 +116,5 @@ \snippet snippetmain.cpp SocketCan Filter Example - Efficient frame format and flexible data-rate are supported in SocketCAN. + Extended frame format and flexible data-rate are supported in SocketCAN. */ diff --git a/src/serialbus/doc/src/tinycan.qdoc b/src/serialbus/doc/src/tinycan.qdoc index b6e25c4..4ca4e53 100644 --- a/src/serialbus/doc/src/tinycan.qdoc +++ b/src/serialbus/doc/src/tinycan.qdoc @@ -31,7 +31,7 @@ \brief Overview of how to use the TinyCAN backend. The TinyCAN backend encapsulates the low-level API to work with the - \l{http://www.mhs-elektronik.de/}{Tiny-CAN} adapters. + \l{http://www.mhs-elektronik.de/}{MHS Elektronik} CAN adapters. \section1 Creating CAN Bus Devices @@ -43,10 +43,9 @@ } \endcode - Where, \e tinycan is the backend name. + Where \e tinycan is the backend name. - Further, it is necessary to request from QCanBus the device with the active Tiny-CAN - interface: + Next, a connection to a specific interface can be established: \code QCanBusDevice *device = QCanBus::instance()->createDevice( @@ -54,12 +53,12 @@ device->connectDevice(); \endcode - Where, \e channela is the active CAN interface name. The Tiny-CAN API provides only two - interfaces \e channela and \e channelb. + Where \e channela is the active CAN interface name. The TinyCAN backend + provides two interfaces, \e channela and \e channelb. \note Only the USB adapters are currently supported by this backend. - The device is now open and can be written and read: + The device is now open for writing and reading CAN frames: \code QCanBusFrame frame; @@ -77,7 +76,7 @@ QCanBusFrame frame = device->readFrame(); \endcode - Tiny-CAN supports the following configurations that can be controlled through + TinyCAN supports the following configurations that can be controlled through \l {QCanBusDevice::}{setConfigurationParameter()}: \table @@ -85,7 +84,7 @@ \li Configuration parameter key \li Description \row - \li QDCanBusDevice::BitRateKey + \li QCanBusDevice::BitRateKey \li Determines the bit rate of the CAN bus connection. The following bit rates are supported: 10000, 20000, 50000, 100000, 125000, 250000, 500000, 800000, 1000000. \endtable diff --git a/src/serialbus/qcanbusdevice.cpp b/src/serialbus/qcanbusdevice.cpp index fb8dee3..e096a1e 100644 --- a/src/serialbus/qcanbusdevice.cpp +++ b/src/serialbus/qcanbusdevice.cpp @@ -45,8 +45,6 @@ #include <QtCore/qscopedvaluerollback.h> #include <QtCore/qtimer.h> -#define SOCKET_CAN_MTU 72 - QT_BEGIN_NAMESPACE /*! @@ -301,8 +299,7 @@ bool QCanBusDevice::hasOutgoingFrames() const \sa configurationParameter() */ -void QCanBusDevice::setConfigurationParameter(int key, - const QVariant &value) +void QCanBusDevice::setConfigurationParameter(int key, const QVariant &value) { Q_D(QCanBusDevice); @@ -628,8 +625,11 @@ bool QCanBusDevice::connectDevice() { Q_D(QCanBusDevice); - if (d->state != QCanBusDevice::UnconnectedState) + if (d->state != QCanBusDevice::UnconnectedState) { + setError(tr("Can not connect an already connected device"), + QCanBusDevice::ConnectionError); return false; + } setState(ConnectingState); @@ -650,6 +650,14 @@ bool QCanBusDevice::connectDevice() */ void QCanBusDevice::disconnectDevice() { + Q_D(QCanBusDevice); + + if (d->state == QCanBusDevice::UnconnectedState + || d->state == QCanBusDevice::ClosingState) { + qWarning("Can not disconnect an unconnected device"); + return; + } + setState(QCanBusDevice::ClosingState); //Unconnected is set by backend -> might be delayed by event loop |