It's now possible to freely connect and disconnect from/to the GVRET device.

This commit is contained in:
Collin Kidder
2015-05-23 10:10:30 -04:00
parent dcbc7ebfed
commit f18411c567
4 changed files with 51 additions and 13 deletions
+21 -6
View File
@@ -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()
+2
View File
@@ -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);
+26 -6
View File
@@ -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;
}
+2 -1
View File
@@ -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;