diff --git a/mainwindow.cpp b/mainwindow.cpp index f717eef..9bf45c5 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -68,6 +68,7 @@ MainWindow::MainWindow(QWidget *parent) : connect(worker, &SerialWorker::receivedFrame, this, &MainWindow::gotFrame, Qt::QueuedConnection); connect(this, &MainWindow::updateBaudRates, worker, &SerialWorker::updateBaudRates, Qt::QueuedConnection); connect(this, &MainWindow::sendCANFrame, worker, &SerialWorker::sendFrame, Qt::QueuedConnection); + connect(worker, &SerialWorker::connectionSuccess, this, &MainWindow::connectionSucceeded, Qt::QueuedConnection); serialWorkerThread.start(); graphingWindow = NULL; @@ -92,7 +93,7 @@ MainWindow::MainWindow(QWidget *parent) : connect(ui->actionSave_DBC_File, SIGNAL(triggered(bool)), this, SLOT(handleSaveDBC())); lbStatusConnected.setText(tr("Not connected")); - lbStatusBauds.setText(tr("Baud 1: 0 Baud 2: 0")); + updateBaudLabel(0,0); lbStatusDatabase.setText(tr("No database loaded")); ui->statusBar->addWidget(&lbStatusConnected); ui->statusBar->addWidget(&lbStatusBauds); @@ -114,6 +115,18 @@ MainWindow::~MainWindow() serialWorkerThread.wait(); } +void MainWindow::updateBaudLabel(int baud0, int baud1) +{ + QString labelText; + labelText = tr("Baud 0: "); + if (baud0 > 0) labelText += QString::number(baud0); + else labelText += tr("disabled"); + labelText += tr(" Baud 1: "); + if (baud1 > 0) labelText += QString::number(baud1); + else labelText += tr("disabled"); + lbStatusBauds.setText(labelText); +} + void MainWindow::gotFrame(CANFrame *frame) { qDebug() << "got frame from serial side. ID was " << frame->ID; @@ -142,50 +155,12 @@ void MainWindow::changeBaudRates() { int Speed1 = 0, Speed2 = 0; - switch (ui->cbSpeed1->currentIndex()) - { - case 0: //disable - break; - case 1: - Speed1 = 125000; - break; - case 2: - Speed1 = 250000; - break; - case 3: - Speed1 = 500000; - break; - case 4: - Speed1 = 1000000; - break; - case 5: - Speed1 = 33333; - break; - } - switch (ui->cbSpeed2->currentIndex()) - { - case 0: //disable - break; - case 1: - Speed2 = 125000; - break; - case 2: - Speed2 = 250000; - break; - case 3: - Speed2 = 500000; - break; - case 4: - Speed2 = 1000000; - break; - case 5: - Speed2 = 33333; - break; - } + Speed1 = ui->cbSpeed1->currentText().toInt(); + Speed2 = ui->cbSpeed2->currentText().toInt(); emit updateBaudRates(Speed1, Speed2); - lbStatusBauds.setText(tr("Baud 1: ") + QString::number(Speed1) + tr(" Baud 2:") + QString::number(Speed2)); + updateBaudLabel(Speed1, Speed2); } //CRTD format from Mark Webb-Johnson / OVMS project @@ -628,6 +603,9 @@ void MainWindow::handleLoadDBC() //right now there is only one file type that can be loaded here so just do it. dbcHandler->loadDBCFile(filename); + dbcHandler->listDebugging(); + QStringList fileList = filename.split('/'); + lbStatusDatabase.setText(fileList[fileList.length() - 1] + tr(" loaded.")); } void MainWindow::handleSaveDBC() @@ -661,13 +639,39 @@ void MainWindow::connButtonPress() { if (ports.at(x).portName() == ui->cbSerialPorts->currentText()) { - emit sendSerialPort(&ports[x]); - lbStatusConnected.setText(tr("Connected to port ") + ports[x].portName()); + emit sendSerialPort(&ports[x]); + lbStatusConnected.setText(tr("Attempting to connect to port ") + ports[x].portName()); return; } } } +void MainWindow::connectionSucceeded(int baud0, int baud1) +{ + lbStatusConnected.setText(tr("Connected to GVRET")); + //now update the status message for baud rates + updateBaudLabel(baud0, baud1); + //finally, find the baud rate in the list of rates or + //add it to the bottom if needed (that'll be weird though... + + int idx = ui->cbSpeed1->findText(QString::number(baud0)); + if (idx > -1) ui->cbSpeed1->setCurrentIndex(idx); + else + { + ui->cbSpeed1->addItem(QString::number(baud0)); + ui->cbSpeed1->setCurrentIndex(ui->cbSpeed1->count() - 1); + } + + idx = ui->cbSpeed2->findText(QString::number(baud0)); + if (idx > -1) ui->cbSpeed2->setCurrentIndex(idx); + else + { + ui->cbSpeed2->addItem(QString::number(baud1)); + ui->cbSpeed2->setCurrentIndex(ui->cbSpeed2->count() - 1); + } + ui->btnConnect->setEnabled(false); +} + void MainWindow::showGraphingWindow() { if (!graphingWindow) graphingWindow = new GraphingWindow(model->getListReference()); diff --git a/mainwindow.h b/mainwindow.h index 2049808..b277592 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -42,6 +42,7 @@ private slots: public slots: void gotFrame(CANFrame *frame); + void connectionSucceeded(int, int); signals: void sendSerialPort(QSerialPortInfo *port); @@ -74,6 +75,7 @@ private: void saveLogFile(QString); void saveMicrochipFile(QString); void addFrameToDisplay(CANFrame &, bool); + void updateBaudLabel(int, int); }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index e38161f..e28929b 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -65,7 +65,7 @@ - Connect To Dongle + Connect To GVRET diff --git a/serialworker.cpp b/serialworker.cpp index fb55f54..eef3136 100644 --- a/serialworker.cpp +++ b/serialworker.cpp @@ -13,17 +13,29 @@ SerialWorker::SerialWorker(QObject *parent) : QObject(parent) SerialWorker::~SerialWorker() { - if (serial != NULL) delete serial; + if (serial != NULL) + { + if (serial->isOpen()) + { + serial->clear(); + serial->close(); + + } + serial->disconnect(); //disconnect all signals + delete serial; + } } void SerialWorker::setSerialPort(QSerialPortInfo *port) { - if (!(serial == NULL)) + if (serial != NULL) { if (serial->isOpen()) { + serial->clear(); serial->close(); } + serial->disconnect(); //disconnect all signals delete serial; } @@ -37,8 +49,10 @@ void SerialWorker::setSerialPort(QSerialPortInfo *port) serial->setDataTerminalReady(true); serial->setRequestToSend(true); QByteArray output; + output.append(0xE7); //this puts the device into binary comm mode output.append(0xE7); - output.append(0xE7); + output.append(0xF1); //signal we want to issue a command + output.append(0x06); //request canbus stats from the board serial->write(output); ///isConnected = true; connect(serial, SIGNAL(readyRead()), this, SLOT(readSerialData())); @@ -137,6 +151,9 @@ void SerialWorker::procRXChar(unsigned char c) case 5: //we set canbus specs we don't accept replies. rx_state = IDLE; break; + case 6: //get canbus parameters from GVRET + rx_state = GET_CANBUS_PARAMS; + rx_step = 0; } break; case BUILD_CAN_FRAME: @@ -213,5 +230,47 @@ void SerialWorker::procRXChar(unsigned char c) } rx_step++; break; + case GET_CANBUS_PARAMS: + switch (rx_step) + { + case 0: + can0Enabled = c; + break; + case 1: + can0Baud = c; + break; + case 2: + can0Baud |= c << 8; + break; + case 3: + can0Baud |= c << 16; + break; + case 4: + can0Baud |= c << 24; + break; + case 5: + can1Enabled = c; + break; + case 6: + can1Baud = c; + break; + case 7: + can1Baud |= c << 8; + break; + case 8: + can1Baud |= c << 16; + break; + case 9: + can1Baud |= c << 24; + rx_step = IDLE; + qDebug() << "Baud 0 = " << can0Baud; + qDebug() << "Baud 1 = " << can1Baud; + if (!can1Enabled) can1Baud = 0; + if (!can0Enabled) can0Baud = 0; + emit connectionSuccess(can0Baud, can1Baud); + break; + } + rx_step++; + break; } } diff --git a/serialworker.h b/serialworker.h index b7d2464..de09f4e 100644 --- a/serialworker.h +++ b/serialworker.h @@ -15,7 +15,8 @@ enum STATE //keep this enum synchronized with the Arduino firmware project GET_DIG_INPUTS, GET_ANALOG_INPUTS, SET_DIG_OUTPUTS, - SETUP_CANBUS + SETUP_CANBUS, + GET_CANBUS_PARAMS }; class SerialWorker : public QObject @@ -29,6 +30,8 @@ public: signals: //we emit signals void error(const QString &); void receivedFrame(CANFrame *); + void connectionSuccess(int, int); + void connectionFailure(); private slots: //we receive things in slots void readSerialData(); @@ -38,7 +41,6 @@ public slots: void sendFrame(const CANFrame *, int); void updateBaudRates(int, int); - private: QString portName; bool quit; @@ -46,6 +48,8 @@ private: STATE rx_state; int rx_step; CANFrame *buildFrame; + int can0Baud, can1Baud; + bool can0Enabled, can1Enabled; void procRXChar(unsigned char); };