Results 1 to 20 of 20

Thread: QThread, bad file descriptor

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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.