Setup QThread with QTimer and QSerial





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I'm trying to set my QSerial port in a separated thread so that my main program to keep it isolated from the GUI processing delay.



I have been able to start the thread and I used a QTimer to restart the ::run() method because i couldn't use a while loop.



In my main i move my object to a new thread and start my thread when a new connection is opened. When the signal started() from my threadComPortRx is emitted, I launch the timer that will make a loop in my thread



Main :



#include "validation_board.h"

Validation_Board::Validation_Board()
{
valid_comm = new ValidationCommunication();
threadComPortRx = new QThread();

valid_dataTreatment->moveToThread(threadDataTreatment);
valid_comm->moveToThread(threadComPortRx);
connect(this, SIGNAL(testsignal(QByteArray)), valid_comm, SLOT(dataSend(QByteArray)));
connect(threadComPortRx, SIGNAL(started()), valid_comm, SLOT(communicationManager()));
}


void Validation_Board::openSerialPort(QString comPort, int portSpeed)
{
valid_comm->openSerialPort("COM10", 9600);
threadComPortRx->start();
}


My class running in a separate thread looks like :



validationcommunication.h :



#ifndef VALIDATIONCOMMUNICATION_H
#define VALIDATIONCOMMUNICATION_H
#include <QQueue>
#include <QElapsedTimer>
#include <QTimer>
#include "comport.h"

class ValidationCommunication : public QThread
{
Q_OBJECT

public:
ValidationCommunication();
void run();
// QSerialPort *_validComport;
ComPort *_validComport;
//void dataReceived(QByteArray rxData);
~ValidationCommunication();
signals:
void dataReceived(QByteArray rxData);
void sendData(QByteArray data);

public slots:
void openSerialPort(QString comPort, int portSpeed);
void closeSerialPort();
bool getIsOpen();
void dataSend(QByteArray data);
void sendCommand(QByteArray txFrame);
bool readData(QByteArray *responseBuffer);
void timerCallback();
void communicationManager();

private:
bool dataToSend;
QByteArray dataTx;
QQueue<QByteArray> queue;
QTimer *timer = new QTimer(this);
};

#endif // VALIDATIONCOMMUNICATION_H


validationcommunication.c :



#include "validationcommunication.h"

ValidationCommunication::ValidationCommunication()
{
_validComport = new ComPort("COM10", 9600);
connect(timer, SIGNAL(timeout()), this, SLOT(timerCallback()));
connect(this, SIGNAL(sendData(QByteArray)), _validComport, SLOT(write(QByteArray)));
}

ValidationCommunication::~ValidationCommunication()
{
delete _validComport;
}

void ValidationCommunication::openSerialPort(QString comPort, int portSpeed)
{

_validComport = new ComPort(comPort, portSpeed);
_validComport->openSerialPort();
}

void ValidationCommunication::dataSend(QByteArray data)
{
dataToSend = true;
dataTx = data;
qCritical() << "Test";
}

void ValidationCommunication::timerCallback()
{
start();
}


void ValidationCommunication::communicationManager()
{
timer->start(1000);
}

void ValidationCommunication::run()
{
QByteArray responseBuffer, test;

qCritical() << "Loop : " << QThread::currentThread();

responseBuffer.clear();

if(dataToSend)
{
qCritical() << "Send data :" << dataTx << " from Thread :" << QThread::currentThread();
sendCommand(dataTx);
}
dataToSend = false;

// exec();
}

void ValidationCommunication::sendCommand(QByteArray txFrame)
{
emit sendData(txFrame);
}


My class ComPort is just a class to manage a QSerialPort object :



**ComPort.h :**
#ifndef COMPORT_H
#define COMPORT_H

#include <QSerialPort>
#include <QSerialPortInfo>
#include <QDebug>
#include <QThread>
#include <QMutex>
//#include <QApplication>

class ComPort : public QObject
{
Q_OBJECT

public:
enum e_Usage
{
BLE,
OTHER
};

ComPort(QString portName, qint32 baudRate, e_Usage usage = OTHER, QSerialPort::FlowControl flowControl = QSerialPort::NoFlowControl);

void closeSerialPort();
void openSerialPort();
bool clear();
QByteArray readyRead();
qint64 bytesAvailable();
QByteArray writeAndRead(QByteArray data);
bool getIsOpen();

virtual ~ComPort();
void disconnectReadyRead();
QByteArray Read(int size, int timeout);
bool waitForReadyRead(int timeout);

public slots:
void write(QByteArray data);
protected slots:

void readyReadBLE();

signals:
void dataReceivedBLE(quint8 hilen, quint8 clas, quint8 method, QByteArray data);
void dataReceived(QByteArray data);

protected:
QSerialPort *_serialPort;
QString _portName;
qint32 _baudRate;
bool _isOpen;
QMutex _mx;
e_Usage _usage;
QSerialPort::FlowControl _flowControl;
};

#endif // COMPORT_H


ComPort.c :



#include "comport.h"

ComPort::ComPort(QString portName, qint32 baudRate, e_Usage usage, QSerialPort::FlowControl flowControl)
{
_usage = usage;
_portName = portName;
_baudRate = baudRate;
_isOpen = false;
_serialPort = NULL;
_flowControl = flowControl;
}

ComPort::~ComPort()
{
if (_serialPort)
delete _serialPort;
}

void ComPort::closeSerialPort()
{
qDebug() << "CLOSE :" << _portName;
if (_serialPort && _serialPort->isOpen())
{
_serialPort->clear();
_serialPort->close();
}
_isOpen = false;
}

void ComPort::openSerialPort()
{
_serialPort = new QSerialPort(this);
_serialPort->setPortName(_portName);
_serialPort->setBaudRate(_baudRate);
_serialPort->setDataBits(QSerialPort::Data8);
_serialPort->setParity(QSerialPort::NoParity);
_serialPort->setStopBits(QSerialPort::OneStop);
_serialPort->setFlowControl(_flowControl);
if (_serialPort->open(QIODevice::ReadWrite))
{
qDebug() << "Connected :" << _portName;
_isOpen = true;
}
else
{
qWarning() << "Error while opening" << _portName <<":" << _serialPort->errorString();
closeSerialPort();
}
}


void ComPort::write(QByteArray data)
{
if (_serialPort)
{
qint64 res = _serialPort->write(data.data(), data.length());
//_serialPort->flush();
if (res <= 0)
qDebug() << _serialPort->errorString();
}
}


My problem is that when I want to send data by emmiting the signal sendData nothing happens altough it is connected to the write slot to my ComPort slot write.










share|improve this question































    1















    I'm trying to set my QSerial port in a separated thread so that my main program to keep it isolated from the GUI processing delay.



    I have been able to start the thread and I used a QTimer to restart the ::run() method because i couldn't use a while loop.



    In my main i move my object to a new thread and start my thread when a new connection is opened. When the signal started() from my threadComPortRx is emitted, I launch the timer that will make a loop in my thread



    Main :



    #include "validation_board.h"

    Validation_Board::Validation_Board()
    {
    valid_comm = new ValidationCommunication();
    threadComPortRx = new QThread();

    valid_dataTreatment->moveToThread(threadDataTreatment);
    valid_comm->moveToThread(threadComPortRx);
    connect(this, SIGNAL(testsignal(QByteArray)), valid_comm, SLOT(dataSend(QByteArray)));
    connect(threadComPortRx, SIGNAL(started()), valid_comm, SLOT(communicationManager()));
    }


    void Validation_Board::openSerialPort(QString comPort, int portSpeed)
    {
    valid_comm->openSerialPort("COM10", 9600);
    threadComPortRx->start();
    }


    My class running in a separate thread looks like :



    validationcommunication.h :



    #ifndef VALIDATIONCOMMUNICATION_H
    #define VALIDATIONCOMMUNICATION_H
    #include <QQueue>
    #include <QElapsedTimer>
    #include <QTimer>
    #include "comport.h"

    class ValidationCommunication : public QThread
    {
    Q_OBJECT

    public:
    ValidationCommunication();
    void run();
    // QSerialPort *_validComport;
    ComPort *_validComport;
    //void dataReceived(QByteArray rxData);
    ~ValidationCommunication();
    signals:
    void dataReceived(QByteArray rxData);
    void sendData(QByteArray data);

    public slots:
    void openSerialPort(QString comPort, int portSpeed);
    void closeSerialPort();
    bool getIsOpen();
    void dataSend(QByteArray data);
    void sendCommand(QByteArray txFrame);
    bool readData(QByteArray *responseBuffer);
    void timerCallback();
    void communicationManager();

    private:
    bool dataToSend;
    QByteArray dataTx;
    QQueue<QByteArray> queue;
    QTimer *timer = new QTimer(this);
    };

    #endif // VALIDATIONCOMMUNICATION_H


    validationcommunication.c :



    #include "validationcommunication.h"

    ValidationCommunication::ValidationCommunication()
    {
    _validComport = new ComPort("COM10", 9600);
    connect(timer, SIGNAL(timeout()), this, SLOT(timerCallback()));
    connect(this, SIGNAL(sendData(QByteArray)), _validComport, SLOT(write(QByteArray)));
    }

    ValidationCommunication::~ValidationCommunication()
    {
    delete _validComport;
    }

    void ValidationCommunication::openSerialPort(QString comPort, int portSpeed)
    {

    _validComport = new ComPort(comPort, portSpeed);
    _validComport->openSerialPort();
    }

    void ValidationCommunication::dataSend(QByteArray data)
    {
    dataToSend = true;
    dataTx = data;
    qCritical() << "Test";
    }

    void ValidationCommunication::timerCallback()
    {
    start();
    }


    void ValidationCommunication::communicationManager()
    {
    timer->start(1000);
    }

    void ValidationCommunication::run()
    {
    QByteArray responseBuffer, test;

    qCritical() << "Loop : " << QThread::currentThread();

    responseBuffer.clear();

    if(dataToSend)
    {
    qCritical() << "Send data :" << dataTx << " from Thread :" << QThread::currentThread();
    sendCommand(dataTx);
    }
    dataToSend = false;

    // exec();
    }

    void ValidationCommunication::sendCommand(QByteArray txFrame)
    {
    emit sendData(txFrame);
    }


    My class ComPort is just a class to manage a QSerialPort object :



    **ComPort.h :**
    #ifndef COMPORT_H
    #define COMPORT_H

    #include <QSerialPort>
    #include <QSerialPortInfo>
    #include <QDebug>
    #include <QThread>
    #include <QMutex>
    //#include <QApplication>

    class ComPort : public QObject
    {
    Q_OBJECT

    public:
    enum e_Usage
    {
    BLE,
    OTHER
    };

    ComPort(QString portName, qint32 baudRate, e_Usage usage = OTHER, QSerialPort::FlowControl flowControl = QSerialPort::NoFlowControl);

    void closeSerialPort();
    void openSerialPort();
    bool clear();
    QByteArray readyRead();
    qint64 bytesAvailable();
    QByteArray writeAndRead(QByteArray data);
    bool getIsOpen();

    virtual ~ComPort();
    void disconnectReadyRead();
    QByteArray Read(int size, int timeout);
    bool waitForReadyRead(int timeout);

    public slots:
    void write(QByteArray data);
    protected slots:

    void readyReadBLE();

    signals:
    void dataReceivedBLE(quint8 hilen, quint8 clas, quint8 method, QByteArray data);
    void dataReceived(QByteArray data);

    protected:
    QSerialPort *_serialPort;
    QString _portName;
    qint32 _baudRate;
    bool _isOpen;
    QMutex _mx;
    e_Usage _usage;
    QSerialPort::FlowControl _flowControl;
    };

    #endif // COMPORT_H


    ComPort.c :



    #include "comport.h"

    ComPort::ComPort(QString portName, qint32 baudRate, e_Usage usage, QSerialPort::FlowControl flowControl)
    {
    _usage = usage;
    _portName = portName;
    _baudRate = baudRate;
    _isOpen = false;
    _serialPort = NULL;
    _flowControl = flowControl;
    }

    ComPort::~ComPort()
    {
    if (_serialPort)
    delete _serialPort;
    }

    void ComPort::closeSerialPort()
    {
    qDebug() << "CLOSE :" << _portName;
    if (_serialPort && _serialPort->isOpen())
    {
    _serialPort->clear();
    _serialPort->close();
    }
    _isOpen = false;
    }

    void ComPort::openSerialPort()
    {
    _serialPort = new QSerialPort(this);
    _serialPort->setPortName(_portName);
    _serialPort->setBaudRate(_baudRate);
    _serialPort->setDataBits(QSerialPort::Data8);
    _serialPort->setParity(QSerialPort::NoParity);
    _serialPort->setStopBits(QSerialPort::OneStop);
    _serialPort->setFlowControl(_flowControl);
    if (_serialPort->open(QIODevice::ReadWrite))
    {
    qDebug() << "Connected :" << _portName;
    _isOpen = true;
    }
    else
    {
    qWarning() << "Error while opening" << _portName <<":" << _serialPort->errorString();
    closeSerialPort();
    }
    }


    void ComPort::write(QByteArray data)
    {
    if (_serialPort)
    {
    qint64 res = _serialPort->write(data.data(), data.length());
    //_serialPort->flush();
    if (res <= 0)
    qDebug() << _serialPort->errorString();
    }
    }


    My problem is that when I want to send data by emmiting the signal sendData nothing happens altough it is connected to the write slot to my ComPort slot write.










    share|improve this question



























      1












      1








      1








      I'm trying to set my QSerial port in a separated thread so that my main program to keep it isolated from the GUI processing delay.



      I have been able to start the thread and I used a QTimer to restart the ::run() method because i couldn't use a while loop.



      In my main i move my object to a new thread and start my thread when a new connection is opened. When the signal started() from my threadComPortRx is emitted, I launch the timer that will make a loop in my thread



      Main :



      #include "validation_board.h"

      Validation_Board::Validation_Board()
      {
      valid_comm = new ValidationCommunication();
      threadComPortRx = new QThread();

      valid_dataTreatment->moveToThread(threadDataTreatment);
      valid_comm->moveToThread(threadComPortRx);
      connect(this, SIGNAL(testsignal(QByteArray)), valid_comm, SLOT(dataSend(QByteArray)));
      connect(threadComPortRx, SIGNAL(started()), valid_comm, SLOT(communicationManager()));
      }


      void Validation_Board::openSerialPort(QString comPort, int portSpeed)
      {
      valid_comm->openSerialPort("COM10", 9600);
      threadComPortRx->start();
      }


      My class running in a separate thread looks like :



      validationcommunication.h :



      #ifndef VALIDATIONCOMMUNICATION_H
      #define VALIDATIONCOMMUNICATION_H
      #include <QQueue>
      #include <QElapsedTimer>
      #include <QTimer>
      #include "comport.h"

      class ValidationCommunication : public QThread
      {
      Q_OBJECT

      public:
      ValidationCommunication();
      void run();
      // QSerialPort *_validComport;
      ComPort *_validComport;
      //void dataReceived(QByteArray rxData);
      ~ValidationCommunication();
      signals:
      void dataReceived(QByteArray rxData);
      void sendData(QByteArray data);

      public slots:
      void openSerialPort(QString comPort, int portSpeed);
      void closeSerialPort();
      bool getIsOpen();
      void dataSend(QByteArray data);
      void sendCommand(QByteArray txFrame);
      bool readData(QByteArray *responseBuffer);
      void timerCallback();
      void communicationManager();

      private:
      bool dataToSend;
      QByteArray dataTx;
      QQueue<QByteArray> queue;
      QTimer *timer = new QTimer(this);
      };

      #endif // VALIDATIONCOMMUNICATION_H


      validationcommunication.c :



      #include "validationcommunication.h"

      ValidationCommunication::ValidationCommunication()
      {
      _validComport = new ComPort("COM10", 9600);
      connect(timer, SIGNAL(timeout()), this, SLOT(timerCallback()));
      connect(this, SIGNAL(sendData(QByteArray)), _validComport, SLOT(write(QByteArray)));
      }

      ValidationCommunication::~ValidationCommunication()
      {
      delete _validComport;
      }

      void ValidationCommunication::openSerialPort(QString comPort, int portSpeed)
      {

      _validComport = new ComPort(comPort, portSpeed);
      _validComport->openSerialPort();
      }

      void ValidationCommunication::dataSend(QByteArray data)
      {
      dataToSend = true;
      dataTx = data;
      qCritical() << "Test";
      }

      void ValidationCommunication::timerCallback()
      {
      start();
      }


      void ValidationCommunication::communicationManager()
      {
      timer->start(1000);
      }

      void ValidationCommunication::run()
      {
      QByteArray responseBuffer, test;

      qCritical() << "Loop : " << QThread::currentThread();

      responseBuffer.clear();

      if(dataToSend)
      {
      qCritical() << "Send data :" << dataTx << " from Thread :" << QThread::currentThread();
      sendCommand(dataTx);
      }
      dataToSend = false;

      // exec();
      }

      void ValidationCommunication::sendCommand(QByteArray txFrame)
      {
      emit sendData(txFrame);
      }


      My class ComPort is just a class to manage a QSerialPort object :



      **ComPort.h :**
      #ifndef COMPORT_H
      #define COMPORT_H

      #include <QSerialPort>
      #include <QSerialPortInfo>
      #include <QDebug>
      #include <QThread>
      #include <QMutex>
      //#include <QApplication>

      class ComPort : public QObject
      {
      Q_OBJECT

      public:
      enum e_Usage
      {
      BLE,
      OTHER
      };

      ComPort(QString portName, qint32 baudRate, e_Usage usage = OTHER, QSerialPort::FlowControl flowControl = QSerialPort::NoFlowControl);

      void closeSerialPort();
      void openSerialPort();
      bool clear();
      QByteArray readyRead();
      qint64 bytesAvailable();
      QByteArray writeAndRead(QByteArray data);
      bool getIsOpen();

      virtual ~ComPort();
      void disconnectReadyRead();
      QByteArray Read(int size, int timeout);
      bool waitForReadyRead(int timeout);

      public slots:
      void write(QByteArray data);
      protected slots:

      void readyReadBLE();

      signals:
      void dataReceivedBLE(quint8 hilen, quint8 clas, quint8 method, QByteArray data);
      void dataReceived(QByteArray data);

      protected:
      QSerialPort *_serialPort;
      QString _portName;
      qint32 _baudRate;
      bool _isOpen;
      QMutex _mx;
      e_Usage _usage;
      QSerialPort::FlowControl _flowControl;
      };

      #endif // COMPORT_H


      ComPort.c :



      #include "comport.h"

      ComPort::ComPort(QString portName, qint32 baudRate, e_Usage usage, QSerialPort::FlowControl flowControl)
      {
      _usage = usage;
      _portName = portName;
      _baudRate = baudRate;
      _isOpen = false;
      _serialPort = NULL;
      _flowControl = flowControl;
      }

      ComPort::~ComPort()
      {
      if (_serialPort)
      delete _serialPort;
      }

      void ComPort::closeSerialPort()
      {
      qDebug() << "CLOSE :" << _portName;
      if (_serialPort && _serialPort->isOpen())
      {
      _serialPort->clear();
      _serialPort->close();
      }
      _isOpen = false;
      }

      void ComPort::openSerialPort()
      {
      _serialPort = new QSerialPort(this);
      _serialPort->setPortName(_portName);
      _serialPort->setBaudRate(_baudRate);
      _serialPort->setDataBits(QSerialPort::Data8);
      _serialPort->setParity(QSerialPort::NoParity);
      _serialPort->setStopBits(QSerialPort::OneStop);
      _serialPort->setFlowControl(_flowControl);
      if (_serialPort->open(QIODevice::ReadWrite))
      {
      qDebug() << "Connected :" << _portName;
      _isOpen = true;
      }
      else
      {
      qWarning() << "Error while opening" << _portName <<":" << _serialPort->errorString();
      closeSerialPort();
      }
      }


      void ComPort::write(QByteArray data)
      {
      if (_serialPort)
      {
      qint64 res = _serialPort->write(data.data(), data.length());
      //_serialPort->flush();
      if (res <= 0)
      qDebug() << _serialPort->errorString();
      }
      }


      My problem is that when I want to send data by emmiting the signal sendData nothing happens altough it is connected to the write slot to my ComPort slot write.










      share|improve this question
















      I'm trying to set my QSerial port in a separated thread so that my main program to keep it isolated from the GUI processing delay.



      I have been able to start the thread and I used a QTimer to restart the ::run() method because i couldn't use a while loop.



      In my main i move my object to a new thread and start my thread when a new connection is opened. When the signal started() from my threadComPortRx is emitted, I launch the timer that will make a loop in my thread



      Main :



      #include "validation_board.h"

      Validation_Board::Validation_Board()
      {
      valid_comm = new ValidationCommunication();
      threadComPortRx = new QThread();

      valid_dataTreatment->moveToThread(threadDataTreatment);
      valid_comm->moveToThread(threadComPortRx);
      connect(this, SIGNAL(testsignal(QByteArray)), valid_comm, SLOT(dataSend(QByteArray)));
      connect(threadComPortRx, SIGNAL(started()), valid_comm, SLOT(communicationManager()));
      }


      void Validation_Board::openSerialPort(QString comPort, int portSpeed)
      {
      valid_comm->openSerialPort("COM10", 9600);
      threadComPortRx->start();
      }


      My class running in a separate thread looks like :



      validationcommunication.h :



      #ifndef VALIDATIONCOMMUNICATION_H
      #define VALIDATIONCOMMUNICATION_H
      #include <QQueue>
      #include <QElapsedTimer>
      #include <QTimer>
      #include "comport.h"

      class ValidationCommunication : public QThread
      {
      Q_OBJECT

      public:
      ValidationCommunication();
      void run();
      // QSerialPort *_validComport;
      ComPort *_validComport;
      //void dataReceived(QByteArray rxData);
      ~ValidationCommunication();
      signals:
      void dataReceived(QByteArray rxData);
      void sendData(QByteArray data);

      public slots:
      void openSerialPort(QString comPort, int portSpeed);
      void closeSerialPort();
      bool getIsOpen();
      void dataSend(QByteArray data);
      void sendCommand(QByteArray txFrame);
      bool readData(QByteArray *responseBuffer);
      void timerCallback();
      void communicationManager();

      private:
      bool dataToSend;
      QByteArray dataTx;
      QQueue<QByteArray> queue;
      QTimer *timer = new QTimer(this);
      };

      #endif // VALIDATIONCOMMUNICATION_H


      validationcommunication.c :



      #include "validationcommunication.h"

      ValidationCommunication::ValidationCommunication()
      {
      _validComport = new ComPort("COM10", 9600);
      connect(timer, SIGNAL(timeout()), this, SLOT(timerCallback()));
      connect(this, SIGNAL(sendData(QByteArray)), _validComport, SLOT(write(QByteArray)));
      }

      ValidationCommunication::~ValidationCommunication()
      {
      delete _validComport;
      }

      void ValidationCommunication::openSerialPort(QString comPort, int portSpeed)
      {

      _validComport = new ComPort(comPort, portSpeed);
      _validComport->openSerialPort();
      }

      void ValidationCommunication::dataSend(QByteArray data)
      {
      dataToSend = true;
      dataTx = data;
      qCritical() << "Test";
      }

      void ValidationCommunication::timerCallback()
      {
      start();
      }


      void ValidationCommunication::communicationManager()
      {
      timer->start(1000);
      }

      void ValidationCommunication::run()
      {
      QByteArray responseBuffer, test;

      qCritical() << "Loop : " << QThread::currentThread();

      responseBuffer.clear();

      if(dataToSend)
      {
      qCritical() << "Send data :" << dataTx << " from Thread :" << QThread::currentThread();
      sendCommand(dataTx);
      }
      dataToSend = false;

      // exec();
      }

      void ValidationCommunication::sendCommand(QByteArray txFrame)
      {
      emit sendData(txFrame);
      }


      My class ComPort is just a class to manage a QSerialPort object :



      **ComPort.h :**
      #ifndef COMPORT_H
      #define COMPORT_H

      #include <QSerialPort>
      #include <QSerialPortInfo>
      #include <QDebug>
      #include <QThread>
      #include <QMutex>
      //#include <QApplication>

      class ComPort : public QObject
      {
      Q_OBJECT

      public:
      enum e_Usage
      {
      BLE,
      OTHER
      };

      ComPort(QString portName, qint32 baudRate, e_Usage usage = OTHER, QSerialPort::FlowControl flowControl = QSerialPort::NoFlowControl);

      void closeSerialPort();
      void openSerialPort();
      bool clear();
      QByteArray readyRead();
      qint64 bytesAvailable();
      QByteArray writeAndRead(QByteArray data);
      bool getIsOpen();

      virtual ~ComPort();
      void disconnectReadyRead();
      QByteArray Read(int size, int timeout);
      bool waitForReadyRead(int timeout);

      public slots:
      void write(QByteArray data);
      protected slots:

      void readyReadBLE();

      signals:
      void dataReceivedBLE(quint8 hilen, quint8 clas, quint8 method, QByteArray data);
      void dataReceived(QByteArray data);

      protected:
      QSerialPort *_serialPort;
      QString _portName;
      qint32 _baudRate;
      bool _isOpen;
      QMutex _mx;
      e_Usage _usage;
      QSerialPort::FlowControl _flowControl;
      };

      #endif // COMPORT_H


      ComPort.c :



      #include "comport.h"

      ComPort::ComPort(QString portName, qint32 baudRate, e_Usage usage, QSerialPort::FlowControl flowControl)
      {
      _usage = usage;
      _portName = portName;
      _baudRate = baudRate;
      _isOpen = false;
      _serialPort = NULL;
      _flowControl = flowControl;
      }

      ComPort::~ComPort()
      {
      if (_serialPort)
      delete _serialPort;
      }

      void ComPort::closeSerialPort()
      {
      qDebug() << "CLOSE :" << _portName;
      if (_serialPort && _serialPort->isOpen())
      {
      _serialPort->clear();
      _serialPort->close();
      }
      _isOpen = false;
      }

      void ComPort::openSerialPort()
      {
      _serialPort = new QSerialPort(this);
      _serialPort->setPortName(_portName);
      _serialPort->setBaudRate(_baudRate);
      _serialPort->setDataBits(QSerialPort::Data8);
      _serialPort->setParity(QSerialPort::NoParity);
      _serialPort->setStopBits(QSerialPort::OneStop);
      _serialPort->setFlowControl(_flowControl);
      if (_serialPort->open(QIODevice::ReadWrite))
      {
      qDebug() << "Connected :" << _portName;
      _isOpen = true;
      }
      else
      {
      qWarning() << "Error while opening" << _portName <<":" << _serialPort->errorString();
      closeSerialPort();
      }
      }


      void ComPort::write(QByteArray data)
      {
      if (_serialPort)
      {
      qint64 res = _serialPort->write(data.data(), data.length());
      //_serialPort->flush();
      if (res <= 0)
      qDebug() << _serialPort->errorString();
      }
      }


      My problem is that when I want to send data by emmiting the signal sendData nothing happens altough it is connected to the write slot to my ComPort slot write.







      multithreading timer serial-port qthread






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 18 '18 at 16:41







      Steven

















      asked Nov 16 '18 at 15:47









      StevenSteven

      163




      163
























          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341179%2fsetup-qthread-with-qtimer-and-qserial%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341179%2fsetup-qthread-with-qtimer-and-qserial%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bressuire

          Vorschmack

          Quarantine