Results 1 to 4 of 4

Thread: Problems with Threads connect

  1. #1
    Join Date
    Mar 2010
    Posts
    15

    Default Problems with Threads connect

    Hi,

    I have created a Thread class:
    Qt Code:
    1. #ifndef SERIALTHREAD_H
    2. #define SERIALTHREAD_H
    3.  
    4. #include <QThread>
    5. #include <qDebug>
    6. #include "qextserialport.h"
    7. class SerialThread : public QThread
    8. {
    9. Q_OBJECT
    10. public:
    11. SerialThread(QString name) {
    12. portName=name;
    13. }
    14.  
    15. void run();
    16.  
    17.  
    18. private:
    19. QString portName;
    20. QextSerialPort *port;
    21.  
    22. private slots:
    23. void onReadyRead();
    24.  
    25. };
    26.  
    27. #endif // SERIALTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "SerialThread.h"
    2.  
    3. void SerialThread::run() {
    4. port = new QextSerialPort(portName, QextSerialPort::EventDriven);
    5. connect(port, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
    6. port->setBaudRate(BAUD9600);
    7. port->setFlowControl(FLOW_OFF);
    8. port->setParity(PAR_NONE);
    9. port->setDataBits(DATA_8);
    10. port->setStopBits(STOP_1);
    11. port->open(QIODevice::ReadWrite);
    12. exec();
    13. }
    14.  
    15. void SerialThread::onReadyRead()
    16. {
    17. QByteArray bytes;
    18. int a = port->bytesAvailable();
    19. bytes.resize(a);
    20. port->read(bytes.data(), bytes.size());
    21. qDebug() << "bytes read:" << bytes.size();
    22. qDebug() << "bytes:" << bytes;
    23. }
    To copy to clipboard, switch view to plain text mode 

    In the MainWindow I call this Thread:
    Qt Code:
    1. MainWindow::MainWindow() {
    2. setupUi(this);
    3. t1 = new SerialThread("/dev/cu.usbserial-ftDIHUS6");
    4. t1->start();
    5. }
    To copy to clipboard, switch view to plain text mode 

    it seems to be a problem with the signals. When I send:
    u I get:
    Qt Code:
    1. bytes read: 1
    2. bytes: "u"
    3. bytes read: 0
    4. bytes: ""
    5. bytes read: 0
    6. bytes: ""
    7. bytes read: 0
    8. bytes: ""
    9. bytes read: 0
    10. bytes: ""
    11. bytes read: 0
    12. bytes: ""
    13. bytes read: 0
    14. bytes: ""
    15. bytes read: 0
    16. bytes: ""
    17. bytes read: 0
    18. bytes: ""
    19. bytes read: 0
    20. bytes: ""
    21. bytes read: 0
    22. bytes: ""
    To copy to clipboard, switch view to plain text mode 


    Without Threads it works so what's the problem?

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problems with Threads connect

    Hi,

    Why do you want to use QextSerialPort inside a thread if you are using it as EventDriven?
    Òscar Llarch i Galán

  3. #3
    Join Date
    Mar 2010
    Posts
    15

    Default Re: Problems with Threads connect

    It was only I try. Later I wanted to use a protocol and some heavy calculations. But it shouldn't be a problem, when a thread have it's one event loop.

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problems with Threads connect

    Hi,

    Think on that QextSerialPort 1.2 is an Alpha version (not a beta and not a release) so it may contain a lot of errors(the creator have closed the development). Try checking the bytes obtained on "bytesAvailable" and if it is 0 just ignore it.
    Òscar Llarch i Galán

Similar Threads

  1. Replies: 16
    Last Post: 16th February 2010, 13:17
  2. qRegisterMetaType - problems with connect()
    By Macok in forum Qt Programming
    Replies: 1
    Last Post: 5th March 2009, 22:55
  3. problems with Threads.
    By Yeshwin in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 16th February 2009, 22:20
  4. Replies: 4
    Last Post: 10th November 2006, 15:38
  5. Problems with threads and windows
    By SkripT in forum Qt Programming
    Replies: 15
    Last Post: 16th January 2006, 17:46

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.