// thread to receive data
CConnectionThread
::CConnectionThread(QObject *parent
){
}
void CConnectionThread::initConnections()
{
m_DataHandler = new CDataHandler(0);
m_DataHandler->initConnections();
}
void CConnectionThread::run()
{
initConnections();
exec();
}
void CDataHandler::initConnections()
{
m_Connection->connectToHost(ipAddress, portNumber);
m_Connection->waitForConnected(2000);
bool ret = connect(m_Connection, SIGNAL(readyRead()), this, SLOT(dataAvailable()));
}
/// data handler class to parse the data from the port and queue up for processing
void CDataHandler::dataAvailable()
{
/// read the length of response
/// read the complete response
processResponse(response);
delete(response);
}
void CDataHandler
::processResponse(QByteArray* buffer
) {
// get response id
switch(responseId)
{
case IMAGE_DATA:
addToImageQueue(buffer); /// queue of QByteArray* that can be accessible to Both UI and worker thread
break;
case NUMERIC_DATA:
addToNumericQueue(buffer);
emit newDataArrived();
break;
}
}
// thread to receive data
CConnectionThread::CConnectionThread(QObject *parent)
: QThread(parent)
{
}
void CConnectionThread::initConnections()
{
m_DataHandler = new CDataHandler(0);
m_DataHandler->initConnections();
}
void CConnectionThread::run()
{
initConnections();
exec();
}
void CDataHandler::initConnections()
{
m_Connection= new QTcpSocket();
m_Connection->connectToHost(ipAddress, portNumber);
m_Connection->waitForConnected(2000);
bool ret = connect(m_Connection, SIGNAL(readyRead()), this, SLOT(dataAvailable()));
}
/// data handler class to parse the data from the port and queue up for processing
void CDataHandler::dataAvailable()
{
QByteArray* response;
/// read the length of response
/// read the complete response
processResponse(response);
delete(response);
}
void CDataHandler::processResponse(QByteArray* buffer)
{
// get response id
switch(responseId)
{
case IMAGE_DATA:
addToImageQueue(buffer); /// queue of QByteArray* that can be accessible to Both UI and worker thread
break;
case NUMERIC_DATA:
addToNumericQueue(buffer);
emit newDataArrived();
break;
}
}
To copy to clipboard, switch view to plain text mode
The worker thread is to read the data from the tcp socket, it should not get affected by any of the UI events. Ideally tcp server should not get slow down.
Tcp server sends image data for every 1 ms.
Bookmarks