How can I send an unsigned char buf[10]
array from a worker function to a gui thread(main thread).
I was trying to create a signal/slot mechanism where the slot has the buf in the parameters of the function to process it in the gui thread.
UPDATE:
this is what I have so far:
worker class:
class Worker : public QObject
{
Q_OBJECT
public:
Worker(FILE *datafile, int sockint, int bitsint);
~Worker();
int channels_buf[10];
FILE *data;
int sock;
int bits;
public slots:
void doWork();
signals:
void finished();
void newinfo(unsigned char buf[10]);
private:
};
the worker constructer
// Worker thread
Worker::Worker(FILE *datafile, int sockint, int bitsint)
:data(datafile)
,sock(sockint)
,bits(bitsint)
{
}
Worker::~Worker()
{
}
the function of the thread
// Worker functions
void Worker::doWork()
{
unsigned char buf[10];
unsigned char crcval;
memset (buf, 0, 10);
while(1)
{
int i;
int numb;
numb = 0;
numb = recv (sock, buf, 10, MSG_WAITALL);
crcval = BP_CRC8 (buf, 9);
// 8 bits
if (bits == 0)
{
if (crcval == buf[9])
{
emit newinfo(buf);
}
}
}
emit finished();
}
then I start the thread
Worker *worker;
QThread *workerThread;
worker = new Worker(data, sock, bits);
workerThread = new QThread(this);
connect(workerThread, SIGNAL(started()), worker, SLOT(doWork()));
connect(workerThread, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(worker, SIGNAL(newinfo(unsigned char[])), this, SLOT(process_new_info(unsigned char[])));
worker->moveToThread(workerThread);
workerThread->start();
this is the function in the gui thread which is supposed to process the unsigned char buf[10]
from the worker.
void gui::process_new_info(unsigned char buf[10])
{
int v = 0;
printf ("%d ->", buf[0]);
fprintf (data, "%d,", buf[0]);
for (int i = 1; i < 9; i++)
{
v = buf[i];
printf ("%d,", v);
fprintf (data, "%d,", v);
}
printf ("\n");
fprintf (data, "\n");
}