March 20, 2012

simonCpp simonCpp
Lab Rat
16 posts

QextSerialPort missing data [solved]

 

Hi all,

I’m using QextSerial port to read data from RS-232 at 400hz rate, most of the time I got currected data but sometimes I got invalid data (missing lines from the packet).

I checked the device with realTerm and the data is currect so it’s not a hardware bug.

what can cose this missing line error? some suggestions?
Thanks.

More info:

The algo:
If (start recording)
1. clear buffer
2. read data and sync on the packet header
3. check sum
4. save data to files
if (stop recording)
1. read data (clear buffer)

My setup
QT 4.7
QextSerialPort 1.2.0
Win xp sp3
using sea-level (rs-232 to usb)

Open port function:

Source code

  1.     this->setState(STOP_RECORDING);
  2.     port = new QextSerialPort(portName,QextSerialPort::EventDriven);
  3.     if(port->open(QIODevice::ReadWrite )) {
  4.           connect( port, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
  5.     }
  6.     else {
  7.             return -1;
  8.     }
  9.  
  10.     port->setBaudRate(BAUD691200);
  11.     port->setDataBits(DATA_8);
  12.     port->setParity(PAR_NONE);
  13.     port->setStopBits(STOP_2);
  14.     port->setFlowControl(FLOW_OFF);
  15.     port->setRts( true );
  16.     port->setDtr( true );
  17.  
  18.     connect(&timer, SIGNAL(timeout()), this, SLOT(procTimerOut()));
  19.     this->timer.setInterval(50);
  20.     return 0;

Reading from port:

  1. #define PACKET_SIZE 40
  2.  
  3. void Driver::onReadyRead()
  4. {/* Read data from port */
  5.     if (this->state() == STOP_RECORDING) {
  6.         this->port->read(port->bytesAvailable());
  7.         return;
  8.     }
  9.  
  10.     if (this->rxBuffer.isEmpty()) {
  11.         this->timer.start();
  12.     }
  13.     QByteArray data = this->port->read(port->bytesAvailable());
  14.     this->rxBuffer.append(data);
  15. }
  16. void Driver::procTimerOut()
  17. {
  18.     this->timer.stop();
  19.     /* Print raw data to file */
  20.     files_data.rawData.write(rxBuffer.toHex());
  21.     if (!this->last_line.isEmpty()){
  22.         rxBuffer.prepend(this->last_line);
  23.     }
  24.     if (rxBuffer.size() < PACKET_SIZE) {
  25.         qDebug("buffer is small [%d]",rxBuffer.size());
  26.         return;
  27.     }
  28.     if (this->sync(rxBuffer) != SUCCESS) {
  29.         emit actionDone(EINVAL,"Cannot sync on packet");
  30.         return;
  31.     }
  32.  
  33.     while (rxBuffer.size() > PACKET_SIZE) {
  34.         const QByteArray sample = rxBuffer.mid(0, PACKET_SIZE);
  35.         rxBuffer.remove(0, PACKET_SIZE);
  36.         if (this->addSample(sample) == INVALID_CHECKSUM)  {
  37.                 QString errMsg = this->portID + ": Invalid checksum at ["
  38.                                  + QString::number(this->sampleCounter) + "]\n"
  39.                                  + QString(sample.toHex());
  40.                 emit actionDone(INVALID_LINE,errMsg);
  41.                 rxBuffer.clear();
  42.                 return;
  43.             }
  44.         }
  45.     }
  46.     if (!rxBuffer.isEmpty()) {
  47.         this->last_line = rxBuffer;   //read all data that left
  48.         rxBuffer.clear();             //remove all data that left
  49.     }
  50.     else {
  51.         this->last_line.clear();
  52.     }
  53. }
  54. int Driver::sync(QByteArray &rxBuffer)
  55. {
  56.     if (!rxBuffer.startsWith(this->key)){//Note: Sync on packet
  57.         int prefix_offset = rxBuffer.indexOf(this->key);
  58.         if (prefix_offset == -1){
  59.             return EINVAL;
  60.         }
  61.         rxBuffer.remove(0,prefix_offset);
  62.         if (!rxBuffer.startsWith(this->key)) {
  63.             return EINVAL;
  64.         }
  65.     }
  66.     return SUCCESS;
  67. }
  68. inline int Driver::addSample(const QByteArray & sample)
  69. {
  70.     const struct packet *pkt;
  71.     pkt = (const struct packet *)sample.constData();
  72.     int checksum_bytes = sizeof(struct packet) - sizeof(pkt->checksum);
  73.     u16 crc = checksum((const unsigned char *)pkt, checksum_bytes);
  74.     if ( crc != pkt->checksum ) {
  75.         return INVALID_CHECKSUM;
  76.     }
  77.     this->print_analyzed_sample(pkt);
  78.     this->sampleCounter++;
  79.     return SUCCESS;
  80. }
  81. }
  82.  
  83. void Driver::startRecording()
  84. {
  85.     this->prepareForRecord();
  86.     this->setState(START_RECORDING);
  87. }
  88. void Driver::stopRecording()
  89. {
  90.     this->setState(STOP_RECORDING);
  91.     this->closeFiles();
  92. }
  93.  
  94. void Driver::prepareForRecord()
  95. {
  96.     this->genetate_output_Files(); //creat output files, and open to write data
  97.     //reset data
  98.     QByteArray buf;
  99.     do {
  100.         buf = port->readAll();
  101.     } while (buf.size() > 0);
  102.     this->rxBuffer.clear();
  103. }

15 replies

March 20, 2012

1+1=2 1+1=2
Hobby Entomologist
638 posts

Hello,

  • Maybe you can give a try to QextSerialPort::Polling mode with a QTimer in order to see whether this issue still exists.
  • If you are using Qt SDK which does not contains a qwineventnotifier_p.h under the “include/QtCore/private” directory, QextWinEventNotifier will be used. If so, you can disable this by coping qwineventnofifier_p.h form src\corelib\kernel to that directory.

Debao

March 21, 2012

simonCpp simonCpp
Lab Rat
16 posts

Hello Debao,
Thanks for your replay.

I tried to use the QextSerialPort::Polling mode with a QTimer before and it still miss data.

About the qwineventnotifier_p.h file, I do have it at the “include/QtCore/private” directory.

I got lack of understanding about what can cause this issue.
Need help.

March 28, 2012

simonCpp simonCpp
Lab Rat
16 posts

Need help anyone?

March 28, 2012

1+1=2 1+1=2
Hobby Entomologist
638 posts

Hi,

I have submitted a bug several day ago, but seems that others do not come with the similar problem.

http://code.google.com/p/qextserialport/issues/detail?id=121

So I do not how to do with this.

However, I found a similar issue at stackoverflow which has been solved.

http://stackoverflow.com/questions/9713656/qextserialport-drops-data-on-windows-what-can-i-do-about-it

You can give a try to it.

Debao

June 28, 2012

Dantcho Dantcho
Lab Rat
8 posts

Hi all,

want to use RS232 interface in QtCreator under windows at first,
but i can not find a appropriate library.

Do anybody have some information obout this?

Thanks!

June 28, 2012

koahnig koahnig
Gene Splicer
2905 posts

welcome to devnet

There are at least two different Qt-based implementations available. There have been already a number of threads concerning the topic. One of the implementations or also both are mentioned in following threads with following tags:
QExtSerialPort [qt-project.org]
QSerialDevice [qt-project.org]
[edit]
A good start might be this overview [qt-project.org]

June 29, 2012

Dantcho Dantcho
Lab Rat
8 posts

Thanks koahing very much,

for the information, I knowed about “QExtSerialPort”, but the QSerialDevice can be the right for my project.

June 29, 2012

kuzulis kuzulis
Robot Herder
361 posts

2 Dantcho

but the QSerialDevice can be the right for my project

Instead of QSerialDevice – use QtSerialPort [qt-project.org].
QSerialDevice is no longer growing (develop is freezing). His code was used as the basis QtSerialPort. Now QtSerialPort is a child.

July 16, 2012

Dantcho Dantcho
Lab Rat
8 posts

Hi all,

I have to work with serial port and need to to install it on my computer.
What i have to do to install it?

I download this:
“qtplayground-qtserialport”

from here:
http://qt-project.org/wiki/QtSerialPort

but I am a little confuse because the installing guide is for linux not for windows.
How to install qt serial port on windows?

Thanks!

July 16, 2012

kuzulis kuzulis
Robot Herder
361 posts

What an embarrassment? Where does it say that only for Linux?

Read it [qt-project.org] again

August 13, 2012

Dantcho Dantcho
Lab Rat
8 posts

Hi everybody,

i develop a programm and i want my widget do not be moved by the mouse(press and drag), the widget must stay always on the same position.
How can i do this?

Thanks?

August 16, 2012

Dantcho Dantcho
Lab Rat
8 posts

Hi everybody,

I createt tree windows with the QtDesigner and from windows one of them start the another windows,
from windows one I have connection to the another windows (include the header files), so I include the the header file from the windows one to the another windows to have connection from every window to the window one, but wenn I declare filetype from windows one I get the following error.
“Fehler:ISO C++ forbids declaration of ‘Com_Net’ with no type”

this is a C++ error, but I include the header files :-(((.

Wath cann I do to solve this problem.

Here is the header file from the another windows.

  1. #ifndef DICS_H
  2. #define DICS_H
  3.  
  4. #include <QWidget>
  5. #include <QSizePolicy>
  6. #include <QMoveEvent>
  7.  
  8. #include "com_net.h"      bq. here include the header files from the windows one
  9. #include "ui_com_net.h"
  10.  
  11. namespace Ui {
  12. class DICS;
  13. }
  14.  
  15. class DICS : public QWidget
  16. {
  17.     Q_OBJECT
  18.    
  19. public:
  20.     explicit DICS(QWidget *parent = 0);
  21.     ~DICS();
  22.  
  23. private slots:
  24.       void on_External_Button_clicked();
  25.       void on_Internal_Button_clicked();
  26.       void on_PABX_Button_clicked();
  27.       void on_PAS_Button_clicked();
  28.    
  29. private:
  30.     Ui::DICS *ui;
  31.     Com_Net *active_com_net;      bq. and here is the pointer declaration from windows one.
  32.     virtual void moveEvent(QMoveEvent *event);
  33. };
  34.  
  35. #endif // DICS_H

Thanks very much, for the help.

August 16, 2012

Dantcho Dantcho
Lab Rat
8 posts

Hallo everybody,

i solved this problem with the type visibility.

August 16, 2012

mlong mlong
Robot Herder
1551 posts

Be sure and edit your original post title to add [Solved]. Thanks!

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

September 28, 2012

Dantcho Dantcho
Lab Rat
8 posts

Dear community,

I work with the “qextserialport-1.2beta2” and all work well, but I have one problem, I want to send in binary mode, ones I can set is “port->setTextModeEnabled(false);” but the serial port stil interpret the data “o” like a null terminator and so cut the string after the every “0”. my question is it possible to send by “qextserialport” binary mode, or I have to look for a another way.

thanks for the answer, :-))).

 
  ‹‹ Anyone tried QTitanGrid and QTitanRibbon?      Using National Instrument’s LabVIEW C-Code Generator with Qt ››

You must log in to post a reply. Not a member yet? Register here!