4
votes

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");
}
1
How far did you get? How did it fail?Drew Dormann
I have created the worker class, which receives arguments from gui thread in the worker constructor. The worker thread is running. My problem now is extracting the data that it produces. :)SamuelNLP
Excellent. Please edit your question to show the relevant code you have written and where it is lacking.Drew Dormann
@DrewDormann I've updated it.SamuelNLP

1 Answers

3
votes

The correct way to make the connection is:

connect( worker, SIGNAL(newinfo(unsigned char[10])), this, SLOT(process_new_info(unsigned char[10])) );

However you will then run into the problem that you would be trying to make a queued connection, and Qt doesn't know how to deal with queueing that type. There are ways around that, but you will find it much easier to just put your data into a container such as QByteArray or QVector rather than a raw array.