Results 1 to 20 of 20

Thread: QThread, bad file descriptor

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default QThread, bad file descriptor

    I created an instance of RS class inside MainWindow. when fd is opened, it gives the value of 19 inside debugger. But, when I access it from inside of the Thread, it always gives value 0.

    this is my main file: it initializes a thread that regularly generates packets inside queue as producer, and consumer on the other end, writes them to serial.

    serial port is opened in Mainwindow constructor:
    Qt Code:
    1. rs_plc.rs_plcOpenPort((char *)"/dev/ttyS0"); /*/dev/ttyS3*/
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. ThreadSafeQueue<QByteArray> UpdateJackQueue;
    2. PlcUpdateThread *plcUpdateProd = new PlcUpdateThread(UpdateJackQueue, 1);
    3. plcUpdateProd->start();
    4.  
    5. PlcUpdateThread *plcUpdateCons = new PlcUpdateThread(UpdateJackQueue, 0);
    6. plcUpdateCons->start();
    To copy to clipboard, switch view to plain text mode 

    here is the Thread:
    Qt Code:
    1. #ifndef PLCUPDATETHREAD_H
    2. #define PLCUPDATETHREAD_H
    3.  
    4. #include <QByteArray>
    5. #include <QThread>
    6. #include "threadsafequeue.h"
    7. #include "safilanLib.h"
    8. #include "rs485.h"
    9.  
    10. #include <QDebug>
    11.  
    12. class PlcUpdateThread : public QThread
    13. {
    14. Q_OBJECT
    15. public:
    16. PlcUpdateThread(ThreadSafeQueue<QByteArray> &q, bool isProducer) : _queue(q), _prod(isProducer) {
    17. if(_prod)
    18. qDebug() << "I am a producer";
    19. else
    20. qDebug() << "I am a consumer";
    21. }
    22.  
    23. void run() {
    24. if(_prod)
    25. while(1) produce();
    26. else
    27. while(1) consume();
    28. }
    29.  
    30.  
    31. void produce() {
    32.  
    33. const char str[]={UPDATE_xx};
    34.  
    35. QByteArray built((char*)str, 3) ;
    36.  
    37. _queue.enqueue(built);
    38.  
    39. // qDebug() << _queue.count();
    40.  
    41. msleep(100);
    42. //sleep(qrand()%5);
    43. }
    44.  
    45. void consume() {
    46. qDebug() << "cosume";
    47. QByteArray v = _queue.dequeue();
    48.  
    49. qDebug() << v.toHex();
    50. rs_plc.writeToSerialPort(v);
    51.  
    52. qDebug() << _queue.count();
    53. msleep(300);
    54. // sleep(qrand()%5);
    55. }
    56.  
    57.  
    58. public slots:
    59.  
    60. private:
    61. ThreadSafeQueue<QByteArray> &_queue;
    62. bool _prod;
    63. };
    64.  
    65. #endif // PLCUPDATETHREAD_H
    To copy to clipboard, switch view to plain text mode 

    here is the write to serial function that the above thread calls:

    and here is the RS instance created statically write after the RS class:
    Qt Code:
    1. void rs_flushPort ();
    2. bool rs_plcConfigPort();
    3. bool rs_azmthConfigPort();
    4.  
    5. };
    6.  
    7. static RS rs_plc;
    To copy to clipboard, switch view to plain text mode 
    Last edited by saman_artorious; 22nd April 2013 at 11:04.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread, bad file descriptor

    Please provide a minimal compilable example reproducing the problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QThread, bad file descriptor

    Quote Originally Posted by wysota View Post
    Please provide a minimal compilable example reproducing the problem.
    can't be more minimized than above now. I omitted serial write, forget about it.
    When fd is set in RS class instance, when I access serial class member from thread, fd is 0. However, If I open and close serial port inside thread everytime I write then it writes successfully! should I keep that way?

    this way it works fine:
    Qt Code:
    1. void consume() {
    2. qDebug() << "cosume";
    3. QByteArray v = _queue.dequeue();
    4.  
    5. qDebug() << v.toHex();
    6. rs_plc.rs_plcOpenPort((char *)"/dev/ttyS0");
    7. rs_plc.writeToSerialPort(v);
    8.  
    9. qDebug() << _queue.count();
    10. msleep(300);
    11. // sleep(qrand()%5);
    12. }
    To copy to clipboard, switch view to plain text mode 

    moreover, the other problem is with reading. inside another thread, I read serial by producer and update ui by consumer, but producer never reads from serial, even when I send a packet via DB9 ,it does not catch it. here is the reading and updating ui thread:
    Qt Code:
    1. void produce() {
    2.  
    3. qDebug() << "produce";
    4.  
    5. QByteArray rd15Bytes;
    6.  
    7. if(rs_plc.rs_plcRead(&rd15Bytes))
    8. {
    9. if(!rd15Bytes.isEmpty())
    10. {
    11. // qDebug() << "Enqueue";
    12. _queue.enqueue(rd15Bytes);
    13. }
    14. }
    15.  
    16. msleep(100);
    17. }
    18.  
    19. void consume() {
    20. qDebug() << "cosume";
    21.  
    22. QByteArray v = _queue.dequeue();
    23.  
    24. rs_plc.updateUI(v);
    25.  
    26. msleep(200);
    27. }
    To copy to clipboard, switch view to plain text mode 

    you may not worry about read and write functions to serial, as they are properly checked and work fine without using threads.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread, bad file descriptor

    Quote Originally Posted by saman_artorious View Post
    can't be more minimized than above now.
    But it can be more compilable than now.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QThread, bad file descriptor

    let me think how to tell you what I really want to do.
    Last edited by saman_artorious; 22nd April 2013 at 14:54.

  6. #6
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QThread, bad file descriptor

    thanks for the producer consumer problem you referenced. i tried it. I use two instances of this class, in the first, producer writes packets to queue, consumer dequeues and writes them to serial. (So, two threads). in the next two threads, producer reads from serial constantly and enqueues buffer, the consumer then dequeues and updates UI.
    I have the following problems:

    1. in this class queue is defined privately inside threadsafequeue, this causes some problems, other objects cannot enqueue inside the queue if it is private! I have defined the threads in main.cpp. but how can other object tell it that they want these packets enQd into queue. so that the consumer can write them to serial? on the other hand if I define the queue not inside of the class and instead in a global header, I receive multiple definition of variable error.

    2.Let's consider four threads, the RS class that communicates with serial and other objects that enqueue packets to queue to be written to serial. The write to serial in producer works fine. but the consumer can never read from serial, always returns length 0 or rarely length that is less than expected. Moreover, in this class I cannot pass packets to thread, otherwise, other object coudav used thread instance to enquque packets into its Q.

    3. I used emit in a single thread, it works fine, but in the producer consumer class emit are not active. when I receive the packet from serial inside of RS class, I emit it to UI. but it doesn't work!

    I do not know how to correct the structure of my program. please help me. I added the code above to clarify what is happening in my code.
    please ask me for further info, i am very curious to solve the structure of my program with threads.

    thanks for the producer consumer problem you referenced. i tried it. I use two instances of this class, in the first, producer writes packets to queue, consumer dequeues and writes them to serial. (So, two threads). in the next two threads, producer reads from serial constantly and enqueues buffer, the consumer then dequeues and updates UI.
    I have the following problems:

    1. in this class queue is defined privately inside threadsafequeue, this causes some problems, other objects cannot enqueue inside the queue if it is private! I have defined the threads in main.cpp. but how can other object tell it that they want these packets enQd into queue. so that the consumer can write them to serial? on the other hand if I define the queue not inside of the class and instead in a global header, I receive multiple definition of variable error.

    2.Let's consider four threads, the RS class that communicates with serial and other objects that enqueue packets to queue to be written to serial. The write to serial in producer works fine. but the consumer can never read from serial, always returns length 0 or rarely length that is less than expected. Moreover, in this class I cannot pass packets to thread, otherwise, other object coudav used thread instance to enquque packets into its Q.

    3. I used emit in a single thread, it works fine, but in the producer consumer class emit are not active. when I receive the packet from serial inside of RS class, I emit it to UI. but it doesn't work!

    I do not know how to correct the structure of my program. please help me. I added the code above to clarify what is happening in my code.
    please ask me for further info, i am very curious to solve the structure of my program with threads.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread, bad file descriptor

    First of all I don't understand why you are using threads at all here if they are causing you problems. Implement your program in a single thread instead.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QThread, bad file descriptor

    I did but it slows down the UI. because when timers trigger, they write and read from serial. So, meanwhile entering input in the UI will not be fast. The write n read to the serial is in the way that when I write, I delay() a bit followed by the read. Then I emit the read packet to UI. This is why decided to use Thread to handle write n read to serial in another thread so that UI does not have to wait for serial write n reads.
    What should I do ?

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread, bad file descriptor

    Quote Originally Posted by saman_artorious View Post
    I did but it slows down the UI.
    If it does then apparently you have not done it correctly.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QThread, bad file descriptor

    and about the bad file descriptor. say, I have a serial class:
    Qt Code:
    1. class RS
    2. {
    3. }
    4. static RS var
    To copy to clipboard, switch view to plain text mode 
    I open this port in mainwindow, but when I access its member function from inside of a thread using the static variable
    Qt Code:
    1. ::run()
    2. {
    3. staticvariable.sendToserial(param);
    4. msleep(300);
    5. }
    To copy to clipboard, switch view to plain text mode 
    it gives bad file descriptor. it seems that the variable fd defined inside serial class gets lost!
    I also replaced opening port from mainwindow to serial class constructor, this way when program runs, it opens port many times!

    any ideas?

  11. #11
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread, bad file descriptor

    The best idea is to use ready classes wrappers for work with serial ports in Qt. For example QtSerialPort (preferred) or QextSerialPort.
    It much quicker and more conveniently, than you will fool about with threads, etc., IMHO.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread, bad file descriptor

    Quote Originally Posted by saman_artorious View Post
    and about the bad file descriptor. say, I have a serial class:
    Qt Code:
    1. class RS
    2. {
    3. }
    4. static RS var
    To copy to clipboard, switch view to plain text mode 
    I open this port in mainwindow, but when I access its member function from inside of a thread using the static variable
    Qt Code:
    1. ::run()
    2. {
    3. staticvariable.sendToserial(param);
    4. msleep(300);
    5. }
    To copy to clipboard, switch view to plain text mode 
    it gives bad file descriptor. it seems that the variable fd defined inside serial class gets lost!
    I also replaced opening port from mainwindow to serial class constructor, this way when program runs, it opens port many times!

    any ideas?
    I have no idea what your code as doing and since you're not willing to show your code but instead all you do is provide random snippets that do not demonstrate the problem, there is nothing I (and possibly all the other participants) can do for you.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QThread, bad file descriptor

    Ok, Consider this thread, it acts like a timer, send some packet to serial:
    Qt Code:
    1. void PlCThead::run()
    2. {
    3. while(1)
    4. {
    5. const char str[]={UPDATE_PACKET};
    6.  
    7. emit requestForWriteAndReceive((char* )str, 3);
    8.  
    9. msleep(100);
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    emit works fine, it goes inside the slot, there, it writes only 78 or char x to serial instead of a packet of 3 bytes.


    Qt Code:
    1. bool RS::rs_serialWrite(char* buff, size_t length)
    2. {
    3. int variables....;
    4.  
    5. QByteArray built((char*)buff, length);
    6. qDebug() << built.toHex(); //this is correct, a packet of size 3
    7.  
    8. len = write(fd, buff, length);
    9.  
    10. qDebug() << len; //this returns 3! but it only write x to serial
    11.  
    12. qDebug() << strerror(errno);
    13.  
    14. return true;
    15. }
    To copy to clipboard, switch view to plain text mode 

    strerror returns back this warning:
    Resource temporarily unavailable
    any ideas? this code works fine with timers, it has been checked and tested accurately, but now i need to add this thread instead of the timer. Thanks ()

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread, bad file descriptor

    As I already said about a week ago, your code in run() is invalid. You are emitting a pointer to a local variable which you'll likely overwrite with other data before anyone has a chance to use the pointer. Emit a byte array instead or use some other data structure to share data between threads. Or drop threads, you really have no need for them.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #15
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QThread, bad file descriptor

    i cannot attach files here, I will attach it in a new post.

  16. #16
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default threads with serial

    I cannot simply omit char* and use QByteArray as local variable, or maybe I can clear it everytime run re-loops can I?

    I do not understand when you note I do not use threads at all. I am giving you a snippet code attached. I omitted timer with one thread instead. There is no other way, the serial device is LECOM, the thread or TImer cannot simply write and return, it needs to wait until it receives packet from serial and then it returns with what it got from the serial.

    I made the code very very short and useful, please have a look. I want to know your opinion about how to get rid of these problems.
    I note again, that the code works very fine with Timer, the only problem is that when it slows down the input, the pc is a intel Series PC, it is very weak. when the timer write and wait for the reads this causes input to have a 1 second delay. (UI input)
    thanks
    Attached Files Attached Files

  17. #17
    Join Date
    Nov 2009
    Location
    San Antonio, TX
    Posts
    69
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: threads with serial

    Not sure what you are trying to accomplish with the static defined in the header file (static RS rs_plc.
    The static means that there will be one copy of 'rs_plc' created for each source file it is included in, is that what you want?
    Is that the reason the fileno is 0 or unavailable?

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: threads with serial

    Quote Originally Posted by saman_artorious View Post
    I cannot simply omit char* and use QByteArray as local variable
    Why not?

    I do not understand when you note I do not use threads at all. I am giving you a snippet code attached.
    I still see no use for threads.

    it needs to wait until it receives packet from serial and then it returns with what it got from the serial.
    No, it doesn't. First you can check to see if there is anything waiting in the port to be read. If not, there is no point in trying to read since there is nothing there. Alternatively you can set your port to non-blocking mode and try to read anyway. You'll get an error and can carry on. There are many approaches you can take.

    I made the code very very short and useful, please have a look.
    For me "short" means max 50 lines of code Your code is bloated and highly C-ish. And the static variable in the header file indeed looks horrible

    I want to know your opinion about how to get rid of these problems.
    Here is my opinion, read this: http://www.mindview.net/Books/TICPP/...ngInCPP2e.html
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #19
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: threads with serial

    No, it doesn't. First you can check to see if there is anything waiting in the port to be read. If not, there is no point in trying to read since there is nothing there. Alternatively you can set your port to non-blocking mode and try to read anyway. You'll get an error and can carry on. There are many approaches you can take.
    I am also not interested in threads if timers do not slow down system speed in my case. thanks for the book, I will definitely have a look, but for now, if I can run this C-ish code without threads, it values most.

    I also tried opening fd in non block mode, but the speed is again slow.

    Qt Code:
    1. fd = open(portPath, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK, S_IWUSR | S_IRUSR | S_IXUSR);
    To copy to clipboard, switch view to plain text mode 

    what am i missing? and i do not understand when you say I should read from it when there is something there. Of course almost always when I write, I immediately get something from serial, it has been designed this way.

  20. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: threads with serial

    Timers do not slow down anything. And you don't need them too, anyway.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 23
    Last Post: 17th April 2013, 09:52
  2. QProcess: Socket descriptor in argv[]
    By Jonny174 in forum Qt Programming
    Replies: 3
    Last Post: 18th February 2012, 12:42
  3. Socket Descriptor
    By ManuMies in forum Qt Programming
    Replies: 1
    Last Post: 17th March 2009, 09:42
  4. Watching a UNIX file descriptor
    By invictus in forum Newbie
    Replies: 1
    Last Post: 13th January 2009, 20:26
  5. Replies: 3
    Last Post: 25th May 2007, 07:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.