views:

323

answers:

1

Hi all,

I asked this question yesterday since I wasn't receiving any data but strangely when I used wait in the destructor I started receveing notification from QSocketNotifier. The rest of the question is same. Can someone suggest something? I have created a sample application from where separate thread is started to read and process data from serial port. QSocketNotifier is used for detecting whether data has arrived on the serial ports or not. I start an event loop using exec() statement in run function of thread. But while running the application only once the socket notifier has worked, the signal for serial port activation is never generated. And once when it was generated it was generated very fast and wasn't equivalent to the frame rate of sending device.

Here is a brief code sample for serial communicator thread:

SerialPortWatchOne.cpp

//constructor
klass::klass()
{
//setport configuration
//miscellaneous initialization
QSocketNotifier* notifier = new QSocketNotifier(mPort->GetFileDescriptor, QSocketNotifier::Read,this);
connect(notifier,SIGNAL(activated(int)),this,SLOT(ReadAndProcessData()));
}

void klass::run()
{
exec();  //this starts an event loop where by Qt signal handling is enabled
}

void klass::ReadAndProcessData()
{
      FlushBuf();      
      int bytes_read=mPort->ReadPort(mBuf,1000);
      if(bytes_read>0)
         //Process data
}

~klass::klass()
{
//desctruction code;
wait();  //so that thread cleanly releases all resources before exit
}

Note: klass is a thread and a member of GUI thread and is instantiated in the GUI thread c'tor. periodically GUI updates its widgets with data from klass thread.

Can anyone suggest as what the issue is? Has someone done this before.

A: 

Only thing that comes to mind is that you're using QSocketNotifier * socketNotifier without an event loop running in the socketNotifier->thread() thread. If no event loop is running, you'll get no event handling. No event handling means no signals fired from QSocketNotifier. But if you're using a separate thread, you might want to use blocking I/O calls anyway.