From f18411c56719d443131e4a81149e421fe056e0cc Mon Sep 17 00:00:00 2001 From: Collin Kidder Date: Sat, 23 May 2015 10:10:30 -0400 Subject: [PATCH] It's now possible to freely connect and disconnect from/to the GVRET device. --- mainwindow.cpp | 27 +++++++++++++++++++++------ mainwindow.h | 2 ++ serialworker.cpp | 32 ++++++++++++++++++++++++++------ serialworker.h | 3 ++- 4 files changed, 51 insertions(+), 13 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index 2501026..134f64e 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -85,6 +85,7 @@ MainWindow::MainWindow(QWidget *parent) : connect(worker, &SerialWorker::connectionSuccess, this, &MainWindow::connectionSucceeded, Qt::QueuedConnection); connect(worker, &SerialWorker::connectionFailure, this, &MainWindow::connectionFailed, Qt::QueuedConnection); connect(worker, &SerialWorker::deviceInfo, this, &MainWindow::gotDeviceInfo, Qt::QueuedConnection); + connect(this, &MainWindow::closeSerialPort, worker, &SerialWorker::closeSerialPort, Qt::QueuedConnection); serialWorkerThread.start(); graphingWindow = NULL; @@ -116,6 +117,8 @@ MainWindow::MainWindow(QWidget *parent) : ui->statusBar->addWidget(&lbStatusConnected); ui->statusBar->addWidget(&lbStatusBauds); ui->statusBar->addWidget(&lbStatusDatabase); + + isConnected = false; } MainWindow::~MainWindow() @@ -654,15 +657,25 @@ void MainWindow::showEditSignalsWindow() void MainWindow::connButtonPress() { - for (int x = 0; x < ports.count(); x++) + if (!isConnected) { - if (ports.at(x).portName() == ui->cbSerialPorts->currentText()) + for (int x = 0; x < ports.count(); x++) { - emit sendSerialPort(&ports[x]); - lbStatusConnected.setText(tr("Attempting to connect to port ") + ports[x].portName()); - return; + if (ports.at(x).portName() == ui->cbSerialPorts->currentText()) + { + emit sendSerialPort(&ports[x]); + lbStatusConnected.setText(tr("Attempting to connect to port ") + ports[x].portName()); + return; + } } } + else + { + emit closeSerialPort(); + isConnected = false; + lbStatusConnected.setText(tr("Not Connected")); + ui->btnConnect->setText(tr("Connect to GVRET")); + } } void MainWindow::connectionSucceeded(int baud0, int baud1) @@ -688,7 +701,9 @@ void MainWindow::connectionSucceeded(int baud0, int baud1) ui->cbSpeed2->addItem(QString::number(baud1)); ui->cbSpeed2->setCurrentIndex(ui->cbSpeed2->count() - 1); } - ui->btnConnect->setEnabled(false); + //ui->btnConnect->setEnabled(false); + ui->btnConnect->setText(tr("Disconnect from GVRET")); + isConnected = true; } void MainWindow::connectionFailed() diff --git a/mainwindow.h b/mainwindow.h index 88fc1f2..3690413 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -51,6 +51,7 @@ public slots: signals: void sendSerialPort(QSerialPortInfo *port); + void closeSerialPort(); void updateBaudRates(int, int); void sendCANFrame(const CANFrame *, int); @@ -69,6 +70,7 @@ private: QLabel lbStatusConnected; QLabel lbStatusBauds; QLabel lbStatusDatabase; + bool isConnected; void loadCRTDFile(QString); void loadNativeCSVFile(QString); void loadGenericCSVFile(QString); diff --git a/serialworker.cpp b/serialworker.cpp index a356d23..860037b 100644 --- a/serialworker.cpp +++ b/serialworker.cpp @@ -11,12 +11,8 @@ SerialWorker::SerialWorker(CANFrameModel *model, QObject *parent) : QObject(pare rx_step = 0; buildFrame = new CANFrame; canModel = model; - ticker.setInterval(250); //tick four times per second - ticker.setSingleShot(false); //keep ticking - ticker.start(); gotFrames = 0; - - connect(&ticker, SIGNAL(timeout()), this, SLOT(handleTick())); + ticker = NULL; } SerialWorker::~SerialWorker() @@ -32,7 +28,7 @@ SerialWorker::~SerialWorker() serial->disconnect(); //disconnect all signals delete serial; } - ticker.stop(); + ticker->stop(); } void SerialWorker::setSerialPort(QSerialPortInfo *port) @@ -68,6 +64,14 @@ void SerialWorker::setSerialPort(QSerialPortInfo *port) connected = false; connect(serial, SIGNAL(readyRead()), this, SLOT(readSerialData())); QTimer::singleShot(1000, this, SLOT(connectionTimeout())); + if (ticker == NULL) + { + ticker = new QTimer; + connect(ticker, SIGNAL(timeout()), this, SLOT(handleTick())); + } + ticker->setInterval(250); //tick four times per second + ticker->setSingleShot(false); //keep ticking + ticker->start(); } void SerialWorker::connectionTimeout() @@ -77,6 +81,7 @@ void SerialWorker::connectionTimeout() { //then emit the the failure signal and see if anyone cares qDebug() << "Failed to connect to GVRET at that com port"; + ticker->stop(); emit connectionFailure(); } } @@ -335,6 +340,7 @@ void SerialWorker::procRXChar(unsigned char c) void SerialWorker::handleTick() { + //qDebug() << "Tick!"; if (gotFrames > 0) { emit receivedFrames(); //notify interested parties that there are new frames @@ -342,3 +348,17 @@ void SerialWorker::handleTick() gotFrames = 0; } } + +//totally shuts down the whole thing +void SerialWorker::closeSerialPort() +{ + if (serial->isOpen()) + { + serial->clear(); + serial->close(); + } + serial->disconnect(); + ticker->stop(); + delete serial; + serial = NULL; +} diff --git a/serialworker.h b/serialworker.h index c8a43a7..e41f7fe 100644 --- a/serialworker.h +++ b/serialworker.h @@ -44,6 +44,7 @@ private slots: //we receive things in slots public slots: void setSerialPort(QSerialPortInfo*); + void closeSerialPort(); void sendFrame(const CANFrame *, int); void updateBaudRates(int, int); @@ -53,7 +54,7 @@ private: bool connected; QSerialPort *serial; CANFrameModel *canModel; - QTimer ticker; + QTimer *ticker; int gotFrames; STATE rx_state; int rx_step;