我有一个带有串行接口的类,当接收到一些有效数据时,该接口可以传输一些数据和emit
的{{1}}信号:
Serial.c
MessageReceived
我有我的主窗口,该窗口打开串行连接并开始消息事务。但是我必须接收大量数据,并且传输需要一段时间,因此我决定使用void Serial::on_serialPort_readData()
{
... Other stuff...
else
{
qDebug() << "Received in serial";
emit MessageReceived(_mReceivedData);
}
}
}
来处理接收到的数据。
我在QThread
(来自here的代码)中创建了一个线程和一个工作线程:
mainwindow.cpp
mainwindow.cpp
串行接口将以不同的包发送数据,我读到here,在单独的线程中使用串行端口是一个坏主意,因此我决定在主线程中使用串行端口,将_mThread = new QThread(this);
_mThreadWorker = new Worker();
_mThreadWorker->moveToThread(_mThread);
// Like in the example
connect(_mThread, &QThread::started, _mThreadWorker, &Worker::Progress);
connect(_mThreadWorker, &Worker::Finished, _mThread, &QThread::quit);
connect(_mThreadWorker, &Worker::Finished, _mThreadWorker, &Worker::deleteLater);
connect(_mThread, &QThread::finished, _mThread, &QObject::deleteLater);
connect(_mProtocolInterface, &Serial::MessageReceived, _mThreadWorker, &Worker::UpdateData);
_mThread->start();
// Send the first packet so the receiver will answer
信号与我的工作人员的MessageReceived
插槽连接:
worker.h
UpdateData
worker.cpp
class Worker : public QObject
{
Q_OBJECT
...
public slots:
void Progress(void);
void UpdateData(QByteArray Data);
public:
Worker(QObject* parent = nullptr);
~Worker();
...
};
但是从接收器接收到数据后,只有插槽void Worker::UpdateData(QByteArray Data)
{
qDebug() << "Received in Worker";
_mData = Data;
}
void Worker::Progress(void)
{
while(_mActive)
{
switch(_mState)
{
case STATE_INIT:
{
_mState = STATE_INIT;
// Just a idle to simulate the data transfer during debugging
break;
}
}
}
}
被调用:
on_serialPort_readData
那么为什么没有调用类>> Received in serial
的插槽UpdateData
?