From 07c6046b2c3a85bd41a0eec37e1b7d3dbac9aa66 Mon Sep 17 00:00:00 2001 From: Collin Kidder Date: Wed, 20 May 2015 19:22:30 -0400 Subject: [PATCH] SerialWorker now asks for device info from GVRET. Also, fixed a bug. Lastly, the connection attempt will now time out after 1 second. --- serialworker.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++-- serialworker.h | 8 +++++++- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/serialworker.cpp b/serialworker.cpp index eef3136..61d768e 100644 --- a/serialworker.cpp +++ b/serialworker.cpp @@ -2,6 +2,7 @@ #include #include +#include SerialWorker::SerialWorker(QObject *parent) : QObject(parent) { @@ -53,9 +54,23 @@ void SerialWorker::setSerialPort(QSerialPortInfo *port) output.append(0xE7); output.append(0xF1); //signal we want to issue a command output.append(0x06); //request canbus stats from the board + output.append(0xF1); //another command to the GVRET + output.append(0x07); //request device information serial->write(output); - ///isConnected = true; + connected = false; connect(serial, SIGNAL(readyRead()), this, SLOT(readSerialData())); + QTimer::singleShot(1000, this, SLOT(connectionTimeout())); +} + +void SerialWorker::connectionTimeout() +{ + //one second after trying to connect are we actually connected? + if (!connected) //no? + { + //then emit the the failure signal and see if anyone cares + qDebug() << "Failed to connect to GVRET at that com port"; + emit connectionFailure(); + } } void SerialWorker::readSerialData() @@ -154,6 +169,11 @@ void SerialWorker::procRXChar(unsigned char c) case 6: //get canbus parameters from GVRET rx_state = GET_CANBUS_PARAMS; rx_step = 0; + break; + case 7: //get device info + rx_state = GET_DEVICE_INFO; + rx_step = 0; + break; } break; case BUILD_CAN_FRAME: @@ -262,15 +282,41 @@ void SerialWorker::procRXChar(unsigned char c) break; case 9: can1Baud |= c << 24; - rx_step = IDLE; + rx_state = IDLE; qDebug() << "Baud 0 = " << can0Baud; qDebug() << "Baud 1 = " << can1Baud; if (!can1Enabled) can1Baud = 0; if (!can0Enabled) can0Baud = 0; + connected = true; emit connectionSuccess(can0Baud, can1Baud); break; } rx_step++; break; + case GET_DEVICE_INFO: + switch (rx_step) + { + case 0: + deviceBuildNum = c; + break; + case 1: + deviceBuildNum |= c << 8; + break; + case 2: + break; //don't care about eeprom version + case 3: + break; //don't care about file type + case 4: + break; //don't care about whether it auto logs or not + case 5: + deviceSingleWireMode = c; + rx_state = IDLE; + qDebug() << "build num: " << deviceBuildNum; + qDebug() << "single wire can: " << deviceSingleWireMode; + emit deviceInfo(deviceBuildNum, deviceSingleWireMode); + break; + } + rx_step++; + break; } } diff --git a/serialworker.h b/serialworker.h index de09f4e..607f995 100644 --- a/serialworker.h +++ b/serialworker.h @@ -16,7 +16,8 @@ enum STATE //keep this enum synchronized with the Arduino firmware project GET_ANALOG_INPUTS, SET_DIG_OUTPUTS, SETUP_CANBUS, - GET_CANBUS_PARAMS + GET_CANBUS_PARAMS, + GET_DEVICE_INFO }; class SerialWorker : public QObject @@ -32,9 +33,11 @@ signals: //we emit signals void receivedFrame(CANFrame *); void connectionSuccess(int, int); void connectionFailure(); + void deviceInfo(int, int); private slots: //we receive things in slots void readSerialData(); + void connectionTimeout(); public slots: void setSerialPort(QSerialPortInfo*); @@ -44,12 +47,15 @@ public slots: private: QString portName; bool quit; + bool connected; QSerialPort *serial; STATE rx_state; int rx_step; CANFrame *buildFrame; int can0Baud, can1Baud; bool can0Enabled, can1Enabled; + int deviceBuildNum; + int deviceSingleWireMode; void procRXChar(unsigned char); };