From 1ea9ea4fac14f857082012e7518ebefa97a37078 Mon Sep 17 00:00:00 2001 From: Collin Kidder Date: Mon, 20 Nov 2017 22:07:13 -0500 Subject: [PATCH] Add fault handling code mostly used to detect when the serial port disappears while a GVRET style device is connected. Beginnings of code to add a reconnect button. --- connections/connectionwindow.cpp | 14 +++++ connections/connectionwindow.h | 1 + connections/gvretserial.cpp | 90 +++++++++++++++++++++++++++++--- connections/gvretserial.h | 1 + ui/connectionwindow.ui | 7 +++ 5 files changed, 105 insertions(+), 8 deletions(-) diff --git a/connections/connectionwindow.cpp b/connections/connectionwindow.cpp index 99d084b..5021893 100644 --- a/connections/connectionwindow.cpp +++ b/connections/connectionwindow.cpp @@ -56,6 +56,7 @@ ConnectionWindow::ConnectionWindow(QWidget *parent) : connect(ui->tableConnections->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &ConnectionWindow::currentRowChanged); connect(ui->btnActivateAll, &QPushButton::clicked, this, &ConnectionWindow::handleEnableAll); connect(ui->btnDeactivateAll, &QPushButton::clicked, this, &ConnectionWindow::handleDisableAll); + connect(ui->btnReconnect, &QPushButton::clicked, this, &ConnectionWindow::handleReconnect); connect(ui->btnRemoveBus, &QPushButton::clicked, this, &ConnectionWindow::handleRemoveConn); connect(ui->btnClearDebug, &QPushButton::clicked, this, &ConnectionWindow::handleClearDebugText); connect(ui->btnSendHex, &QPushButton::clicked, this, &ConnectionWindow::handleSendHex); @@ -149,6 +150,19 @@ void ConnectionWindow::setActiveAll(bool pActive) connModel->refresh(); } +void ConnectionWindow::handleReconnect() +{ + int selIdx = ui->tableConnections->selectionModel()->currentIndex().row(); + if (selIdx <0) return; + + int busId; + CANConnection* conn_p = connModel->getAtIdx(selIdx, busId); + if(!conn_p) return; + + conn_p->stop(); + conn_p->start(); +} + void ConnectionWindow::consoleEnableChanged(bool checked) { int busId; diff --git a/connections/connectionwindow.h b/connections/connectionwindow.h index 77a0d88..104cbab 100644 --- a/connections/connectionwindow.h +++ b/connections/connectionwindow.h @@ -52,6 +52,7 @@ private slots: void handleRemoveConn(); void handleEnableAll(); void handleDisableAll(); + void handleReconnect(); void handleRevert(); void handleNewConn(); void handleClearDebugText(); diff --git a/connections/gvretserial.cpp b/connections/gvretserial.cpp index 0e38871..c5c334a 100644 --- a/connections/gvretserial.cpp +++ b/connections/gvretserial.cpp @@ -37,7 +37,7 @@ GVRetSerial::~GVRetSerial() void GVRetSerial::piStarted() -{ +{ connectDevice(); /* start timer */ @@ -328,6 +328,7 @@ void GVRetSerial::connectDevice() /* connect reading event */ connect(serial, SIGNAL(readyRead()), this, SLOT(readSerialData())); + connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(serialError(QSerialPort::SerialPortError))); } @@ -344,6 +345,82 @@ void GVRetSerial::disconnectDevice() { delete serial; serial = NULL; } + setStatus(CANCon::NOT_CONNECTED); + CANConStatus stats; + stats.conStatus = getStatus(); + stats.numHardwareBuses = mNumBuses; + emit status(stats); +} + +void GVRetSerial::serialError(QSerialPort::SerialPortError err) +{ + QString errMessage; + bool killConnection = false; + switch (err) + { + case QSerialPort::NoError: + return; + case QSerialPort::DeviceNotFoundError: + errMessage = "Device not found error on serial"; + killConnection = true; + break; + case QSerialPort::PermissionError: + errMessage = "Permission error on serial port"; + killConnection = true; + break; + case QSerialPort::OpenError: + errMessage = "Open error on serial port"; + killConnection = true; + break; + case QSerialPort::ParityError: + errMessage = "Parity error on serial port"; + break; + case QSerialPort::FramingError: + errMessage = "Framing error on serial port"; + break; + case QSerialPort::BreakConditionError: + errMessage = "Break error on serial port"; + break; + case QSerialPort::WriteError: + errMessage = "Write error on serial port"; + break; + case QSerialPort::ReadError: + errMessage = "Read error on serial port"; + break; + case QSerialPort::ResourceError: + errMessage = "Serial port seems to have disappeared."; + killConnection = true; + break; + case QSerialPort::UnsupportedOperationError: + errMessage = "Unsupported operation on serial port"; + killConnection = true; + break; + case QSerialPort::UnknownError: + errMessage = "Beats me what happened to the serial port."; + killConnection = true; + break; + case QSerialPort::TimeoutError: + errMessage = "Timeout error on serial port"; + killConnection = true; + break; + case QSerialPort::NotOpenError: + errMessage = "The serial port isn't open dummy"; + killConnection = true; + break; + } + serial->clearError(); + serial->flush(); + serial->close(); + if (errMessage.length() > 1) + { + qDebug() << errMessage; + debugOutput(errMessage); + } + if (killConnection) + { + qDebug() << "Shooting the serial object in the head. It deserves it."; + disconnectDevice(); + } } @@ -781,7 +858,7 @@ void GVRetSerial::handleTick() { if (lastSystemTimeBasis != CANConManager::getInstance()->getTimeBasis()) rebuildLocalTimeBasis(); //qDebug() << "Tick!"; -/* + if( CANCon::CONNECTED == getStatus() ) { if (!gotValidated && doValidation) @@ -792,16 +869,16 @@ void GVRetSerial::handleTick() qDebug() << "Comm validation failed. "; setStatus(CANCon::NOT_CONNECTED); - emit status(getStatus()); + //emit status(getStatus()); disconnectDevice(); //start by stopping everything. //Then wait 500ms and restart the connection automatically - QTimer::singleShot(500, this, SLOT(connectDevice())); + //QTimer::singleShot(500, this, SLOT(connectDevice())); return; } } + else if (doValidation); //qDebug() << "Comm connection validated"; } -*/ if (doValidation && serial && serial->isOpen()) sendCommValidation(); } @@ -813,9 +890,6 @@ void GVRetSerial::sendCommValidation() gotValidated = false; output.append((char)0xF1); //another command to the GVRET output.append((char)0x09); //request a reply to get validation - //send it twice for good measure. - output.append((char)0xF1); //another command to the GVRET - output.append((char)0x09); //request a reply to get validation serial->write(output); } diff --git a/connections/gvretserial.h b/connections/gvretserial.h index a4a3963..63e7f9f 100644 --- a/connections/gvretserial.h +++ b/connections/gvretserial.h @@ -62,6 +62,7 @@ private slots: void connectDevice(); void connectionTimeout(); void readSerialData(); + void serialError(QSerialPort::SerialPortError err); void handleTick(); private: diff --git a/ui/connectionwindow.ui b/ui/connectionwindow.ui index c24610e..d8b48f7 100644 --- a/ui/connectionwindow.ui +++ b/ui/connectionwindow.ui @@ -38,6 +38,13 @@ + + + + Reconnect + + +