diff --git a/connections/canconnection.cpp b/connections/canconnection.cpp index 41b6329..cdfb748 100644 --- a/connections/canconnection.cpp +++ b/connections/canconnection.cpp @@ -256,6 +256,10 @@ void CANConnection::setCapSuspended(bool pIsSuspended) { mIsCapSuspended = pIsSuspended; } +void CANConnection::debugInput(QByteArray bytes) { + +} + bool CANConnection::addTargettedFrame(int pBusId, uint32_t ID, uint32_t mask, QObject *receiver) { /* diff --git a/connections/canconnection.h b/connections/canconnection.h index c6471e2..e53b687 100644 --- a/connections/canconnection.h +++ b/connections/canconnection.h @@ -91,6 +91,11 @@ signals: */ void status(CANCon::status pStatus); + /** + * @brief Event sent when device has done something worthy of debugging output. + * @param debugString: String based output to show for debugging purposes + */ + void debugOutput(QString debugString); public slots: @@ -175,6 +180,8 @@ public slots: */ bool removeAllTargettedFrames(QObject *receiver); + void debugInput(QByteArray bytes); + protected: //determine if the passed frame is part of a filter or not. diff --git a/connections/connectionwindow.cpp b/connections/connectionwindow.cpp index 782385a..6e2c3f7 100644 --- a/connections/connectionwindow.cpp +++ b/connections/connectionwindow.cpp @@ -22,7 +22,6 @@ ConnectionWindow::ConnectionWindow(QWidget *parent) : qRegisterMetaType("const CANFrame *"); qRegisterMetaType *>("const QList *"); - connModel = new CANConnectionModel(this); ui->tableConnections->setModel(connModel); ui->tableConnections->setColumnWidth(0, 50); @@ -39,7 +38,13 @@ ConnectionWindow::ConnectionWindow(QWidget *parent) : ui->cbSpeed->addItem(tr("250000")); ui->cbSpeed->addItem(tr("500000")); ui->cbSpeed->addItem(tr("1000000")); - ui->cbSpeed->addItem(tr("33333")); + ui->cbSpeed->addItem(tr("33333")); + + ui->textConsole->setEnabled(false); + ui->btnClearDebug->setEnabled(false); + ui->btnSendHex->setEnabled(false); + ui->btnSendText->setEnabled(false); + ui->lineSend->setEnabled(false); /* load connection configuration */ loadConnections(); @@ -59,6 +64,10 @@ ConnectionWindow::ConnectionWindow(QWidget *parent) : connect(ui->btnActivateAll, &QPushButton::clicked, this, &ConnectionWindow::handleEnableAll); connect(ui->btnDeactivateAll, &QPushButton::clicked, this, &ConnectionWindow::handleDisableAll); connect(ui->btnRemoveBus, &QPushButton::clicked, this, &ConnectionWindow::handleRemoveConn); + connect(ui->btnClearDebug, &QPushButton::clicked, this, &ConnectionWindow::handleClearDebugText); + connect(ui->btnSendHex, &QPushButton::clicked, this, &ConnectionWindow::handleSendHex); + connect(ui->btnSendText, &QPushButton::clicked, this, &ConnectionWindow::handleSendText); + connect(ui->ckEnableConsole, &QCheckBox::toggled, this, &ConnectionWindow::consoleEnableChanged); } ConnectionWindow::~ConnectionWindow() @@ -119,6 +128,28 @@ void ConnectionWindow::setActiveAll(bool pActive) connModel->refresh(); } +void ConnectionWindow::consoleEnableChanged(bool checked) { + int busId; + + CANConnection* conn_p = connModel->getAtIdx(ui->tableConnections->currentIndex().row(), busId); + + ui->textConsole->setEnabled(checked); + ui->btnClearDebug->setEnabled(checked); + ui->btnSendHex->setEnabled(checked); + ui->btnSendText->setEnabled(checked); + ui->lineSend->setEnabled(checked); + + if(!conn_p) return; + + if (checked) { //enable console + connect(conn_p, SIGNAL(debugOutput(QString)), this, SLOT(getDebugText(QString))); + connect(this, SIGNAL(sendDebugData(QByteArray)), conn_p, SLOT(debugInput(QByteArray))); + } + else { //turn it off + disconnect(conn_p, SIGNAL(debugOutput(QString)), 0, 0); + disconnect(this, SIGNAL(sendDebugData(QByteArray)), conn_p, SLOT(debugInput(QByteArray))); + } +} void ConnectionWindow::handleNewConn() { @@ -209,14 +240,17 @@ void ConnectionWindow::handleOKButton() } } - - void ConnectionWindow::currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous) { Q_UNUSED(previous); int selIdx = current.row(); + int busId; + + disconnect(connModel->getAtIdx(previous.row(), busId), SIGNAL(debugOutput(QString)), 0, 0); + disconnect(this, SIGNAL(sendDebugData(QByteArray)), connModel->getAtIdx(previous.row(), busId), SLOT(debugInput(QByteArray))); + /* enable / diable connection type */ ui->stPort->setEnabled(selIdx==-1); ui->gbType->setEnabled(selIdx==-1); @@ -235,11 +269,16 @@ void ConnectionWindow::currentRowChanged(const QModelIndex ¤t, const QMode } else { - int busId; bool ret; CANBus bus; CANConnection* conn_p = connModel->getAtIdx(selIdx, busId); if(!conn_p) return; + + if (ui->ckEnableConsole->isChecked()) { //only connect if console is actually enabled + connect(conn_p, SIGNAL(debugOutput(QString)), this, SLOT(getDebugText(QString))); + connect(this, SIGNAL(sendDebugData(QByteArray)), conn_p, SLOT(debugInput(QByteArray))); + } + ret = conn_p->getBusSettings(busId, bus); if(!ret) return; @@ -252,6 +291,29 @@ void ConnectionWindow::currentRowChanged(const QModelIndex ¤t, const QMode } } +void ConnectionWindow::getDebugText(QString debugText) { + ui->textConsole->append(debugText); +} + +void ConnectionWindow::handleClearDebugText() { + ui->textConsole->clear(); +} + +void ConnectionWindow::handleSendHex() { + QByteArray bytes; + QStringList tokens = ui->lineSend->text().split(' '); + foreach (QString token, tokens) { + bytes.append(token.toInt(nullptr, 16)); + } + emit sendDebugData(bytes); +} + +void ConnectionWindow::handleSendText() { + QByteArray bytes; + bytes = ui->lineSend->text().toLatin1(); + bytes.append('\r'); //add carriage return for line ending + emit sendDebugData(bytes); +} void ConnectionWindow::selectSerial() { diff --git a/connections/connectionwindow.h b/connections/connectionwindow.h index 56062da..b3d6dfe 100644 --- a/connections/connectionwindow.h +++ b/connections/connectionwindow.h @@ -35,6 +35,7 @@ public: signals: void updateBusSettings(CANBus *bus); void updatePortName(QString port); + void sendDebugData(QByteArray bytes); public slots: void setSpeed(int speed0); @@ -42,15 +43,21 @@ public slots: void setSuspendAll(bool pSuspend); + void getDebugText(QString debugText); + private slots: void handleOKButton(); void handleConnTypeChanged(); void currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous); + void consoleEnableChanged(bool checked); void handleRemoveConn(); void handleEnableAll(); void handleDisableAll(); void handleRevert(); void handleNewConn(); + void handleClearDebugText(); + void handleSendHex(); + void handleSendText(); void connectionStatus(CANCon::status); private: diff --git a/connections/gvretserial.cpp b/connections/gvretserial.cpp index fbe1ba9..129f2af 100644 --- a/connections/gvretserial.cpp +++ b/connections/gvretserial.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "gvretserial.h" @@ -11,6 +12,7 @@ GVRetSerial::GVRetSerial(QString portName) : mTimer(this) /*NB: set this as parent of timer to manage it from working thread */ { qDebug() << "GVRetSerial()"; + debugOutput("GVRetSerial()"); serial = NULL; rx_state = IDLE; @@ -30,6 +32,7 @@ GVRetSerial::~GVRetSerial() { stop(); qDebug() << "~GVRetSerial()"; + debugOutput("~GVRetSerial()"); } @@ -126,6 +129,7 @@ void GVRetSerial::piSetBusSettings(int pBusIdx, CANBus bus) /* update baud rates */ QByteArray buffer; qDebug() << "Got signal to update bauds. 1: " << can0Baud <<" 2: " << can1Baud; + debugOutput("Got signal to update bauds. 1: " + QString::number(can0Baud) + " 2: " + QString::number(can1Baud)); buffer[0] = (char)0xF1; //start of a command over serial buffer[1] = 5; //setup canbus buffer[2] = (unsigned char)(can0Baud & 0xFF); //four bytes of ID LSB first @@ -175,6 +179,7 @@ bool GVRetSerial::piSendFrame(const CANFrame& frame) buffer[8 + frame.len] = 0; //qDebug() << "writing " << buffer.length() << " bytes to serial port"; + debugOutput("writing " + QString::number(buffer.length()) + " bytes to serial port"); serial->write(buffer); return true; @@ -208,6 +213,7 @@ void GVRetSerial::connectDevice() serial = new QSerialPort(QSerialPortInfo(getPort())); if(!serial) { qDebug() << "can't open serial port " << getPort(); + debugOutput("can't open serial port " + getPort()); return; } @@ -252,6 +258,12 @@ void GVRetSerial::connectDevice() continuousTimeSync = true; serial->write(output); + QString buildDebug; + buildDebug = "Write to serial -> "; + foreach (int byt, output) { + buildDebug = buildDebug % QString::number(byt, 16) % " "; + } + debugOutput(buildDebug); if(doValidation) { QTimer::singleShot(1000, this, SLOT(connectionTimeout())); @@ -300,15 +312,23 @@ void GVRetSerial::readSerialData() { QByteArray data = serial->readAll(); unsigned char c; + QString debugBuild; + debugOutput("Got data from serial. Len = " % QString::number(data.length())); //qDebug() << (tr("Got data from serial. Len = %0").arg(data.length())); for (int i = 0; i < data.length(); i++) { c = data.at(i); //qDebug() << c << " " << QString::number(c, 16) << " " << QString(c); + debugBuild = debugBuild % QString::number(c, 16) % " "; procRXChar(c); } + debugOutput(debugBuild); } +//Debugging data sent from connection window. Inject it into Comm traffic. +void GVRetSerial::debugInput(QByteArray bytes) { + serial->write(bytes); +} void GVRetSerial::procRXChar(unsigned char c) { diff --git a/connections/gvretserial.h b/connections/gvretserial.h index 5180b27..435963f 100644 --- a/connections/gvretserial.h +++ b/connections/gvretserial.h @@ -54,6 +54,9 @@ protected: void disconnectDevice(); +public slots: + void debugInput(QByteArray bytes); + private slots: void connectDevice(); void connectionTimeout(); diff --git a/ui/connectionwindow.ui b/ui/connectionwindow.ui index 5e7ffc4..634ba67 100644 --- a/ui/connectionwindow.ui +++ b/ui/connectionwindow.ui @@ -7,7 +7,7 @@ 0 0 956 - 527 + 665 @@ -224,7 +224,11 @@ - + + + true + + @@ -246,6 +250,75 @@ + + + + Enable Console + + + + + + + Device Console: + + + + + + + false + + + + + + + Send: + + + + + + + false + + + + + + + + + false + + + Send Hex + + + + + + + false + + + Send Text + + + + + + + false + + + Clear + + + + +