[Qt-interest] Problem with FD_SET and select

Girish Ramakrishnan girish at forwardbias.in
Fri Sep 18 07:16:37 CEST 2009


Flavio Ceolin wrote:
> 
> On Sep 18, 2009, at 1:26 AM, Girish Ramakrishnan wrote:
> 
>> Are you aware of QSocketNotifier? All you have to do is to pass the
>> serial handle to it and you a signal when you can read from it.
>>
>> Girish
>>
>> Flavio Ceolin wrote:
>>> Hi people,
>>>
>>> I'm  trying to using serial port on an POSIX system (MACOSX) with qt.
>>> I have a a thread that read data from serial. The code is this:
>>>
>>> void ThreadCommRead::run()
>>> {
>>>    fd_set fds;
>>>    int aux=0;
>>>    FD_ZERO(&fds);
>>>    FD_SET(m_handle,&fds);
>>>    while(running){
>>>        aux = select(m_handle+1,&fds,NULL,NULL,&timer);
>>>        if(aux>0){
>>>            m_mutex->lock();
>>>            if((nbytes = read(m_handle,buffer,BUFFERSIZE))>0)
>>>            emit dataRead(buffer,nbytes);
>>>            m_mutex->unlock();
>>>        }
>>>    }
>>> }
>>>
>>> The problem is that the behavior of function FD_SET is wrong and aux
>>> never is different of zero.
>>> I did a simple app for terminal and the same code using pthreads works
>>> fine.
>>> I think that mainloop of qt application is disturbing.
>>> Someone can help me?
>>>
>>>
>>> Flavio Ceolin
>>> skype: flavio.ceolin
>>> msn: flavio.ceolin at hotmail.com
>>> phone: +55 14 32276948
>>> mobile:  +55 13 97044877
>>> flavio.ceolin at gmail.com
>>>
>>>
>>>
>>
> 
> Thanks a lot girish,
> I'm going to try use QSocketNotifier.
> I don't have to use FD_SET?

Yup, no FD_SET requried. Qt will put your fd as part of it's own
select() call in the ui thread.

Girish



More information about the Qt-interest-old mailing list