diff --git a/connections/canconconst.h b/connections/canconconst.h index 386407a..fa117b3 100644 --- a/connections/canconconst.h +++ b/connections/canconconst.h @@ -17,6 +17,7 @@ namespace CANCon { GVRET_SERIAL, KVASER, SOCKETCAN, + REMOTE, NONE }; } diff --git a/connections/canconfactory.cpp b/connections/canconfactory.cpp index 259fd29..5042ac0 100644 --- a/connections/canconfactory.cpp +++ b/connections/canconfactory.cpp @@ -8,11 +8,13 @@ using namespace CANCon; CANConnection* CanConFactory::create(type pType, QString pPortName) { switch(pType) { - case SOCKETCAN: - return new SerialBusConnection(pPortName); - case GVRET_SERIAL: - return new GVRetSerial(pPortName); - default: {} + case SOCKETCAN: + return new SerialBusConnection(pPortName); + case GVRET_SERIAL: + return new GVRetSerial(pPortName); + case REMOTE: + return new GVRetSerial(pPortName); //it's a special case of GVRET connected over TCP/IP so it uses the same class + default: {} } return NULL; diff --git a/connections/connectionwindow.cpp b/connections/connectionwindow.cpp index 9ecd213..d0e3834 100644 --- a/connections/connectionwindow.cpp +++ b/connections/connectionwindow.cpp @@ -356,7 +356,7 @@ void ConnectionWindow::selectSocketCan() void ConnectionWindow::selectRemote() { - ui->lPort->setText("Access Key:"); + ui->lPort->setText("IP Address:"); ui->stPort->setCurrentWidget(ui->etPage); } @@ -394,6 +394,7 @@ void ConnectionWindow::setPortName(CANCon::type pType, QString pPortName) break; } case CANCon::SOCKETCAN: + case CANCon::REMOTE: { ui->lePort->setText(pPortName); break; @@ -416,6 +417,7 @@ QString ConnectionWindow::getPortName() case CANCon::KVASER: return ui->cbPort->currentText(); case CANCon::SOCKETCAN: + case CANCon::REMOTE: return ui->lePort->text(); default: qDebug() << "getPortName: can't get port"; @@ -429,7 +431,7 @@ CANCon::type ConnectionWindow::getConnectionType() if (ui->rbGVRET->isChecked()) return CANCon::GVRET_SERIAL; if (ui->rbKvaser->isChecked()) return CANCon::KVASER; if (ui->rbSocketCAN->isChecked()) return CANCon::SOCKETCAN; - + if (ui->rbRemote->isChecked()) return CANCon::REMOTE; qDebug() << "getConnectionType: error"; return CANCon::NONE; } diff --git a/connections/gvretserial.cpp b/connections/gvretserial.cpp index 0bc861c..98ecf54 100644 --- a/connections/gvretserial.cpp +++ b/connections/gvretserial.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "gvretserial.h" @@ -15,6 +16,7 @@ GVRetSerial::GVRetSerial(QString portName) : debugOutput("GVRetSerial()"); serial = NULL; + tcpClient = NULL; rx_state = IDLE; rx_step = 0; gotValidated = true; @@ -37,17 +39,24 @@ GVRetSerial::~GVRetSerial() void GVRetSerial::sendToSerial(const QByteArray &bytes) { - if (serial == NULL) + if (serial == NULL && tcpClient == NULL) { debugOutput("Attempt to write to serial port when it has not been initialized!"); return; } - if (!serial->isOpen()) + + if (serial && !serial->isOpen()) { debugOutput("Attempt to write to serial port when it is not open!"); return; } + if (tcpClient && !tcpClient->isOpen()) + { + debugOutput("Attempt to write to TCP/IP port when it is not open!"); + return; + } + QString buildDebug; buildDebug = "Write to serial -> "; foreach (int byt, bytes) { @@ -56,7 +65,8 @@ void GVRetSerial::sendToSerial(const QByteArray &bytes) } debugOutput(buildDebug); - serial->write(bytes); + if (serial) serial->write(bytes); + if (tcpClient) tcpClient->write(bytes); } void GVRetSerial::piStarted() @@ -271,29 +281,52 @@ void GVRetSerial::connectDevice() /* disconnect device */ if(serial) disconnectDevice(); + if(tcpClient) + disconnectDevice(); /* open new device */ - serial = new QSerialPort(QSerialPortInfo(getPort())); - if(!serial) { - qDebug() << "can't open serial port " << getPort(); - debugOutput("can't open serial port " + getPort()); - return; - } - debugOutput("Created Serial Port Object"); - - /* configure */ - serial->setDataBits(serial->Data8); - serial->setFlowControl(serial->HardwareControl); //this is important though - if (!serial->open(QIODevice::ReadWrite)) + if (getPort().contains('.')) //TCP/IP mode then since it looks like an IP address { - qDebug() << serial->errorString(); + tcpClient = new QTcpSocket(); + tcpClient->connectToHost(getPort(), 23); + connect(tcpClient, SIGNAL(readyRead()), this, SLOT(readSerialData())); + connect(tcpClient, SIGNAL(connected()), this, SLOT(tcpConnected())); } - serial->setDataTerminalReady(true); //you do need to set these or the fan gets dirty - serial->setRequestToSend(true); + else { + serial = new QSerialPort(QSerialPortInfo(getPort())); + if(!serial) { + qDebug() << "can't open serial port " << getPort(); + debugOutput("can't open serial port " + getPort()); + return; + } + debugOutput("Created Serial Port Object"); - debugOutput("Opened Serial Port"); + /* configure */ + serial->setDataBits(serial->Data8); + //serial->setFlowControl(serial->HardwareControl); //this is important though + serial->setFlowControl(serial->NoFlowControl); + serial->setBaudRate(1000000); + if (!serial->open(QIODevice::ReadWrite)) + { + qDebug() << serial->errorString(); + } + serial->setDataTerminalReady(false); //you do need to set these or the fan gets dirty + serial->setRequestToSend(false); + //serial->setDataTerminalReady(true); //you do need to set these or the fan gets dirty + //serial->setRequestToSend(true); + debugOutput("Opened Serial Port"); + /* connect reading event */ + connect(serial, SIGNAL(readyRead()), this, SLOT(readSerialData())); + connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(serialError(QSerialPort::SerialPortError))); + } + if (serial) tcpConnected(); //well, not a proper function name I guess... +} + +void GVRetSerial::tcpConnected() +{ + qDebug() << "Connected to GVRET Device!"; QByteArray output; output.append((unsigned char)0xE7); //this puts the device into binary comm mode output.append((unsigned char)0xE7); @@ -339,13 +372,8 @@ void GVRetSerial::connectDevice() stats.numHardwareBuses = mNumBuses; emit status(stats); } - - /* connect reading event */ - connect(serial, SIGNAL(readyRead()), this, SLOT(readSerialData())); - connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(serialError(QSerialPort::SerialPortError))); } - void GVRetSerial::disconnectDevice() { if (serial != NULL) { @@ -359,6 +387,16 @@ void GVRetSerial::disconnectDevice() { delete serial; serial = NULL; } + if (tcpClient != NULL) + { + if (tcpClient->isOpen()) + { + tcpClient->close(); + } + tcpClient->disconnect(); + delete tcpClient; + tcpClient = NULL; + } setStatus(CANCon::NOT_CONNECTED); CANConStatus stats; stats.conStatus = getStatus(); @@ -454,9 +492,13 @@ void GVRetSerial::connectionTimeout() void GVRetSerial::readSerialData() { - QByteArray data = serial->readAll(); + QByteArray data; unsigned char c; QString debugBuild; + + if (serial) data = serial->readAll(); + if (tcpClient) data = tcpClient->readAll(); + 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++) @@ -837,8 +879,11 @@ void GVRetSerial::procRXChar(unsigned char c) qDebug() << "SWCAN Baud = " << swcanBaud; qDebug() << "LIN1 Baud = " << lin1Baud; qDebug() << "LIN2 Baud = " << lin2Baud; - mBusData[2].mBus.setSpeed(swcanBaud); - mBusData[2].mBus.setEnabled(swcanEnabled); + if (getNumBuses() > 2) + { + mBusData[2].mBus.setSpeed(swcanBaud); + mBusData[2].mBus.setEnabled(swcanEnabled); + } setStatus(CANCon::CONNECTED); stats.conStatus = getStatus(); @@ -877,8 +922,8 @@ void GVRetSerial::handleTick() { if (!gotValidated && doValidation) { - if (serial == NULL) return; - if (serial->isOpen()) //if it's still false we have a problem... + if (serial == NULL && tcpClient == NULL) return; + if ( (serial && serial->isOpen()) || (tcpClient && tcpClient->isOpen())) //if it's still false we have a problem... { qDebug() << "Comm validation failed. "; @@ -894,6 +939,7 @@ void GVRetSerial::handleTick() else if (doValidation); //qDebug() << "Comm connection validated"; } if (doValidation && serial && serial->isOpen()) sendCommValidation(); + if (doValidation && tcpClient && tcpClient->isOpen()) sendCommValidation(); } diff --git a/connections/gvretserial.h b/connections/gvretserial.h index 345d0d8..6a54ad6 100644 --- a/connections/gvretserial.h +++ b/connections/gvretserial.h @@ -5,6 +5,7 @@ #include #include #include +#include /*************/ #include @@ -63,6 +64,7 @@ private slots: void connectionTimeout(); void readSerialData(); void serialError(QSerialPort::SerialPortError err); + void tcpConnected(); void handleTick(); private: @@ -81,6 +83,7 @@ protected: bool isAutoRestart; bool continuousTimeSync; QSerialPort *serial; + QTcpSocket *tcpClient; int framesRapid; STATE rx_state; uint32_t rx_step;