我试图在单独的线程中设置QSerial端口,以便我的主程序将其与GUI处理延迟隔离开。
我已经能够启动线程,并且我使用QTimer重新启动:: run()方法,因为我无法使用while循环。
在我的主机中,我将对象移动到新线程并在打开新连接时启动线程。从我的threadComPortRx发出信号started()时,我启动计时器,该计时器将在我的线程中循环
主要:
#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();
}
我在单独线程中运行的类如下:
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);
}
我的类ComPort只是管理QSerialPort对象的类:
**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();
}
}
我的问题是,当我想通过发出信号sendData来发送数据时,什么也没有发生,尽管它已连接到ComPort插槽写操作的写插槽。