Commit b7b26912 authored by oberion's avatar oberion

Serial commmunication bug fix

port has to be closed and deleted in the same thread as it was created. changed the disconnect function
parent aef6f4ee
......@@ -27,7 +27,7 @@ using namespace TNX;
SerialLink::SerialLink(QString portname, int baudRate, bool hardwareFlowControl, bool parity,
int dataBits, int stopBits) :
port(NULL)
port(NULL), m_stopp(false)
{
// Setup settings
this->porthandle = portname.trimmed();
......@@ -121,6 +121,14 @@ void SerialLink::run()
// Qt way to make clear what a while(1) loop does
forever {
{
QMutexLocker locker(&this->m_stoppMutex);
if(this->m_stopp)
{
this->m_stopp = false;
break;
}
}
// Check if new bytes have arrived, if yes, emit the notification signal
checkForBytes();
/* Serial data isn't arriving that fast normally, this saves the thread
......@@ -128,6 +136,13 @@ void SerialLink::run()
*/
MG::SLEEP::msleep(SerialLink::poll_interval);
}
if (port) {
port->flushInBuffer();
port->flushOutBuffer();
port->close();
delete port;
port = NULL;
}
}
......@@ -234,27 +249,36 @@ qint64 SerialLink::bytesAvailable()
**/
bool SerialLink::disconnect()
{
if (port) {
if(this->isRunning())
{
{
QMutexLocker locker(&this->m_stoppMutex);
this->m_stopp = true;
}
this->wait();
// if (port) {
//#if !defined _WIN32 || !defined _WIN64
/* Block the thread until it returns from run() */
//#endif
// dataMutex.lock();
port->flushInBuffer();
port->flushOutBuffer();
port->close();
delete port;
port = NULL;
// port->flushInBuffer();
// port->flushOutBuffer();
// port->close();
// delete port;
// port = NULL;
// dataMutex.unlock();
if(this->isRunning()) this->terminate(); //stop running the thread, restart it upon connect
// if(this->isRunning()) this->terminate(); //stop running the thread, restart it upon connect
bool closed = true;
//port->isOpen();
emit disconnected();
emit connected(false);
return closed;
} else {
}
else {
// No port, so we're disconnected
return true;
}
......@@ -269,6 +293,10 @@ bool SerialLink::disconnect()
bool SerialLink::connect()
{
if (this->isRunning()) this->disconnect();
{
QMutexLocker locker(&this->m_stoppMutex);
this->m_stopp = false;
}
this->start(LowPriority);
return true;
}
......
......@@ -164,6 +164,10 @@ protected:
QMutex statisticsMutex;
QMutex dataMutex;
private:
volatile bool m_stopp;
QMutex m_stoppMutex;
void setName(QString name);
bool hardwareConnect();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment