Results 1 to 4 of 4

Thread: QSerialport in multithread

  1. #1
    Join Date
    Aug 2012
    Location
    Kuala Lumpur, Malaysia
    Posts
    9
    Thanks
    8
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default QSerialport in multithread

    Hi guys..

    Previously i'm using external library for accessing RS232 serial port. Now i want to try to use Qserialport in Qt5.1.
    however, i have some problems as i'm using multithreading program (Qthread).

    Main thread
    Qt Code:
    1. void MainWindow::on_plotButton_clicked()
    2. {
    3. //Initialize thread for acc
    4. Thread_Acc = new QThread;
    5. Sensors* Acc = new Sensors();
    6. Acc->moveToThread(Thread_Acc);
    7. connect(Thread_Acc, SIGNAL(started()), Acc, SLOT(FetchData_AW()));
    8. connect(Acc, SIGNAL(finished()), Thread_Acc, SLOT(quit()));
    9. connect(Acc, SIGNAL(finished()), Acc, SLOT(deleteLater()));
    10. connect(Thread_Acc, SIGNAL(finished()), Thread_Acc, SLOT(deleteLater()));}
    To copy to clipboard, switch view to plain text mode 

    Worker thread
    Qt Code:
    1. void Sensors::FetchData_AW()
    2. {
    3. foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
    4. qDebug() << "Name : " << info.portName();
    5. qDebug() << "Description : " << info.description();
    6. qDebug() << "Manufacturer: " << info.manufacturer();
    7.  
    8. if(info.portName() == "COM24")
    9. {
    10. cport_nr_Acc.setPort(info);
    11. cport_nr_Acc.close();
    12. if (cport_nr_Acc.open(QIODevice::ReadWrite))
    13. {
    14. cport_nr_Acc.setBaudRate(57600);
    15. cport_nr_Acc.setDataBits(QSerialPort::Data8);
    16. cport_nr_Acc.setParity(QSerialPort::NoParity);
    17. cport_nr_Acc.setStopBits(QSerialPort::OneStop);
    18. cport_nr_Acc.setFlowControl(QSerialPort::NoFlowControl);
    19. }else
    20. return;
    21. break;
    22. }
    23. }
    24. Fetch_ACC = new QTimer (this);
    25. connect(Fetch_ACC,SIGNAL(timeout()),this,SLOT(Loop_DataAW()));
    26. Fetch_ACC->start(55);
    27. }
    To copy to clipboard, switch view to plain text mode 

    the program can run but it display warning as below and the input from the device is also not as expected (value not stabilize) comparing if i use external rs232 library.
    Qt Code:
    1. QObject: Cannot create children for a parent that is in a different thread.
    2. (Parent is QSerialPort(0x127bac98), parent's thread is QThread(0x10b40ed0), current thread is QThread(0x128967f0)
    3. QObject: Cannot create children for a parent that is in a different thread.
    4. (Parent is QSerialPort(0x127bac98), parent's thread is QThread(0x10b40ed0), current thread is QThread(0x128967f0)
    5. QObject: Cannot create children for a parent that is in a different thread.
    6. (Parent is QSerialPort(0x127bac98), parent's thread is QThread(0x10b40ed0), current thread is QThread(0x128967f0)
    To copy to clipboard, switch view to plain text mode 

    is there any interruption of the port if I'm using it from other than main thread?
    i already make the Qserialport as the variable in worker-thread class instead of global variable.
    what can i do to solve this issues because i really want to use readyRead signal.

    Thanks in advance... =)
    A little knowledge that acts is worth infinitely more than much knowledge that is idle
    Regards..

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSerialport in multithread

    Where to you create your QSerialPort instances?
    Maybe accidentally as non-pointer members of Sensors or in Sensors' constructor?

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    snow_starzz (3rd December 2013)

  4. #3
    Join Date
    Aug 2012
    Location
    Kuala Lumpur, Malaysia
    Posts
    9
    Thanks
    8
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QSerialport in multithread

    Qt Code:
    1. class Sensors : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Sensors();
    7. QSerialPort cport_nr_Acc;
    8.  
    9. ..... }
    To copy to clipboard, switch view to plain text mode 

    should i used pointer instead??


    Added after 1 45 minutes:


    SOLVE!!

    Thanks anda_skoa.. =)

    I'm using pointer for the qserialport and for the value, i'm not using the proper method to assigned the value from the QBytearray.

    Code below for others reference.. =)
    Qt Code:
    1. Sensors::Sensors()
    2. {
    3. cport_nr_Acc = new QSerialPort (this);
    4. }
    5. void Sensors::FetchData_AW()
    6. {
    7. cport_nr_Acc->setPortName("COM24");
    8. cport_nr_Acc->close();
    9. if (cport_nr_Acc->open(QIODevice::ReadWrite))
    10. {
    11. cport_nr_Acc->setBaudRate(57600);
    12. cport_nr_Acc->setDataBits(QSerialPort::Data8);
    13. cport_nr_Acc->setParity(QSerialPort::NoParity);
    14. cport_nr_Acc->setStopBits(QSerialPort::OneStop);
    15. cport_nr_Acc->setFlowControl(QSerialPort::NoFlowControl);
    16. }else
    17. return;
    18.  
    19. Fetch_ACC = new QTimer (this);
    20. connect(Fetch_ACC,SIGNAL(timeout()),this,SLOT(Loop_DataAW()));
    21. Fetch_ACC->start(25);
    22. }
    23. void Sensors::Loop_DataAW()
    24. {
    25. if(!EN_A)
    26. {
    27. Fetch_ACC->stop();
    28. cport_nr_Acc->close();
    29. emit finished();
    30. return;
    31. }
    32.  
    33. // expected to get 14 byte of input from device
    34. for(int y = 0;y<14;y++)
    35. {
    36. cport_nr_Acc->waitForReadyRead(50);
    37. nA = cport_nr_Acc->bytesAvailable();
    38. if(nA>=14)
    39. break;
    40. Sleep(1);
    41. }
    42.  
    43. if(nA == 14)
    44. {
    45. inacc = cport_nr_Acc->readAll();
    46. inacc[nA] = 0; // put a "null" at the end of a string!
    47.  
    48. char *p = inacc.data();
    49. // input buffer .xxyyzzxxyyzz. --> direct value
    50. dataAx = (static_cast<unsigned char>(p[1])<<8)|static_cast<unsigned char>(p[2]);
    51. dataAy = (static_cast<unsigned char>(p[3])<<8)|static_cast<unsigned char>(p[4]);
    52. dataAz = (static_cast<unsigned char>(p[5])<<8)|static_cast<unsigned char>(p[6]);
    53. dataWx = (static_cast<unsigned char>(p[7])<<8)|static_cast<unsigned char>(p[8]);
    54. dataWy = (static_cast<unsigned char>(p[9])<<8)|static_cast<unsigned char>(p[10]);
    55. dataWz = (static_cast<unsigned char>(p[11])<<8)|static_cast<unsigned char>(p[12]);
    56.  
    57. }else
    58. {
    59. qDebug() << "Polling fail " << nA;
    60. return;
    61. }
    To copy to clipboard, switch view to plain text mode 

    but why pointer should be use?? doesn't it still in the same thread if the variable is class variable??
    Last edited by snow_starzz; 3rd December 2013 at 03:26.
    A little knowledge that acts is worth infinitely more than much knowledge that is idle
    Regards..

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSerialport in multithread

    Quote Originally Posted by snow_starzz View Post
    but why pointer should be use?? doesn't it still in the same thread if the variable is class variable??
    The difference is that you now have a parent.

    Basically in your original situation the QSerialPort object was created by the main thread, as part of creating the Sensors object. At this point both "belonged" to the main thread.
    You then correctly moved the Sensors object to the new thread, however the QSerialPort instance was not.

    Now you have QSerialPort as a child of Sensors, it is being moved alongside its parent.
    Another option would have been to create it in the new thread's context, like you do with the QTimer.

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    snow_starzz (6th December 2013)

Similar Threads

  1. Replies: 12
    Last Post: 20th June 2013, 15:02
  2. setting QSerialPort port
    By saman_artorious in forum Qt Programming
    Replies: 5
    Last Post: 17th May 2013, 10:45
  3. Build QSerialPort with QT5.02 and mingw error
    By wang9658 in forum Newbie
    Replies: 5
    Last Post: 24th April 2013, 16:56
  4. Qt5 cmake and QSerialPort
    By Chris.Burner in forum Newbie
    Replies: 1
    Last Post: 21st April 2013, 17:13
  5. inbiza-labs qserialport on arm platform
    By sean_h in forum Newbie
    Replies: 0
    Last Post: 28th March 2012, 12:45

Tags for this Thread

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.