Added a mode for TCP/IP network connection to a GVRET compatible device (ESP32RET supports this)

This commit is contained in:
Collin Kidder
2018-04-17 20:01:53 -04:00
parent 63ee973b9c
commit 60abd15461
5 changed files with 90 additions and 36 deletions
+1
View File
@@ -17,6 +17,7 @@ namespace CANCon {
GVRET_SERIAL, GVRET_SERIAL,
KVASER, KVASER,
SOCKETCAN, SOCKETCAN,
REMOTE,
NONE NONE
}; };
} }
+2
View File
@@ -12,6 +12,8 @@ CANConnection* CanConFactory::create(type pType, QString pPortName)
return new SerialBusConnection(pPortName); return new SerialBusConnection(pPortName);
case GVRET_SERIAL: case GVRET_SERIAL:
return new GVRetSerial(pPortName); 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: {} default: {}
} }
+4 -2
View File
@@ -356,7 +356,7 @@ void ConnectionWindow::selectSocketCan()
void ConnectionWindow::selectRemote() void ConnectionWindow::selectRemote()
{ {
ui->lPort->setText("Access Key:"); ui->lPort->setText("IP Address:");
ui->stPort->setCurrentWidget(ui->etPage); ui->stPort->setCurrentWidget(ui->etPage);
} }
@@ -394,6 +394,7 @@ void ConnectionWindow::setPortName(CANCon::type pType, QString pPortName)
break; break;
} }
case CANCon::SOCKETCAN: case CANCon::SOCKETCAN:
case CANCon::REMOTE:
{ {
ui->lePort->setText(pPortName); ui->lePort->setText(pPortName);
break; break;
@@ -416,6 +417,7 @@ QString ConnectionWindow::getPortName()
case CANCon::KVASER: case CANCon::KVASER:
return ui->cbPort->currentText(); return ui->cbPort->currentText();
case CANCon::SOCKETCAN: case CANCon::SOCKETCAN:
case CANCon::REMOTE:
return ui->lePort->text(); return ui->lePort->text();
default: default:
qDebug() << "getPortName: can't get port"; qDebug() << "getPortName: can't get port";
@@ -429,7 +431,7 @@ CANCon::type ConnectionWindow::getConnectionType()
if (ui->rbGVRET->isChecked()) return CANCon::GVRET_SERIAL; if (ui->rbGVRET->isChecked()) return CANCon::GVRET_SERIAL;
if (ui->rbKvaser->isChecked()) return CANCon::KVASER; if (ui->rbKvaser->isChecked()) return CANCon::KVASER;
if (ui->rbSocketCAN->isChecked()) return CANCon::SOCKETCAN; if (ui->rbSocketCAN->isChecked()) return CANCon::SOCKETCAN;
if (ui->rbRemote->isChecked()) return CANCon::REMOTE;
qDebug() << "getConnectionType: error"; qDebug() << "getConnectionType: error";
return CANCon::NONE; return CANCon::NONE;
} }
+60 -14
View File
@@ -4,6 +4,7 @@
#include <QSerialPortInfo> #include <QSerialPortInfo>
#include <QSettings> #include <QSettings>
#include <QStringBuilder> #include <QStringBuilder>
#include <QtNetwork>
#include "gvretserial.h" #include "gvretserial.h"
@@ -15,6 +16,7 @@ GVRetSerial::GVRetSerial(QString portName) :
debugOutput("GVRetSerial()"); debugOutput("GVRetSerial()");
serial = NULL; serial = NULL;
tcpClient = NULL;
rx_state = IDLE; rx_state = IDLE;
rx_step = 0; rx_step = 0;
gotValidated = true; gotValidated = true;
@@ -37,17 +39,24 @@ GVRetSerial::~GVRetSerial()
void GVRetSerial::sendToSerial(const QByteArray &bytes) 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!"); debugOutput("Attempt to write to serial port when it has not been initialized!");
return; return;
} }
if (!serial->isOpen())
if (serial && !serial->isOpen())
{ {
debugOutput("Attempt to write to serial port when it is not open!"); debugOutput("Attempt to write to serial port when it is not open!");
return; return;
} }
if (tcpClient && !tcpClient->isOpen())
{
debugOutput("Attempt to write to TCP/IP port when it is not open!");
return;
}
QString buildDebug; QString buildDebug;
buildDebug = "Write to serial -> "; buildDebug = "Write to serial -> ";
foreach (int byt, bytes) { foreach (int byt, bytes) {
@@ -56,7 +65,8 @@ void GVRetSerial::sendToSerial(const QByteArray &bytes)
} }
debugOutput(buildDebug); debugOutput(buildDebug);
serial->write(bytes); if (serial) serial->write(bytes);
if (tcpClient) tcpClient->write(bytes);
} }
void GVRetSerial::piStarted() void GVRetSerial::piStarted()
@@ -271,8 +281,18 @@ void GVRetSerial::connectDevice()
/* disconnect device */ /* disconnect device */
if(serial) if(serial)
disconnectDevice(); disconnectDevice();
if(tcpClient)
disconnectDevice();
/* open new device */ /* open new device */
if (getPort().contains('.')) //TCP/IP mode then since it looks like an IP address
{
tcpClient = new QTcpSocket();
tcpClient->connectToHost(getPort(), 23);
connect(tcpClient, SIGNAL(readyRead()), this, SLOT(readSerialData()));
connect(tcpClient, SIGNAL(connected()), this, SLOT(tcpConnected()));
}
else {
serial = new QSerialPort(QSerialPortInfo(getPort())); serial = new QSerialPort(QSerialPortInfo(getPort()));
if(!serial) { if(!serial) {
qDebug() << "can't open serial port " << getPort(); qDebug() << "can't open serial port " << getPort();
@@ -283,17 +303,30 @@ void GVRetSerial::connectDevice()
/* configure */ /* configure */
serial->setDataBits(serial->Data8); serial->setDataBits(serial->Data8);
serial->setFlowControl(serial->HardwareControl); //this is important though //serial->setFlowControl(serial->HardwareControl); //this is important though
serial->setFlowControl(serial->NoFlowControl);
serial->setBaudRate(1000000);
if (!serial->open(QIODevice::ReadWrite)) if (!serial->open(QIODevice::ReadWrite))
{ {
qDebug() << serial->errorString(); qDebug() << serial->errorString();
} }
serial->setDataTerminalReady(true); //you do need to set these or the fan gets dirty serial->setDataTerminalReady(false); //you do need to set these or the fan gets dirty
serial->setRequestToSend(true); serial->setRequestToSend(false);
//serial->setDataTerminalReady(true); //you do need to set these or the fan gets dirty
//serial->setRequestToSend(true);
debugOutput("Opened Serial Port"); 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; QByteArray output;
output.append((unsigned char)0xE7); //this puts the device into binary comm mode output.append((unsigned char)0xE7); //this puts the device into binary comm mode
output.append((unsigned char)0xE7); output.append((unsigned char)0xE7);
@@ -339,13 +372,8 @@ void GVRetSerial::connectDevice()
stats.numHardwareBuses = mNumBuses; stats.numHardwareBuses = mNumBuses;
emit status(stats); 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() { void GVRetSerial::disconnectDevice() {
if (serial != NULL) if (serial != NULL)
{ {
@@ -359,6 +387,16 @@ void GVRetSerial::disconnectDevice() {
delete serial; delete serial;
serial = NULL; serial = NULL;
} }
if (tcpClient != NULL)
{
if (tcpClient->isOpen())
{
tcpClient->close();
}
tcpClient->disconnect();
delete tcpClient;
tcpClient = NULL;
}
setStatus(CANCon::NOT_CONNECTED); setStatus(CANCon::NOT_CONNECTED);
CANConStatus stats; CANConStatus stats;
stats.conStatus = getStatus(); stats.conStatus = getStatus();
@@ -454,9 +492,13 @@ void GVRetSerial::connectionTimeout()
void GVRetSerial::readSerialData() void GVRetSerial::readSerialData()
{ {
QByteArray data = serial->readAll(); QByteArray data;
unsigned char c; unsigned char c;
QString debugBuild; QString debugBuild;
if (serial) data = serial->readAll();
if (tcpClient) data = tcpClient->readAll();
debugOutput("Got data from serial. Len = " % QString::number(data.length())); debugOutput("Got data from serial. Len = " % QString::number(data.length()));
//qDebug() << (tr("Got data from serial. Len = %0").arg(data.length())); //qDebug() << (tr("Got data from serial. Len = %0").arg(data.length()));
for (int i = 0; i < data.length(); i++) for (int i = 0; i < data.length(); i++)
@@ -837,8 +879,11 @@ void GVRetSerial::procRXChar(unsigned char c)
qDebug() << "SWCAN Baud = " << swcanBaud; qDebug() << "SWCAN Baud = " << swcanBaud;
qDebug() << "LIN1 Baud = " << lin1Baud; qDebug() << "LIN1 Baud = " << lin1Baud;
qDebug() << "LIN2 Baud = " << lin2Baud; qDebug() << "LIN2 Baud = " << lin2Baud;
if (getNumBuses() > 2)
{
mBusData[2].mBus.setSpeed(swcanBaud); mBusData[2].mBus.setSpeed(swcanBaud);
mBusData[2].mBus.setEnabled(swcanEnabled); mBusData[2].mBus.setEnabled(swcanEnabled);
}
setStatus(CANCon::CONNECTED); setStatus(CANCon::CONNECTED);
stats.conStatus = getStatus(); stats.conStatus = getStatus();
@@ -877,8 +922,8 @@ void GVRetSerial::handleTick()
{ {
if (!gotValidated && doValidation) if (!gotValidated && doValidation)
{ {
if (serial == NULL) return; if (serial == NULL && tcpClient == NULL) return;
if (serial->isOpen()) //if it's still false we have a problem... if ( (serial && serial->isOpen()) || (tcpClient && tcpClient->isOpen())) //if it's still false we have a problem...
{ {
qDebug() << "Comm validation failed. "; qDebug() << "Comm validation failed. ";
@@ -894,6 +939,7 @@ void GVRetSerial::handleTick()
else if (doValidation); //qDebug() << "Comm connection validated"; else if (doValidation); //qDebug() << "Comm connection validated";
} }
if (doValidation && serial && serial->isOpen()) sendCommValidation(); if (doValidation && serial && serial->isOpen()) sendCommValidation();
if (doValidation && tcpClient && tcpClient->isOpen()) sendCommValidation();
} }
+3
View File
@@ -5,6 +5,7 @@
#include <QCanBusDevice> #include <QCanBusDevice>
#include <QThread> #include <QThread>
#include <QTimer> #include <QTimer>
#include <QTcpSocket>
/*************/ /*************/
#include <QDateTime> #include <QDateTime>
@@ -63,6 +64,7 @@ private slots:
void connectionTimeout(); void connectionTimeout();
void readSerialData(); void readSerialData();
void serialError(QSerialPort::SerialPortError err); void serialError(QSerialPort::SerialPortError err);
void tcpConnected();
void handleTick(); void handleTick();
private: private:
@@ -81,6 +83,7 @@ protected:
bool isAutoRestart; bool isAutoRestart;
bool continuousTimeSync; bool continuousTimeSync;
QSerialPort *serial; QSerialPort *serial;
QTcpSocket *tcpClient;
int framesRapid; int framesRapid;
STATE rx_state; STATE rx_state;
uint32_t rx_step; uint32_t rx_step;