Skip to content

Commit

Permalink
FreeEmsPlugin: Fix for Serial Comms not detecting USB unplug
Browse files Browse the repository at this point in the history
SerialRXThread will now poll the serial ports on a system every
1.5 seconds, and notify the user if the currently connected
port has disappeared. This will allow the user to save off
their tune and reconnect, then reload the tune to avoid any
data loss
  • Loading branch information
malcom2073 committed May 13, 2014
1 parent e24696f commit 192346b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions plugins/freeems/freeemscomms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,7 @@ void FreeEmsComms::run()
rxThread = new SerialRXThread();
connect(rxThread,SIGNAL(incomingPacket(QByteArray)),this,SLOT(parseEverything(QByteArray)),Qt::DirectConnection);
connect(rxThread,SIGNAL(dataRead(QByteArray)),this,SLOT(dataLogRead(QByteArray)),Qt::DirectConnection);
connect(rxThread,SIGNAL(portGone()),this,SLOT(rxThreadPortGone()),Qt::DirectConnection);

//Before we finish emitting the fact that we are connected, let's verify this is a freeems system we are talking to.
if (!sendPacket(GET_FIRMWARE_VERSION))
Expand Down Expand Up @@ -1301,6 +1302,12 @@ void FreeEmsComms::run()
rxThread = 0;
}
}
void FreeEmsComms::rxThreadPortGone()
{
disconnectSerial();
emit error("Serial port has disappeared. Save your tune (File->Save tune) then ensure the device is still connected and powered, then reconnect");
}

void FreeEmsComms::sendNextInterrogationPacket()
{
if (m_interrogatePacketList.size() == 0)
Expand Down
1 change: 1 addition & 0 deletions plugins/freeems/freeemscomms.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ public slots:
int burnBlockFromRamToFlash(unsigned short location,unsigned short offset, unsigned short size);
private slots:
void ramBlockUpdateRec(QByteArray header,QByteArray payload);
void rxThreadPortGone();
void flashBlockUpdateRec(QByteArray header,QByteArray payload);
void packetNakedRec(unsigned short payloadid,QByteArray header,QByteArray payload,unsigned short errornum);
void packetAckedRec(unsigned short payloadid,QByteArray header,QByteArray payload);
Expand Down
1 change: 1 addition & 0 deletions plugins/freeems/serialport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ int SerialPort::openPort(QString portName,int baudrate,bool oddparity)
{
//QLOG_DEBUG() << "SerialPort::openPort thread:" << QThread::currentThread();
//m_serialPort = new QSerialPort();
m_portName = portName;
m_serialPort->setPortName(portName);
if (!m_serialPort->open(QIODevice::ReadWrite))
{
Expand Down
1 change: 1 addition & 0 deletions plugins/freeems/serialport.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class SerialPort : public QObject
int readBytes(QByteArray *array, int maxlen,int timeout=0);
int bufferSize() { return m_queuedMessages.size(); }
void setInterByteSendDelay(int milliseconds);
QString portName() { return m_portName; }
private:
QSerialPort *m_serialPort;
QByteArray m_privBuffer;
Expand Down
21 changes: 21 additions & 0 deletions plugins/freeems/serialrxthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "serialrxthread.h"
#include <QDateTime>
#include <qserialportinfo.h>
#include "QsLog.h"

SerialRXThread::SerialRXThread(QObject *parent) : QThread(parent)
Expand Down Expand Up @@ -143,6 +144,7 @@ QByteArray SerialRXThread::readSinglePacket(SerialPort *port)

void SerialRXThread::run()
{
qint64 msecs = QDateTime::currentMSecsSinceEpoch();
QString byteoutofpacket;
QByteArray qbuffer;
//unsigned char buffer[10240];
Expand All @@ -153,6 +155,25 @@ void SerialRXThread::run()
int readlen=0;
while (!m_terminate)
{
if (QDateTime::currentMSecsSinceEpoch() - msecs > 1500)
{
//Every second, check that the port still exists.
msecs = QDateTime::currentMSecsSinceEpoch();
bool found = false;
foreach(QSerialPortInfo info,QSerialPortInfo::availablePorts())
{
if (m_serialPort->portName().contains(info.portName()))
{
found = true;
}
}
if (!found)
{
emit portGone();
return;
}

}
readlen = m_serialPort->readBytes(&buffer,100);
if (readlen < 0)
{
Expand Down
1 change: 1 addition & 0 deletions plugins/freeems/serialrxthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class SerialRXThread : public QThread
signals:
void incomingPacket(QByteArray packet);
void dataRead(QByteArray data);
void portGone();
public slots:

};
Expand Down

0 comments on commit 192346b

Please sign in to comment.