Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions CLX000CanBus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
QList<QCanBusDeviceInfo> CLX000CanBus::interfaces() {
QList<QCanBusDeviceInfo> result;

static QString const description("CLX000");
static QString const description(QStringLiteral("CLX000"));

// Find all available USB-CDC devices.
auto allAvailablePorts = QSerialPortInfo::availablePorts();

for(auto const& portInfo: allAvailablePorts) {
for(auto const& portInfo: qAsConst(allAvailablePorts)) {
// Remove all non-CDC ports.
if(!portInfo.hasVendorIdentifier() || !portInfo.hasProductIdentifier()) {
continue;
Expand All @@ -31,12 +31,14 @@ QList<QCanBusDeviceInfo> CLX000CanBus::interfaces() {
// Create new interface description for this.
result.push_back(
createDeviceInfo(
QStringLiteral("clx000can"), // eg. from plugin.json
portInfo.portName(),
portInfo.serialNumber(),
description,
0,
false,
false
QString(), // alias (empty)
0, // channel
false, // isVirtual
false // isFlexibleDataRateCapable
)
);
}
Expand Down Expand Up @@ -94,7 +96,7 @@ void CLX000CanBus::attemptParse() {

}

bool CLX000CanBus::attemptParseSequence(QByteArray sequence) {
bool CLX000CanBus::attemptParseSequence(const QByteArray &sequence) {
QCanBusFrame frame;
QByteArray destuffed;

Expand Down Expand Up @@ -202,7 +204,7 @@ bool CLX000CanBus::packFrame(QCanBusFrame const& frame, QByteArray &packedFrame)

/* Escape any 0x7E sequences */
packedFrame.append(0x07E);
for (char const& byteValue: payloadArray) {
for (char const& byteValue: qAsConst(payloadArray)) {
if( byteValue == 0x7E ) {
packedFrame.append(static_cast<quint8>(0x7D));
packedFrame.append(static_cast<quint8>(byteValue ^ 0b00100000) );
Expand Down
3 changes: 2 additions & 1 deletion CLX000CanBus.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
#include <QtSerialPort/QSerialPort>

class CLX000CanBus : public QCanBusDevice {
Q_OBJECT
public:
static QList<QCanBusDeviceInfo> interfaces();
protected:
QQueue<char> queue;
QVector<QCanBusFrame> frames;
void attemptParse();
bool attemptParseSequence(QByteArray sequence);
bool attemptParseSequence(const QByteArray &sequence);
bool packFrame(QCanBusFrame const& frame, QByteArray &packedFrame);
quint64 frameCount;
};
6 changes: 3 additions & 3 deletions CLX000CanBusUSB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>

CLX000CanBusUSB::CLX000CanBusUSB(QString interface) : port(interface) {
CLX000CanBusUSB::CLX000CanBusUSB(const QString &interface) : port(interface) {
port.setReadBufferSize(1024*1024*10);

connect(
&port,
SIGNAL(readyRead()),
&QIODevice::readyRead,
this,
SLOT(dataAvailable())
&CLX000CanBusUSB::dataAvailable
);

queue.reserve(1024*1024);
Expand Down
2 changes: 1 addition & 1 deletion CLX000CanBusUSB.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class CLX000CanBusUSB : public CLX000CanBus {
Q_OBJECT
public:
explicit CLX000CanBusUSB(QString interface);
explicit CLX000CanBusUSB(const QString &interface);

bool open() override;
void close() override;
Expand Down
6 changes: 3 additions & 3 deletions CLX000Plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
#include <QString>
#include <QtPlugin>
#include <QtCore/QList>
#include <QtSerialBus/QCanBusFactoryV2>
#include <QtSerialBus/QCanBusFactory>

class CLX000CanBusPlugin : public QObject, public QCanBusFactoryV2
class CLX000CanBusPlugin : public QObject, public QCanBusFactory
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QCanBusFactory" FILE "plugin.json")
Q_INTERFACES(QCanBusFactoryV2)
Q_INTERFACES(QCanBusFactory)

public:
QList<QCanBusDeviceInfo> availableDevices(QString *errorMessage) const override;
Expand Down
28 changes: 13 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,63 +1,61 @@
cmake_minimum_required(VERSION 3.15)
project(clx000canbus)
cmake_minimum_required(VERSION 3.16)
project(clx000canbus LANGUAGES CXX)

set(CMAKE_VERBOSE_MAKEFILE ON)

enable_language(CXX)

set(RELEASE_NAME clx000canbus)

# Find required Qt dependencies.
set(REQUIRED_QT5_COMPONENTS
set(REQUIRED_QT_COMPONENTS
Core
SerialBus
SerialPort
)
)

find_package(Qt5 COMPONENTS ${REQUIRED_QT5_COMPONENTS} REQUIRED)
find_package(Qt6 COMPONENTS ${REQUIRED_QT_COMPONENTS} REQUIRED)

# All core source and header files, sorted alphabetically.
set(SOURCE_FILES
CLX000CanBus.cpp
CLX000CanBusUSB.cpp
CLX000Plugin.cpp
CRC16.cpp
)
)

set(HEADERS_FILES
CLX000CanBus.h
CLX000CanBusUSB.h
CLX000Plugin.h
CRC16.h
)
)

set(OTHER_FILES
plugin.json
)
)

add_library(${RELEASE_NAME} MODULE ${SOURCE_FILES} ${HEADERS_FILES} ${OTHER_FILES})

# Enable and configure automatic moc generation.
set_property(TARGET ${RELEASE_NAME} PROPERTY AUTOMOC ON)

# CXX standard configuration.
set_property(TARGET ${RELEASE_NAME} PROPERTY CXX_STANDARD 11)
set_property(TARGET ${RELEASE_NAME} PROPERTY CXX_STANDARD 17)
set_property(TARGET ${RELEASE_NAME} PROPERTY CXX_STANDARD_REQUIRED YES)
set_property(TARGET ${RELEASE_NAME} PROPERTY CXX_EXTENSIONS NO)

# General Qt configuration.
if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
if(CMAKE_BUILD_TYPE STREQUAL Debug)
add_compile_definitions(${RELEASE_NAME} QT_DEBUG)
else()
add_compile_definitions(${RELEASE_NAME} QT_NO_DEBUG)
add_compile_definitions(${RELEASE_NAME} QT_NO_DEBUG_OUTPUT)
endif()

# Link against all required Qt modules.
foreach(QT_COMPONENT ${REQUIRED_QT5_COMPONENTS})
target_link_libraries(${RELEASE_NAME} "Qt5::${QT_COMPONENT}")
foreach(QT_COMPONENT ${REQUIRED_QT_COMPONENTS})
target_link_libraries(${RELEASE_NAME} Qt6::${QT_COMPONENT})
endforeach()

add_custom_command(TARGET ${RELEASE_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${RELEASE_NAME}> ${CMAKE_BINARY_DIR}/plugins/canbus
)
)
2 changes: 1 addition & 1 deletion CRC16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "CRC16.h"

uint16_t calculateCRC16(QByteArray data, CRC16Type crc16Type) {
uint16_t calculateCRC16(const QByteArray &data, CRC16Type crc16Type) {
uint16_t const initial = 0x0000u;
uint16_t const polynomial = 0xA001;
uint16_t const final = 0x0000;
Expand Down
2 changes: 1 addition & 1 deletion CRC16.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ enum struct CRC16Type {
CRC_16_IBM
};

uint16_t calculateCRC16(QByteArray data, CRC16Type crc16Type = CRC16Type::CRC_16_IBM);
uint16_t calculateCRC16(const QByteArray &data, CRC16Type crc16Type = CRC16Type::CRC_16_IBM);