From 0bd3a3b95ba452b0bc048d4b7ea0675a421447fd Mon Sep 17 00:00:00 2001 From: Collin Kidder Date: Sat, 26 Mar 2016 20:17:10 -0400 Subject: [PATCH] Additional implementation of new connection system. The GVRET code can now connect and run its thread. --- canconnection.cpp | 5 +++++ canconnection.h | 9 +++++++-- canconnectioncontainer.cpp | 15 ++++++++++++++- canconnectioncontainer.h | 6 ++++-- canconnectionmodel.cpp | 35 +++++++++++++++++++++++++---------- canconnectionmodel.h | 5 +++++ connectionwindow.cpp | 38 +++++++++++++++++++++++++++++++++++++- connectionwindow.h | 5 ++++- connectionwindow.ui | 2 +- mainwindow.cpp | 1 - mainwindow.h | 2 ++ mainwindow.ui | 3 +-- serialworker.cpp | 18 ++++++++++++++++++ serialworker.h | 3 +++ 14 files changed, 126 insertions(+), 21 deletions(-) diff --git a/canconnection.cpp b/canconnection.cpp index c26c507..fb9ac11 100644 --- a/canconnection.cpp +++ b/canconnection.cpp @@ -126,3 +126,8 @@ void CANConnection::startFrameCapture(int bus) { } + +void CANConnection::updateBusSettings(CAN_Bus *bus) +{ + +} diff --git a/canconnection.h b/canconnection.h index 258378c..57be38b 100644 --- a/canconnection.h +++ b/canconnection.h @@ -33,6 +33,11 @@ public: CANConnection *getConnection(); }; + +//Gentle reminder, CANConnection objects run in their own thread (each one, different thread) +//so, for the love of God, do not try to access them directly. +//Use the signal/slots system to do indirect calls. +//Please and thank you. class CANConnection : public QObject { Q_OBJECT @@ -41,7 +46,7 @@ public: CANConnection(CANFrameModel *, int); virtual int getNumBuses(); int getBusBase(); - QString getConnTypeName(); + virtual QString getConnTypeName(); QString getConnPortName(); signals: @@ -59,7 +64,7 @@ public slots: virtual void updatePortName(QString); //string version of the port to connect to. This base doesnt know a thing about this value virtual void stopFrameCapture(int); //pass bus number virtual void startFrameCapture(int); //pass bus number. Only if stopped. Defaults to started anyway - //virtual void updateBusSettings(int); //bus number that was updated. + virtual void updateBusSettings(CAN_Bus *bus); //reference to the bus that changed. protected: bool quit; diff --git a/canconnectioncontainer.cpp b/canconnectioncontainer.cpp index 15d9baa..63a5069 100644 --- a/canconnectioncontainer.cpp +++ b/canconnectioncontainer.cpp @@ -1,10 +1,18 @@ +#include "canconnection.h" #include "canconnectioncontainer.h" CANConnectionContainer::CANConnectionContainer(CANConnection *conn) { thread = new QThread(); connection = conn; - connection->moveToThread(thread); + connection->moveToThread(thread); + + connect(thread, &QThread::finished, conn, &QObject::deleteLater); + connect(thread, &QThread::started, conn, &CANConnection::run); //setup timers within the proper thread + connect(conn, &CANConnection::frameUpdateRapid, MainWindow::getReference(), &MainWindow::gotFrames, Qt::QueuedConnection); + connect(MainWindow::getReference(), &MainWindow::sendCANFrame, conn, &CANConnection::sendFrame, Qt::QueuedConnection); + thread->start(); + thread->setPriority(QThread::HighPriority); } CANConnectionContainer::~CANConnectionContainer() @@ -13,3 +21,8 @@ CANConnectionContainer::~CANConnectionContainer() delete thread; delete connection; } + +CANConnection* CANConnectionContainer::getRef() +{ + return connection; +} diff --git a/canconnectioncontainer.h b/canconnectioncontainer.h index eb90066..9ff2b22 100644 --- a/canconnectioncontainer.h +++ b/canconnectioncontainer.h @@ -2,17 +2,19 @@ #define CANCONNECTIONCONTAINER_H #include "canconnection.h" +#include #include +#include "mainwindow.h" -class CANConnectionContainer +class CANConnectionContainer : public QObject { + Q_OBJECT public: CANConnectionContainer(CANConnection *conn); ~CANConnectionContainer(); CANConnection* getRef(); - private: CANConnection *connection; QThread *thread; diff --git a/canconnectionmodel.cpp b/canconnectionmodel.cpp index 02063b6..089bba0 100644 --- a/canconnectionmodel.cpp +++ b/canconnectionmodel.cpp @@ -1,3 +1,5 @@ +#include "canconnection.h" +#include "canconnectioncontainer.h" #include "canconnectionmodel.h" CANConnectionModel::CANConnectionModel(QObject *parent) @@ -71,10 +73,12 @@ QVariant CANConnectionModel::data(const QModelIndex &index, int role) const return QString::number(bus.busNum); break; case 1: //type - if (conn) conn->getConnTypeName(); + if (conn) return conn->getConnTypeName(); + else qDebug() << "Tried to show connection type but connection was NULL"; break; case 2: //port - if (conn) conn->getConnPortName(); + if (conn) return conn->getConnPortName(); + else qDebug() << "Tried to show connection port but connection was NULL"; break; case 3: //speed return QString::number(bus.speed); @@ -102,20 +106,31 @@ QVariant CANConnectionModel::data(const QModelIndex &index, int role) const void CANConnectionModel::addConnection(CANConnection *conn) { CAN_Bus bus; - beginResetModel(); CANConnectionContainer *cont = new CANConnectionContainer(conn); connections.append(cont); +} - int numBuses = conn->getNumBuses(); - for (int i = 0; i < numBuses; i++) - { - bus.busNum = conn->getBusBase() + i; - buses.append(bus); - } - +void CANConnectionModel::addBus(CAN_Bus &bus) +{ + beginResetModel(); + buses.append(bus); endResetModel(); } +CAN_Bus* CANConnectionModel::getBus(int bus) +{ + if (bus < 0) return NULL; + if (bus >= buses.count()) return NULL; + return &buses[bus]; +} + +CANConnection* CANConnectionModel::getConnection(int conn) +{ + if (conn < 0) return NULL; + if (conn >= connections.count()) return NULL; + return connections[conn]->getRef(); +} + CAN_Bus* CANConnectionModel::findBusByNum(int bus) { for (int i = 0; i < buses.count(); i++) diff --git a/canconnectionmodel.h b/canconnectionmodel.h index 8042719..f6a183b 100644 --- a/canconnectionmodel.h +++ b/canconnectionmodel.h @@ -6,6 +6,8 @@ #include +class CANConnectionContainer; + class CANConnectionModel : public QAbstractTableModel { Q_OBJECT @@ -22,6 +24,9 @@ public: QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; void addConnection(CANConnection *conn); + void addBus(CAN_Bus &bus); + CAN_Bus* getBus(int bus); + CANConnection* getConnection(int conn); private: QList connections; diff --git a/connectionwindow.cpp b/connectionwindow.cpp index 7b5ffe9..be52a32 100644 --- a/connectionwindow.cpp +++ b/connectionwindow.cpp @@ -88,8 +88,44 @@ void ConnectionWindow::handleOKButton() { if (ui->rbGVRET->isChecked()) { - SerialWorker *serial = new SerialWorker(canModel, 0); + SerialWorker *serial = new SerialWorker(canModel, connModel->rowCount()); connModel->addConnection(serial); + + qDebug() << "Setup initial connection object"; + + CAN_Bus bus; + bus.active = true; + bus.busNum = serial->getBusBase(); + bus.connection = serial; + bus.listenOnly = ui->ckListenOnly->isChecked(); + bus.singleWire = ui->ckSingleWire->isChecked(); + + if (ui->cbSpeed->currentIndex() < 1) bus.speed = 0; //default speed + else if (ui->cbSpeed->currentIndex() == 1) + { + bus.speed = 0; + bus.active = false; + } + else bus.speed = ui->cbSpeed->currentText().toInt(); + connModel->addBus(bus); + + int numBuses = serial->getNumBuses(); + for (int i = 1; i < numBuses; i++) + { + bus.active = false; + bus.listenOnly = false; + bus.singleWire = false; + bus.speed = 250000; + bus.busNum = serial->getBusBase() + i; + bus.connection = serial; + connModel->addBus(bus); + qDebug() << "Added bus " << bus.busNum; + } + + //call through signal/slot interface without using connect + QMetaObject::invokeMethod(serial, "updatePortName", + Qt::QueuedConnection, + Q_ARG(QString, ui->cbPort->currentText())); } else if (ui->rbKvaser->isChecked()) { diff --git a/connectionwindow.h b/connectionwindow.h index 66b92da..afa75e3 100644 --- a/connectionwindow.h +++ b/connectionwindow.h @@ -10,6 +10,8 @@ #include "canconnection.h" #include "serialworker.h" +class CANConnectionModel; + namespace Ui { class ConnectionWindow; } @@ -38,7 +40,8 @@ public: bool getSWMode(); signals: - void updateConnectionSettings(QString connectionType, QString port, int speed); + void updateBusSettings(CAN_Bus *bus); + void updatePortName(QString port); public slots: void setSpeed(int speed0); diff --git a/connectionwindow.ui b/connectionwindow.ui index b62f1b3..6dbd5fe 100644 --- a/connectionwindow.ui +++ b/connectionwindow.ui @@ -6,7 +6,7 @@ 0 0 - 952 + 1009 433 diff --git a/mainwindow.cpp b/mainwindow.cpp index be2d26f..5ab24af 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -109,7 +109,6 @@ MainWindow::MainWindow(QWidget *parent) : model->setDBCHandler(dbcHandler); connect(ui->actionSetup, SIGNAL(triggered(bool)), SLOT(showConnectionSettingsWindow())); - connect(ui->actionConnect, SIGNAL(triggered(bool)), this, SLOT(connButtonPress())); connect(ui->actionOpen_Log_File, &QAction::triggered, this, &MainWindow::handleLoadFile); connect(ui->actionGraph_Dta, &QAction::triggered, this, &MainWindow::showGraphingWindow); connect(ui->actionFrame_Data_Analysis, &QAction::triggered, this, &MainWindow::showFrameDataAnalysis); diff --git a/mainwindow.h b/mainwindow.h index 278c688..03bb1cd 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -27,6 +27,8 @@ #include "udsscanwindow.h" #include "isotp_interpreterwindow.h" +class ConnectionWindow; + namespace Ui { class MainWindow; } diff --git a/mainwindow.ui b/mainwindow.ui index fafd407..327ac92 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -245,7 +245,6 @@ Connection - @@ -350,7 +349,7 @@ - Set Port + Open Connection WIndow diff --git a/serialworker.cpp b/serialworker.cpp index 95b9322..811dea7 100644 --- a/serialworker.cpp +++ b/serialworker.cpp @@ -7,6 +7,7 @@ SerialWorker::SerialWorker(CANFrameModel *model, int base) : CANConnection(model, base) { + qDebug() << "Serial Worker constructor"; serial = NULL; rx_state = IDLE; rx_step = 0; @@ -39,6 +40,7 @@ SerialWorker::~SerialWorker() void SerialWorker::run() { + qDebug() << "Serial worker thread starting"; ticker = new QTimer; connect(ticker, SIGNAL(timeout()), this, SLOT(handleTick())); @@ -545,4 +547,20 @@ void SerialWorker::updatePortName(QString portName) } } +int SerialWorker::getNumBuses() +{ + return 2; +} +QString SerialWorker::getConnTypeName() +{ + return QString("GVRET"); +} + +void SerialWorker::updateBusSettings(CAN_Bus *bus) +{ + int busNum = bus->busNum - busBase; + if (busNum < 0) return; + if (busNum >= numBuses) return; + qDebug() << "About to update bus " << busNum << " on GVRET"; +} diff --git a/serialworker.h b/serialworker.h index df54810..a83a637 100644 --- a/serialworker.h +++ b/serialworker.h @@ -38,6 +38,8 @@ public: SerialWorker(CANFrameModel *, int); ~SerialWorker(); void readSettings(); + int getNumBuses(); + QString getConnTypeName() override; private slots: //we receive things in slots void readSerialData(); @@ -55,6 +57,7 @@ public slots: //void stopFrameCapture(int) override; //void startFrameCapture(int) override; void updatePortName(QString) override; //string version of the port to connect to. This base doesnt know a thing about this value + void updateBusSettings(CAN_Bus *bus) override; private: bool doValidation;