From e7ded7737a00ac3a5b5668d1d3a321d77b8936eb Mon Sep 17 00:00:00 2001 From: Collin Kidder Date: Sat, 19 Mar 2016 21:40:21 -0400 Subject: [PATCH] Preliminary pass at converting connection system to use a more generic approach and support more devices and multiple devices at the same time. --- SavvyCAN.pro | 10 +- canconnection.cpp | 128 +++++++++++++++++++ canconnection.h | 74 +++++++++++ canconnectioncontainer.cpp | 15 +++ canconnectioncontainer.h | 21 ++++ canconnectionmodel.cpp | 130 +++++++++++++++++++ canconnectionmodel.h | 33 +++++ connectionwindow.cpp | 166 +++++++++++++----------- connectionwindow.h | 26 ++-- connectionwindow.ui | 252 ++++++++++++++++++++++++------------- firmwareuploaderwindow.cpp | 6 +- mainwindow.cpp | 67 ++-------- mainwindow.h | 6 - serialworker.cpp | 59 +++++---- serialworker.h | 33 ++--- 15 files changed, 739 insertions(+), 287 deletions(-) create mode 100644 canconnection.cpp create mode 100644 canconnection.h create mode 100644 canconnectioncontainer.cpp create mode 100644 canconnectioncontainer.h create mode 100644 canconnectionmodel.cpp create mode 100644 canconnectionmodel.h diff --git a/SavvyCAN.pro b/SavvyCAN.pro index acc556d..fba0533 100644 --- a/SavvyCAN.pro +++ b/SavvyCAN.pro @@ -48,7 +48,10 @@ SOURCES += main.cpp\ udsscanwindow.cpp \ can_structs.cpp \ isotp_interpreterwindow.cpp \ - isotp_decoder.cpp + isotp_decoder.cpp \ + canconnectionmodel.cpp \ + canconnection.cpp \ + canconnectioncontainer.cpp HEADERS += mainwindow.h \ can_structs.h \ @@ -83,7 +86,10 @@ HEADERS += mainwindow.h \ fuzzingwindow.h \ udsscanwindow.h \ isotp_interpreterwindow.h \ - isotp_decoder.h + isotp_decoder.h \ + canconnectionmodel.h \ + canconnection.h \ + canconnectioncontainer.h FORMS += mainwindow.ui \ graphingwindow.ui \ diff --git a/canconnection.cpp b/canconnection.cpp new file mode 100644 index 0000000..c26c507 --- /dev/null +++ b/canconnection.cpp @@ -0,0 +1,128 @@ +#include "canconnection.h" + +CAN_Bus::CAN_Bus() +{ + speed = 250000; + listenOnly = false; + singleWire = false; + active = false; + connection = NULL; + busNum = 0; +} + +void CAN_Bus::setSpeed(int newSpeed) +{ + speed = newSpeed; +} + +void CAN_Bus::setListenOnly(bool mode) +{ + listenOnly = mode; +} + +void CAN_Bus::setSingleWire(bool mode) +{ + singleWire = mode; +} + +void CAN_Bus::setEnabled(bool mode) +{ + active = mode; +} + +void CAN_Bus::setConnection(CANConnection *conn) +{ + connection = conn; +} + +void CAN_Bus::setBusNum(int num) +{ + busNum = num; +} + +int CAN_Bus::getSpeed() +{ + return speed; +} + +int CAN_Bus::getBusNum() +{ + return busNum; +} + +bool CAN_Bus::isListenOnly() +{ + return listenOnly; +} + +bool CAN_Bus::isSingleWire() +{ + return singleWire; +} + +bool CAN_Bus::isActive() +{ + return active; +} + +CANConnection* CAN_Bus::getConnection() +{ + return connection; +} + +CANConnection::CANConnection(CANFrameModel *pModel, int base) +{ + model = pModel; + numBuses = getNumBuses(); + busBase = base; +} + +int CANConnection::getNumBuses() +{ + return 1; +} + +int CANConnection::getBusBase() +{ + return busBase; +} + +QString CANConnection::getConnTypeName() +{ + return QString("Generic"); +} + +QString CANConnection::getConnPortName() +{ + return portName; +} + +void CANConnection::run() +{ + +} + +void CANConnection::sendFrame(const CANFrame * frame) +{ + +} + +void CANConnection::sendFrameBatch(const QList *frames) +{ + +} + +void CANConnection::updatePortName(QString portName) +{ + this->portName = portName; +} + +void CANConnection::stopFrameCapture(int bus) +{ + +} + +void CANConnection::startFrameCapture(int bus) +{ + +} diff --git a/canconnection.h b/canconnection.h new file mode 100644 index 0000000..258378c --- /dev/null +++ b/canconnection.h @@ -0,0 +1,74 @@ +#ifndef CANCONNECTION_H +#define CANCONNECTION_H + +#include +#include +#include "can_structs.h" +#include "canframemodel.h" + +class CANConnection; + +class CAN_Bus +{ +public: + CAN_Bus(); + int busNum; + int speed; + bool listenOnly; + bool singleWire; + bool active; //is this bus turned on? + CANConnection *connection; + + void setSpeed(int); // new speed + void setListenOnly(bool); //bool for whether to only listen + void setSingleWire(bool); //bool for whether to use single wire mode + void setEnabled(bool); //whether this bus should be enabled or not. + void setConnection(CANConnection *); + void setBusNum(int); + int getSpeed(); + int getBusNum(); + bool isListenOnly(); + bool isSingleWire(); + bool isActive(); + CANConnection *getConnection(); +}; + +class CANConnection : public QObject +{ + Q_OBJECT + +public: + CANConnection(CANFrameModel *, int); + virtual int getNumBuses(); + int getBusBase(); + QString getConnTypeName(); + QString getConnPortName(); + +signals: + void error(const QString &); + void frameUpdateRapid(int); + void frameUpdate(int); + void connectionSuccess(); + void connectionFailure(); + void deviceInfo(int, int); //First param = driver version (or version of whatever you want), second param a status byte (listen only, single wire, etc) + +public slots: + virtual void run(); + virtual void sendFrame(const CANFrame *); + virtual void sendFrameBatch(const QList *); + 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. + +protected: + bool quit; + int numBuses; + int busBase; //first bus this class is supposed to handle + QString portName; //for easy access later on + QString connType; //what kind of connection this is (socketcan, kvaser, etc) + bool isConnected; //is the whole device connected? (Is the code connected to the device itself) + CANFrameModel *model; +}; + +#endif // CANCONNECTION_H diff --git a/canconnectioncontainer.cpp b/canconnectioncontainer.cpp new file mode 100644 index 0000000..15d9baa --- /dev/null +++ b/canconnectioncontainer.cpp @@ -0,0 +1,15 @@ +#include "canconnectioncontainer.h" + +CANConnectionContainer::CANConnectionContainer(CANConnection *conn) +{ + thread = new QThread(); + connection = conn; + connection->moveToThread(thread); +} + +CANConnectionContainer::~CANConnectionContainer() +{ + //have to stop the actual execution first before deleting + delete thread; + delete connection; +} diff --git a/canconnectioncontainer.h b/canconnectioncontainer.h new file mode 100644 index 0000000..eb90066 --- /dev/null +++ b/canconnectioncontainer.h @@ -0,0 +1,21 @@ +#ifndef CANCONNECTIONCONTAINER_H +#define CANCONNECTIONCONTAINER_H + +#include "canconnection.h" +#include + +class CANConnectionContainer +{ +public: + CANConnectionContainer(CANConnection *conn); + ~CANConnectionContainer(); + + CANConnection* getRef(); + + +private: + CANConnection *connection; + QThread *thread; +}; + +#endif // CANCONNECTIONCONTAINER_H diff --git a/canconnectionmodel.cpp b/canconnectionmodel.cpp new file mode 100644 index 0000000..02063b6 --- /dev/null +++ b/canconnectionmodel.cpp @@ -0,0 +1,130 @@ +#include "canconnectionmodel.h" + +CANConnectionModel::CANConnectionModel(QObject *parent) + : QAbstractTableModel(parent) +{ +} + +QVariant CANConnectionModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (role != Qt::DisplayRole) + return QVariant(); + + if (orientation == Qt::Horizontal) + { + switch (section) + { + case 0: + return QString(tr("Bus")); + break; + case 1: + return QString(tr("Type")); + break; + case 2: + return QString(tr("Port")); + break; + case 3: + return QString(tr("Speed")); + break; + case 4: + return QString(tr("Listen Only")); + break; + case 5: + return QString(tr("Single Wire")); + break; + case 6: + return QString(tr("Active")); + break; + } + } + + else + return QString::number(section + 1); + + return QVariant(); +} + +int CANConnectionModel::rowCount(const QModelIndex &parent) const +{ + return buses.count(); +} + +int CANConnectionModel::columnCount(const QModelIndex &parent) const +{ + return 7; +} + +QVariant CANConnectionModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + if (index.row() >= (buses.count())) + return QVariant(); + + if (role == Qt::DisplayRole) { + CAN_Bus bus = buses[index.row()]; + CANConnection *conn = bus.connection; + switch (index.column()) + { + case 0: //bus + return QString::number(bus.busNum); + break; + case 1: //type + if (conn) conn->getConnTypeName(); + break; + case 2: //port + if (conn) conn->getConnPortName(); + break; + case 3: //speed + return QString::number(bus.speed); + break; + case 4: //Listen Only + if (bus.listenOnly) return QString("True"); + else return QString("False"); + break; + case 5: //Single Wire + if (bus.singleWire) return QString("True"); + else return QString("False"); + break; + case 6: //Active + if (bus.active) return QString("True"); + else return QString("False"); + break; + default: + return QVariant(); + } + } + else + return QVariant(); +} + +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); + } + + endResetModel(); +} + +CAN_Bus* CANConnectionModel::findBusByNum(int bus) +{ + for (int i = 0; i < buses.count(); i++) + { + if (buses[i].busNum == bus) + { + return &buses[i]; + } + } + return NULL; +} + diff --git a/canconnectionmodel.h b/canconnectionmodel.h new file mode 100644 index 0000000..8042719 --- /dev/null +++ b/canconnectionmodel.h @@ -0,0 +1,33 @@ +#ifndef CANCONNECTIONMODEL_H +#define CANCONNECTIONMODEL_H + +#include "canconnection.h" +#include "canconnectioncontainer.h" + +#include + +class CANConnectionModel : public QAbstractTableModel +{ + Q_OBJECT + +public: + explicit CANConnectionModel(QObject *parent = 0); + + // Header: + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; + + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + int columnCount(const QModelIndex &parent = QModelIndex()) const override; + + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + + void addConnection(CANConnection *conn); + +private: + QList connections; + QList buses; + + CAN_Bus *findBusByNum(int bus); +}; + +#endif // CANCONNECTIONMODEL_H diff --git a/connectionwindow.cpp b/connectionwindow.cpp index 8a8ea45..7b5ffe9 100644 --- a/connectionwindow.cpp +++ b/connectionwindow.cpp @@ -2,7 +2,7 @@ #include "ui_connectionwindow.h" #include -ConnectionWindow::ConnectionWindow(QWidget *parent) : +ConnectionWindow::ConnectionWindow(CANFrameModel *cModel, QWidget *parent) : QDialog(parent), ui(new Ui::ConnectionWindow) { @@ -10,34 +10,34 @@ ConnectionWindow::ConnectionWindow(QWidget *parent) : settings = new QSettings(); + connModel = new CANConnectionModel(); + ui->tableConnections->setModel(connModel); + + canModel = cModel; + + ui->tableConnections->setColumnWidth(0, 50); + ui->tableConnections->setColumnWidth(1, 110); + ui->tableConnections->setColumnWidth(2, 110); + ui->tableConnections->setColumnWidth(3, 150); + ui->tableConnections->setColumnWidth(4, 75); + ui->tableConnections->setColumnWidth(5, 75); + ui->tableConnections->setColumnWidth(6, 75); + int temp = settings->value("Main/DefaultConnectionType", 0).toInt(); - if (temp == 0) currentConnType = ConnectionType::GVRET_SERIAL; - if (temp == 1) currentConnType = ConnectionType::KVASER; - if (temp == 2) currentConnType = ConnectionType::SOCKETCAN; + //currentPortName = settings->value("Main/DefaultConnectionPort", "").toString(); - currentPortName = settings->value("Main/DefaultConnectionPort", "").toString(); - - currentSpeed1 = -1; - currentSpeed2 = -1; + //currentSpeed1 = -1; ui->ckSingleWire->setChecked(settings->value("Main/SingleWireMode", false).toBool()); - ui->cbSpeed0->addItem(tr("")); - ui->cbSpeed0->addItem(tr("Disabled")); - ui->cbSpeed0->addItem(tr("125000")); - ui->cbSpeed0->addItem(tr("250000")); - ui->cbSpeed0->addItem(tr("500000")); - ui->cbSpeed0->addItem(tr("1000000")); - ui->cbSpeed0->addItem(tr("33333")); - - ui->cbSpeed1->addItem(tr("")); - ui->cbSpeed1->addItem(tr("Disabled")); - ui->cbSpeed1->addItem(tr("125000")); - ui->cbSpeed1->addItem(tr("250000")); - ui->cbSpeed1->addItem(tr("500000")); - ui->cbSpeed1->addItem(tr("1000000")); - ui->cbSpeed1->addItem(tr("33333")); + ui->cbSpeed->addItem(tr("")); + ui->cbSpeed->addItem(tr("Disabled")); + ui->cbSpeed->addItem(tr("125000")); + ui->cbSpeed->addItem(tr("250000")); + ui->cbSpeed->addItem(tr("500000")); + ui->cbSpeed->addItem(tr("1000000")); + ui->cbSpeed->addItem(tr("33333")); #ifdef Q_OS_LINUX ui->rbSocketCAN->setEnabled(true); @@ -47,10 +47,12 @@ ConnectionWindow::ConnectionWindow(QWidget *parent) : ui->rbKvaser->setEnabled(true); #endif - connect(ui->btnOK, SIGNAL(clicked(bool)), this, SLOT(handleOKButton())); - connect(ui->rbGVRET, SIGNAL(toggled(bool)), this, SLOT(handleConnTypeChanged())); - connect(ui->rbKvaser, SIGNAL(toggled(bool)), this, SLOT(handleConnTypeChanged())); - connect(ui->rbSocketCAN, SIGNAL(toggled(bool)), this, SLOT(handleConnTypeChanged())); + connect(ui->btnOK, &QAbstractButton::clicked, this, &ConnectionWindow::handleOKButton); + connect(ui->rbGVRET, &QAbstractButton::toggled, this, &ConnectionWindow::handleConnTypeChanged); + connect(ui->rbKvaser, &QAbstractButton::toggled, this, &ConnectionWindow::handleConnTypeChanged); + connect(ui->rbSocketCAN, &QAbstractButton::toggled, this, &ConnectionWindow::handleConnTypeChanged); + connect(ui->btnRevert, &QPushButton::clicked, this, &ConnectionWindow::handleRevert); + connect(ui->tableConnections->selectionModel(), &QItemSelectionModel::selectionChanged, this, &ConnectionWindow::handleConnSelectionChanged); } ConnectionWindow::~ConnectionWindow() @@ -78,6 +80,28 @@ void ConnectionWindow::handleOKButton() QString conn; int connType = 0; + if (ui->tableConnections->selectionModel()->currentIndex().row() >= 0) + { + + } + else //new connection + { + if (ui->rbGVRET->isChecked()) + { + SerialWorker *serial = new SerialWorker(canModel, 0); + connModel->addConnection(serial); + } + else if (ui->rbKvaser->isChecked()) + { + + } + else if (ui->rbSocketCAN->isChecked()) + { + + } + } + +/* if (ui->rbGVRET->isChecked()) { conn = "GVRET"; @@ -100,16 +124,29 @@ void ConnectionWindow::handleOKButton() } currentPortName = getPortName(); - currentSpeed1 = getSpeed0(); - currentSpeed2 = getSpeed1(); + currentSpeed1 = getSpeed(); settings->setValue("Main/DefaultConnectionPort", currentPortName); settings->setValue("Main/DefaultConnectionType", connType); settings->setValue("Main/SingleWireMode", ui->ckSingleWire->isChecked()); - emit updateConnectionSettings(conn, getPortName(), getSpeed0(), getSpeed1()); + emit updateConnectionSettings(conn, getPortName(), getSpeed()); this->close(); +*/ +} + +void ConnectionWindow::handleConnSelectionChanged() +{ + int selIdx = ui->tableConnections->selectionModel()->currentIndex().row(); + if (selIdx == 0) + { + ui->btnOK->setText(tr("Create New Connection")); + } + else + { + ui->btnOK->setText(tr("Update Connection Settings")); + } } void ConnectionWindow::getSerialPorts() @@ -120,7 +157,7 @@ void ConnectionWindow::getSerialPorts() for (int i = 0; i < ports.count(); i++) { ui->cbPort->addItem(ports[i].portName()); - if (currentPortName == ports[i].portName()) ui->cbPort->setCurrentIndex(i); + //if (currentPortName == ports[i].portName()) ui->cbPort->setCurrentIndex(i); } } @@ -147,47 +184,33 @@ void ConnectionWindow::getSocketcanPorts() } } -void ConnectionWindow::setSpeeds(int speed0, int speed1) +void ConnectionWindow::setSpeed(int speed0) { bool found = false; - qDebug() << speed0 << "X" << speed1; + qDebug() << speed0; - for (int i = 0; i < ui->cbSpeed0->count(); i++) + for (int i = 0; i < ui->cbSpeed->count(); i++) { - if (ui->cbSpeed0->itemText(i).toInt() == speed0) + if (ui->cbSpeed->itemText(i).toInt() == speed0) { - ui->cbSpeed0->setCurrentIndex(i); + ui->cbSpeed->setCurrentIndex(i); found = true; } } if (!found) { - ui->cbSpeed0->addItem(QString::number(speed0)); - ui->cbSpeed0->setCurrentIndex(ui->cbSpeed0->count() - 1); + ui->cbSpeed->addItem(QString::number(speed0)); + ui->cbSpeed->setCurrentIndex(ui->cbSpeed->count() - 1); } - found = false; - for (int i = 0; i < ui->cbSpeed1->count(); i++) - { - if (ui->cbSpeed1->itemText(i).toInt() == speed1) - { - ui->cbSpeed1->setCurrentIndex(i); - found = true; - } - } - if (!found) - { - ui->cbSpeed1->addItem(QString::number(speed1)); - ui->cbSpeed1->setCurrentIndex(ui->cbSpeed1->count() - 1); - } } //-1 means leave it at whatever it booted up to. 0 means disable. Otherwise the actual rate we want. -int ConnectionWindow::getSpeed0() +int ConnectionWindow::getSpeed() { - switch (ui->cbSpeed0->currentIndex()) + switch (ui->cbSpeed->currentIndex()) { case -1: return -1; @@ -199,26 +222,7 @@ int ConnectionWindow::getSpeed0() return 0; break; default: - return (ui->cbSpeed0->currentText().toInt()); - break; - } -} - -int ConnectionWindow::getSpeed1() -{ - switch (ui->cbSpeed1->currentIndex()) - { - case -1: - return -1; - break; - case 0: - return -1; - break; - case 1: - return 0; - break; - default: - return (ui->cbSpeed1->currentText().toInt()); + return (ui->cbSpeed->currentText().toInt()); break; } } @@ -235,13 +239,23 @@ ConnectionType::ConnectionType ConnectionWindow::getConnectionType() if (ui->rbSocketCAN->isChecked()) return ConnectionType::SOCKETCAN; } -void ConnectionWindow::setCAN1SWMode(bool mode) +void ConnectionWindow::setSWMode(bool mode) { ui->ckSingleWire->setChecked(mode); } -bool ConnectionWindow::getCAN1SWMode() +bool ConnectionWindow::getSWMode() { if (ui->ckSingleWire->checkState() == Qt::Checked) return true; return false; } + +void ConnectionWindow::handleRemoveConn() +{ + +} + +void ConnectionWindow::handleRevert() +{ + +} diff --git a/connectionwindow.h b/connectionwindow.h index e098f67..66b92da 100644 --- a/connectionwindow.h +++ b/connectionwindow.h @@ -1,10 +1,14 @@ #ifndef CONNECTIONWINDOW_H #define CONNECTIONWINDOW_H +#include "canconnectionmodel.h" + #include #include #include #include +#include "canconnection.h" +#include "serialworker.h" namespace Ui { class ConnectionWindow; @@ -25,34 +29,36 @@ class ConnectionWindow : public QDialog Q_OBJECT public: - explicit ConnectionWindow(QWidget *parent = 0); + explicit ConnectionWindow(CANFrameModel *canModel, QWidget *parent = 0); ~ConnectionWindow(); void showEvent(QShowEvent *); - int getSpeed0(); - int getSpeed1(); + int getSpeed(); QString getPortName(); //name of port to connect to ConnectionType::ConnectionType getConnectionType(); - bool getCAN1SWMode(); + bool getSWMode(); signals: - void updateConnectionSettings(QString connectionType, QString port, int speed0, int speed1); + void updateConnectionSettings(QString connectionType, QString port, int speed); public slots: - void setSpeeds(int speed0, int speed1); - void setCAN1SWMode(bool mode); + void setSpeed(int speed0); + void setSWMode(bool mode); private slots: void handleOKButton(); void handleConnTypeChanged(); + void handleConnSelectionChanged(); + void handleRemoveConn(); + void handleRevert(); + private: Ui::ConnectionWindow *ui; QList ports; QSettings *settings; + CANConnectionModel *connModel; + CANFrameModel *canModel; - ConnectionType::ConnectionType currentConnType; - QString currentPortName; - int currentSpeed1, currentSpeed2; void getSerialPorts(); void getKvaserPorts(); diff --git a/connectionwindow.ui b/connectionwindow.ui index dc48369..b62f1b3 100644 --- a/connectionwindow.ui +++ b/connectionwindow.ui @@ -6,109 +6,191 @@ 0 0 - 335 - 235 + 952 + 433 Connection Settings - + - - - Connection Type - - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter - - - - - - GVRET Serial - - - true - - - - - - - false - - - Kvaser - - - - - - - false - - - SocketCAN - - - - - - - - - - - 0 - 0 - - - - Port: - - - - - - - - - - + + + - CAN0 Speed + Connections: - - - - CAN1 Speed + + + + QAbstractItemView::SingleSelection + + + QAbstractItemView::SelectRows - - + + + + + + Activate Selected Connection + + + + + + + Remove Selected Connection + + + + - - + + + + + + Activate All Connections + + + + + + + Deactivate All Connections + + + + - - - Set CAN1 to Single Wire Mode - - - - - - - Update Settings - - + + + + + Connection Type + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + + + + + + GVRET Serial + + + true + + + + + + + false + + + Kvaser + + + + + + + false + + + SocketCAN + + + + + + + + + + + 0 + 0 + + + + Port: + + + + + + + + + + Single Wire Mode + + + + + + + Listen Only Mode + + + + + + + + + CAN Speed + + + + + + + + + + + + + + Create New Connection + + + + + + + Revert + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + diff --git a/firmwareuploaderwindow.cpp b/firmwareuploaderwindow.cpp index b9be616..f881ff5 100644 --- a/firmwareuploaderwindow.cpp +++ b/firmwareuploaderwindow.cpp @@ -80,7 +80,7 @@ void FirmwareUploaderWindow::gotTargettedFrame(int frameLoc) if ((frame.data[6] == ((token >> 16) & 0xFF)) && (frame.data[7] == ((token >> 24) & 0xFF))) { qDebug() << "starting firmware process"; - MainWindow::getReference()->setTargettedID(baseAddress + 0x20); + //MainWindow::getReference()->setTargettedID(baseAddress + 0x20); transferInProgress = true; sendFirmwareChunk(); } @@ -163,7 +163,7 @@ void FirmwareUploaderWindow::handleStartStopTransfer() bus = ui->spinBus->value(); baseAddress = Utility::ParseStringToNum(ui->txtBaseAddr->text()); qDebug() << "Base address: " + QString::number(baseAddress); - MainWindow::getReference()->setTargettedID(baseAddress + 0x10); + //MainWindow::getReference()->setTargettedID(baseAddress + 0x10); CANFrame *output = new CANFrame; output->extended = false; output->len = 8; @@ -181,7 +181,7 @@ void FirmwareUploaderWindow::handleStartStopTransfer() else //stop anything in process { ui->btnStartStop->setText("Start Upload"); - MainWindow::getReference()->setTargettedID(-1); + //MainWindow::getReference()->setTargettedID(-1); } } diff --git a/mainwindow.cpp b/mainwindow.cpp index 5426539..be2d26f 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -64,6 +64,7 @@ MainWindow::MainWindow(QWidget *parent) : //enabling the below line kills performance in every way imaginable. Left here as a warning. Do not do this. //ui->canFramesView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); + /* worker = new SerialWorker(model); worker->moveToThread(&serialWorkerThread); connect(&serialWorkerThread, &QThread::finished, worker, &QObject::deleteLater); @@ -81,6 +82,7 @@ MainWindow::MainWindow(QWidget *parent) : connect(this, &MainWindow::settingsUpdated, worker, &SerialWorker::readSettings); serialWorkerThread.start(); serialWorkerThread.setPriority(QThread::HighPriority); + */ graphingWindow = NULL; frameInfoWindow = NULL; @@ -176,7 +178,7 @@ MainWindow::MainWindow(QWidget *parent) : model->clearFrames(); //Automatically create the connection window so it can be updated even if we never opened it. - connectionWindow = new ConnectionWindow(); + connectionWindow = new ConnectionWindow(model); connect(connectionWindow, SIGNAL(updateConnectionSettings(QString,QString,int,int)), this, SLOT(updateConnectionSettings(QString,QString,int,int))); } @@ -384,45 +386,7 @@ void MainWindow::updateConnectionSettings(QString connectionType, QString port, canSpeed1 = speed1; if (isConnected) { - emit updateBaudRates(speed0, speed1); - } -} - -void MainWindow::connButtonPress() -{ - if (!isConnected) - { - if (connType == "GVRET") - { - QList ports; - ports = QSerialPortInfo::availablePorts(); - - for (int i = 0; i < ports.count(); i++) - { - if (ports[i].portName() == portName) - { - portInfo = ports[i]; - //need to create some way to send single wire mode to serial port code - emit sendSerialPort(&portInfo); - lbStatusConnected.setText(tr("Attempting to connect to port ") + portName); - } - } - } - else if (connType == "KVASER") - { - - } - else if (connType == "SOCKETCAN") - { - - } - } - else - { - emit closeSerialPort(); - isConnected = false; - lbStatusConnected.setText(tr("Not Connected")); - ui->actionConnect->setText(tr("Connect")); + //emit updateBaudRates(speed0, speed1); } } @@ -576,16 +540,6 @@ void MainWindow::normalizeTiming() emit framesUpdated(-2); //claim an all new set of frames because every frame was updated. } -void MainWindow::changeBaudRates() -{ - int Speed1 = 0, Speed2 = 0; - - //Speed1 = ui->cbSpeed1->currentText().toInt(); - //Speed2 = ui->cbSpeed2->currentText().toInt(); - - emit updateBaudRates(Speed1, Speed2); -} - void MainWindow::handleLoadFile() { QString filename; @@ -785,7 +739,7 @@ void MainWindow::connectionSucceeded(int baud0, int baud1) ui->cbSpeed2->setCurrentIndex(ui->cbSpeed2->count() - 1); } */ - if (connectionWindow) connectionWindow->setSpeeds(baud0, baud1); + if (connectionWindow) connectionWindow->setSpeed(baud0); //ui->btnConnect->setEnabled(false); ui->actionConnect->setText(tr("Disconnect")); isConnected = true; @@ -843,16 +797,11 @@ void MainWindow::gotDeviceInfo(int build, int swCAN) if (connectionWindow) { - if (swCAN == 1) connectionWindow->setCAN1SWMode(true); - else connectionWindow->setCAN1SWMode(false); + if (swCAN == 1) connectionWindow->setSWMode(true); + else connectionWindow->setSWMode(false); } } -void MainWindow::setTargettedID(int id) -{ - worker->targetFrameID(id); -} - void MainWindow::showSettingsDialog() { if (!settingsDialog) @@ -1039,7 +988,7 @@ void MainWindow::showConnectionSettingsWindow() { if (!connectionWindow) { - connectionWindow = new ConnectionWindow(); + connectionWindow = new ConnectionWindow(model); connect(connectionWindow, SIGNAL(updateConnectionSettings(QString,QString,int,int)), this, SLOT(updateConnectionSettings(QString,QString,int,int))); } connectionWindow->show(); diff --git a/mainwindow.h b/mainwindow.h index fc8115f..278c688 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -39,7 +39,6 @@ public: explicit MainWindow(QWidget *parent = 0); static QString loadedFileName; static MainWindow *getReference(); - void setTargettedID(int); ~MainWindow(); private slots: @@ -48,7 +47,6 @@ private slots: void handleSaveFilteredFile(); void handleSaveFilters(); void handleLoadFilters(); - void connButtonPress(); void showGraphingWindow(); void showFrameDataAnalysis(); void clearFrames(); @@ -69,7 +67,6 @@ private slots: void showISOInterpreterWindow(); void exitApp(); void handleSaveDecoded(); - void changeBaudRates(); void connectionFailed(); void gotDeviceInfo(int, int); void connectionSucceeded(int, int); @@ -92,9 +89,6 @@ public slots: void updateConnectionSettings(QString connectionType, QString port, int speed0, int speed1); signals: - void sendSerialPort(QSerialPortInfo *port); - void closeSerialPort(); - void updateBaudRates(int, int); void sendCANFrame(const CANFrame *, int); void stopFrameCapturing(); void startFrameCapturing(); diff --git a/serialworker.cpp b/serialworker.cpp index 61a0b5f..95b9322 100644 --- a/serialworker.cpp +++ b/serialworker.cpp @@ -5,19 +5,16 @@ #include #include -SerialWorker::SerialWorker(CANFrameModel *model, QObject *parent) : QObject(parent) +SerialWorker::SerialWorker(CANFrameModel *model, int base) : CANConnection(model, base) { serial = NULL; rx_state = IDLE; rx_step = 0; buildFrame = new CANFrame; - canModel = model; ticker = NULL; framesRapid = 0; - capturing = true; gotValidated = true; isAutoRestart = false; - targetID = -1; txTimestampBasis = QDateTime::currentMSecsSinceEpoch(); @@ -121,8 +118,8 @@ void SerialWorker::setSerialPort(QSerialPortInfo *port) continuousTimeSync = true; serial->write(output); - if (doValidation) connected = false; - else connected = true; + if (doValidation) isConnected = false; + else isConnected = true; connect(serial, SIGNAL(readyRead()), this, SLOT(readSerialData())); if (doValidation) QTimer::singleShot(1000, this, SLOT(connectionTimeout())); } @@ -130,7 +127,7 @@ void SerialWorker::setSerialPort(QSerialPortInfo *port) void SerialWorker::connectionTimeout() { //one second after trying to connect are we actually connected? - if (!connected) //no? + if (!isConnected) //no? { //then emit the the failure signal and see if anyone cares qDebug() << "Failed to connect to GVRET at that com port"; @@ -157,7 +154,7 @@ void SerialWorker::readSerialData() } } -void SerialWorker::sendFrame(const CANFrame *frame, int bus = 0) +void SerialWorker::sendFrame(const CANFrame *frame) { QByteArray buffer; int c; @@ -169,12 +166,12 @@ void SerialWorker::sendFrame(const CANFrame *frame, int bus = 0) //qDebug() << "Sending out frame with id " << frame->ID; //show our sent frames in the list too. This happens even if we're not connected. - canModel->addFrame(tempFrame, false); + model->addFrame(tempFrame, false); framesRapid++; if (serial == NULL) return; if (!serial->isOpen()) return; - if (!connected) return; + if (!isConnected) return; ID = frame->ID; if (frame->extended) ID |= 1 << 31; @@ -185,7 +182,7 @@ void SerialWorker::sendFrame(const CANFrame *frame, int bus = 0) buffer[3] = (unsigned char)(ID >> 8); buffer[4] = (unsigned char)(ID >> 16); buffer[5] = (unsigned char)(ID >> 24); - buffer[6] = (unsigned char)(bus & 1); + buffer[6] = (unsigned char)((frame->bus - this->getBusBase()) & 1); buffer[7] = (unsigned char)frame->len; for (c = 0; c < frame->len; c++) { @@ -203,7 +200,7 @@ void SerialWorker::sendFrame(const CANFrame *frame, int bus = 0) void SerialWorker::sendFrameBatch(const QList *frames) { sendBulkMutex.lock(); - for (int i = 0; i < frames->length(); i++) sendFrame(&frames->at(i), frames->at(i).bus); + for (int i = 0; i < frames->length(); i++) sendFrame(&frames->at(i)); sendBulkMutex.unlock(); } @@ -321,15 +318,14 @@ void SerialWorker::procRXChar(unsigned char c) rx_state = IDLE; rx_step = 0; //qDebug() << "emit from serial handler to main form id: " << buildFrame->ID; - if (capturing) - { + //if (capturing) + //{ buildFrame->isReceived = true; - canModel->addFrame(*buildFrame, false); + model->addFrame(*buildFrame, false); //take the time the frame came in and try to resync the time base. if (continuousTimeSync) txTimestampBasis = QDateTime::currentMSecsSinceEpoch() - (buildFrame->timestamp / 1000); - framesRapid++; - if (buildFrame->ID == targetID) emit gotTargettedFrame(canModel->rowCount() - 1); - } + framesRapid++; + //} } break; } @@ -414,8 +410,8 @@ void SerialWorker::procRXChar(unsigned char c) qDebug() << "Baud 1 = " << can1Baud; if (!can1Enabled) can1Baud = 0; if (!can0Enabled) can0Baud = 0; - connected = true; - emit connectionSuccess(can0Baud, can1Baud); + isConnected = true; + emit connectionSuccess(); break; } rx_step++; @@ -462,7 +458,7 @@ void SerialWorker::handleTick() { //qDebug() << "Tick!"; - if (connected) + if (isConnected) { if (!gotValidated && doValidation) { @@ -517,6 +513,7 @@ void SerialWorker::closeSerialPort() serial = NULL; } +/* void SerialWorker::stopFrameCapture() { qDebug() << "Stopping frame capture"; @@ -528,8 +525,24 @@ void SerialWorker::startFrameCapture() qDebug() << "Starting up frame capture"; capturing = true; } +*/ -void SerialWorker::targetFrameID(int target) +void SerialWorker::updatePortName(QString portName) { - targetID = target; + QList ports; + + CANConnection::updatePortName(portName); + + ports = QSerialPortInfo::availablePorts(); + + for (int i = 0; i < ports.count(); i++) + { + if (portName == ports[i].portName()) + { + setSerialPort(&ports[i]); + return; + } + } } + + diff --git a/serialworker.h b/serialworker.h index 1658c04..df54810 100644 --- a/serialworker.h +++ b/serialworker.h @@ -8,6 +8,7 @@ #include #include "can_structs.h" #include "canframemodel.h" +#include "canconnection.h" namespace SERIALSTATE { @@ -29,23 +30,14 @@ enum STATE //keep this enum synchronized with the Arduino firmware project } using namespace SERIALSTATE; -class SerialWorker : public QObject +class SerialWorker : public CANConnection { Q_OBJECT public: - SerialWorker(CANFrameModel *model, QObject *parent = 0); + SerialWorker(CANFrameModel *, int); ~SerialWorker(); void readSettings(); - void targetFrameID(int); - -signals: //we emit signals - void error(const QString &); - void frameUpdateRapid(int); //sent *much* more rapidly than the above signal - one param for # of frames - void connectionSuccess(int, int); - void connectionFailure(); - void deviceInfo(int, int); - void gotTargettedFrame(int); private slots: //we receive things in slots void readSerialData(); @@ -54,31 +46,26 @@ private slots: //we receive things in slots void handleReconnect(); public slots: - void run(); + void run() override; void setSerialPort(QSerialPortInfo*); void closeSerialPort(); - void sendFrame(const CANFrame *, int); - void sendFrameBatch(const QList *); + void sendFrame(const CANFrame *) override; + void sendFrameBatch(const QList *) override; void updateBaudRates(int, int); - void stopFrameCapture(); - void startFrameCapture(); //only need to call this if previously stopped. Otherwise it's the default + //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 -private: - QString portName; - bool quit; - bool connected; - bool capturing; +private: bool doValidation; bool gotValidated; bool isAutoRestart; bool continuousTimeSync; QSerialPort *serial; QSerialPortInfo *currentPort; - CANFrameModel *canModel; QTimer *ticker; QMutex sendBulkMutex; int framesRapid; - int targetID; STATE rx_state; int rx_step; CANFrame *buildFrame;