lemanhtrung / qextserialport

Automatically exported from code.google.com/p/qextserialport
Other
0 stars 0 forks source link

bytesAvailable() returns 4,294,967,295 #128

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
System:
Windows 7 64bit
QExtSerialPort 1.2beta1 (built with Mingw-gcc4.6 or Msvc2010 (static and 
dynamic libraries))
Using a USB to serial dongle (These generally cause odd behaviour but older 
versions of QExtSerialPort do not have this problem)

Problem:
Using a loop like below to read data from a QExtSerialPort named serialPort 
works fine for a period (10 seconds up to 2 minutes) then 
serialPort->bytesAvailible() starts returning 4,294,967,295, which is not good. 

QByteArray dataBuffer;
while(true)
{
   Sleep(1);
   while(serialPort->bytesAvailible() > 0)
   {
      dataBuffer.append(serialPort->realAll());

      // do stuff with data buffer to read messages
   }
}

Initial Investigation:
The problem seems to come from qextserialport_win.cpp line 157.  Where 
Status.cbInQue which is a DWORD (unsigned long) gets the number 4,294,967,295 
placed in it and is put into a qint64.  It seems like this value should 
actually be seen as a -1 because it is an error (I don;t thing we should ever 
have 4GB of data).  Replacing the line with

return static_cast<qint32>(Status.cbInQue);

results in it returning a -1 which I think is more correct.

Bigger Issue:
That's not the whole issue though because, even with is now returning -1 the 
port just starts returning errors after a short period of working and the only 
way to make it go again seems to be deleting the QExtSerialPort and creating a 
new one.  This behaviour does not occur with version 1.1 or a source tarball I 
downloaded in November 2011 (I can upload the tarball to compare against if 
needed).

Original issue reported on code.google.com by Ben.Arc...@gmail.com on 25 Apr 2012 at 11:45

GoogleCodeExporter commented 9 years ago
Hi Ben,

Perhaps you can try to initialize Errors and Status to Zeros. 
Output the value of Errors when you get 4,294,967,295. and output the return 
value of GetLastError() too.

qint64 QextSerialPortPrivate::bytesAvailable_sys() const
{
    DWORD Errors;
    COMSTAT Status;
    if (ClearCommError(Win_Handle, &Errors, &Status)) {
        return Status.cbInQue;
    }
    return (qint64)-1;
}

Seems you are using Polling Mode, if so, only ReadFile() will be called once 
port is opened.

qint64 QextSerialPortPrivate::readData_sys(char *data, qint64 maxSize)
{
    DWORD bytesRead = 0;
    if (!ReadFile(Win_Handle, (void*)data, (DWORD)maxSize, & bytesRead, NULL))
        return -1;
    return (qint64)bytesRead;
}

You can output the return value of GetLastError() here too when error occur.

Hope this is useful to you.

Original comment by dbzhang...@gmail.com on 26 Apr 2012 at 2:04

GoogleCodeExporter commented 9 years ago
How to use ReadData() prototype 

Original comment by sushantb...@gmail.com on 23 Jul 2013 at 5:17