Results 1 to 4 of 4

Thread: QtSerialPort : missing received datas / Console application

  1. #1
    Join Date
    May 2016
    Posts
    2
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default QtSerialPort : missing received datas / Console application

    Hi everyone,

    I hope you are fine.

    I started this thread because I am facing with a QtSerialPort problem. I have a computer with two serial ports (in 2016!!!) that I have connected together with a crossed subd9 cable. Now I am trying to send informations from one to other.

    The problem is that they are always missing datas (at the end of the vector). This is confirmed with an external program (Device monitoring studio) wich is a serial sniffer (hahaha).

    I wrote a small program to illustrate this problem :


    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QtSerialPort/QtSerialPort>
    3. #include<QDebug>
    4. #include "windows.h"
    5. #include <iostream>
    6.  
    7. using namespace std;
    8.  
    9.  
    10.  
    11. int main(int argc, char *argv[])
    12. {
    13. QCoreApplication a(argc, argv);
    14.  
    15.  
    16. int MessageLength=120;
    17. char *MessageToSend= new char[MessageLength];
    18.  
    19. int MessageLengthToRead=MessageLength;
    20. char *dataToReceive= new char [MessageLengthToRead];
    21.  
    22.  
    23. //*****************************************************************************************************************
    24. //Here I create two serial ports : the first one to emit and the second one to receive
    25. QSerialPort *SerialEmitter = new QSerialPort;
    26. QSerialPort *SerialReceiver = new QSerialPort;
    27.  
    28. //*****************************************************************************************************************
    29. //Here we initialize serial ports
    30.  
    31.  
    32. SerialEmitter->setPortName("COM9");
    33. SerialEmitter->open(QSerialPort::OpenModeFlag::ReadWrite);
    34. SerialEmitter->setBaudRate(QSerialPort::Baud115200);
    35. SerialEmitter->setDataBits(QSerialPort::Data8);
    36. SerialEmitter->setParity(QSerialPort::NoParity);
    37. SerialEmitter->setStopBits(QSerialPort::OneStop);
    38.  
    39. //*****************************************************************************************************************
    40. //Here I initialize the message i am going to send
    41.  
    42. SerialReceiver->setPortName("COM12");
    43. SerialReceiver->open(QSerialPort::OpenModeFlag::ReadWrite);
    44. SerialReceiver->setBaudRate(QSerialPort::Baud115200);
    45. SerialReceiver->setDataBits(QSerialPort::Data8);
    46. SerialReceiver->setParity(QSerialPort::NoParity);
    47. SerialReceiver->setStopBits(QSerialPort::OneStop);
    48. SerialReceiver->setFlowControl(QSerialPort::NoFlowControl);
    49. SerialReceiver->setReadBufferSize(MessageLength);
    50.  
    51. SerialReceiver->clear();
    52. SerialEmitter->clear();
    53.  
    54. //*****************************************************************************************************************
    55.  
    56. //Initialisation of the data to send
    57. for (int i =0; i<MessageLength; i++)
    58. {
    59. MessageToSend[i]=(char)(i);
    60. }
    61.  
    62. //Displaying Datas
    63. for(int i=0;i<MessageLength;i++)
    64. {
    65. cout<<(int)((unsigned char)MessageToSend[i])<<" ";
    66.  
    67. }
    68. cout<<endl;
    69.  
    70. cout<< "***************************************************************"<<endl;
    71. //Here I write datas
    72. cout<< "The value retrun of Serial emitter wtrite is "<< SerialEmitter->write(MessageToSend,MessageLength)<<endl;
    73.  
    74. //Here I wait loooong time!
    75. SerialEmitter->waitForBytesWritten(-1);
    76. SerialReceiver->waitForReadyRead(1000);
    77.  
    78.  
    79. //Here I read datas
    80. cout<< "Number of readed bytes "<<SerialReceiver->read(dataToReceive, MessageLength)<<endl;
    81.  
    82.  
    83. for(int i=0;i<(MessageLengthToRead);i++)
    84. {
    85. cout<<(int)((unsigned char)dataToReceive[i])<< " ";
    86.  
    87. }
    88. cout<<endl;
    89.  
    90. SerialEmitter->close();
    91. SerialReceiver->close();
    92.  
    93. return a.exec();
    94. }
    To copy to clipboard, switch view to plain text mode 


    Here is what the program write in the console : here normally both vector stop @119 but the program read only 65 bytes (even if I wait more time with the waitForReadyRead commad).

    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    ************************************************** *************
    The value retrun of Serial emitter wtrite is 120
    Number of readed bytes 65
    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 0 51 0 50 0 92 0 119 0 105 0 110 0 109 0 109 0 46 0 100 0 108 0 108 0 0 0 192 0 3 7 116 0 95 0 53 0 95 0 54 0 95 0 48 0 95 0 77 0 105 0 110 0 71 0
    If any of you has an idea to fix this, I will really appreciate because I am really freezing.

    QSerialPort

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

    Default Re: QtSerialPort : missing received datas / Console application

    Looks like you only wait for the first readyRead, only get part of the data, but instead iof waiting for more you continue with what you have.

    Cheers,
    _

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

    Awawa (1st June 2016)

  4. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,460
    Thanked 271 Times in 266 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtSerialPort : missing received datas / Console application

    From QIODevice::waitForReadyRead doc : Blocks until new data is available for reading and the readyRead() signal has been emitted, or until msecs milliseconds have passed. -
    which means don't wait if any data (even one byte) are ready.

  5. The following user says thank you to Lesiok for this useful post:

    Awawa (1st June 2016)

  6. #4
    Join Date
    May 2016
    Posts
    2
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QtSerialPort : missing received datas / Console application

    Hi Guys,

    Thank you very much for your answers. I rode the QIODevice::waitForReadyRead definition too fast and I misunderstood this point.

    For guys who will read this thread and who will be faced with the same problem, i can suggest this solution (which is a blocking one). This solution is exposed in qt example.

    Qt Code:
    1. QByteArray dataToReceiveQBA;
    2.  
    3. char* dataToReceive= new char;
    4.  
    5.  
    6.  
    7. cout<< "the device is open Y/N: "<< SerialEmitter->isOpen()<<endl;
    8. cout<< "The value retrun of this function is "<< SerialEmitter->write(TheMessage,TailleMessage*sizeof(char))<<endl;
    9. SerialEmitter->waitForBytesWritten(200);
    10.  
    11. dataToReceiveQBA=SerialEmitter->readAll();
    12.  
    13. while (SerialReceiver->waitForReadyRead(250))
    14. {
    15. dataToReceiveQBA.append(SerialEmitter->readAll());
    16. }
    17.  
    18.  
    19. dataToReceive= dataToReceiveQBA.data();
    To copy to clipboard, switch view to plain text mode 
    Last edited by Awawa; 1st June 2016 at 11:50.

Similar Threads

  1. Replies: 4
    Last Post: 29th July 2015, 05:41
  2. Replies: 2
    Last Post: 22nd March 2014, 18:35
  3. Replies: 1
    Last Post: 15th January 2013, 19:08
  4. Replies: 2
    Last Post: 21st November 2010, 18:03
  5. KDevelop + QT + GDB = Application received SIGTRAP
    By GodOfWar in forum Qt Programming
    Replies: 7
    Last Post: 29th April 2008, 19:59

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.