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