PDA

View Full Version : Data corruption with read() / ReadFile()



Teuniz
20th November 2009, 16:07
I have a problem with Qt and reading bytes from the serial port.

I have a QTimer at 100 mS. This timerhandler call's read() on Linux or
ReadFile() on windows in order to read bytes from the serial port.

Everything works well except when I put some load on the computer.
For example, I get datacorruption on the serial port when I plug a USB-stick into the computer (on Linux).
I get also data corruption when I open other applications (on windows).

I tried with a real COM-port and with a RS-232 to USB converter. Same result.

When I don't use Qt but instead use a console program that only uses the standard C-library,
no datacorruption occurs, even under heavy load conditions.

I lowered the amount of bytes sent to the pc from 9000 bytes/sec. to 1000 bytes/sec. Same result.

Any ideas?

Are there any known problems of mixing calls to read() or ReadFile() and Qt?

caduel
20th November 2009, 16:19
You are aware that the 100ms timer is not guaranteed to "fire" within 100ms?
(Linux is not a real-time OS.)

kuzulis
20th November 2009, 16:38
And why use a timer to call the reading?
It is easier to do - is to use ready-made libraries for use with serial port:
QextSerialPort
or
QSerialDevice v 0.1.0 (but better from SVN) :
release: http://fireforge.net/frs/download.php/386/QSerialDevice_v0.1.0.zip
svn:http://fireforge.net/snapshots.php?group_id=199

Teuniz
20th November 2009, 16:44
You are aware that the 100ms timer is not guaranteed to "fire" within 100ms?
(Linux is not a real-time OS.)

Yes, and I checked how many bytes are received during every call to read().
The device sends 772 bytes per second to the pc. Baudrate of the serial port is 115K2.
What I see is that one call receives 78 bytes, the next call 0 bytes, next call 78 bytes, next call 0 bytes, and so on.

When I plug-in the USB-stick, the amount of bytes received per call does not change which gives me the impression that the timer does not delay too much.

What happens is that in one block of data (772 bytes) 4 bytes are missing.

Teuniz
20th November 2009, 16:50
And why use a timer to call the reading?
It is easier to do - is to use ready-made libraries for use with serial port:
QextSerialPort
or
QSerialDevice v 0.1.0 (but better from SVN) :
release: http://fireforge.net/frs/download.php/386/QSerialDevice_v0.1.0.zip
svn:http://fireforge.net/snapshots.php?group_id=199

I tried QextSerialPort one or two years ago but it wouldn't compile and it was not maintained anymore.

I will have a look again at these libs but I want to know why Qt does not work well with direct call's to read()...

kuzulis
20th November 2009, 16:56
Teuniz ,

used only QSerialDevice (from SVN).

Your questions difficult to answer, because you did not show the code.

If you have any questions QSerialDevice - you can write to me at mail (e-mail address in source code QSerialDevice)