forked from qt-creator/qt-creator
Trk: Make Windows reader thread read > 1 bytes by querying size.
This commit is contained in:
@@ -486,6 +486,7 @@ public:
|
|||||||
signals:
|
signals:
|
||||||
void error(const QString &);
|
void error(const QString &);
|
||||||
void dataReceived(char c);
|
void dataReceived(char c);
|
||||||
|
void dataReceived(const QByteArray &data);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void terminate();
|
void terminate();
|
||||||
@@ -514,11 +515,23 @@ WinReaderThread::~WinReaderThread()
|
|||||||
// Return 0 to continue or error code
|
// Return 0 to continue or error code
|
||||||
int WinReaderThread::tryRead()
|
int WinReaderThread::tryRead()
|
||||||
{
|
{
|
||||||
|
enum { BufSize = 1024 };
|
||||||
|
char buffer[BufSize];
|
||||||
|
// Check if there are already bytes waiting. If not, wait for first byte
|
||||||
|
COMSTAT comStat;
|
||||||
|
if (!ClearCommError(m_context->device, NULL, &comStat)){
|
||||||
|
emit error(QString::fromLatin1("ClearCommError failed: %1").arg(winErrorMessage(GetLastError())));
|
||||||
|
return -7;
|
||||||
|
}
|
||||||
|
const DWORD bytesToRead = qMax(DWORD(1), qMin(comStat.cbInQue, DWORD(BufSize)));
|
||||||
// Trigger read
|
// Trigger read
|
||||||
char c;
|
|
||||||
DWORD bytesRead = 0;
|
DWORD bytesRead = 0;
|
||||||
if (ReadFile(m_context->device, &c, 1, &bytesRead, &m_context->readOverlapped)) {
|
if (ReadFile(m_context->device, &buffer, bytesToRead, &bytesRead, &m_context->readOverlapped)) {
|
||||||
emit dataReceived(c);
|
if (bytesRead == 1) {
|
||||||
|
emit dataReceived(buffer[0]);
|
||||||
|
} else {
|
||||||
|
emit dataReceived(QByteArray(buffer, bytesRead));
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
const DWORD readError = GetLastError();
|
const DWORD readError = GetLastError();
|
||||||
@@ -540,7 +553,11 @@ int WinReaderThread::tryRead()
|
|||||||
emit error(QString::fromLatin1("GetOverlappedResult failed: %1").arg(winErrorMessage(GetLastError())));
|
emit error(QString::fromLatin1("GetOverlappedResult failed: %1").arg(winErrorMessage(GetLastError())));
|
||||||
return -3;
|
return -3;
|
||||||
}
|
}
|
||||||
emit dataReceived(c);
|
if (bytesRead == 1) {
|
||||||
|
emit dataReceived(buffer[0]);
|
||||||
|
} else {
|
||||||
|
emit dataReceived(QByteArray(buffer, bytesRead));
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -787,10 +804,9 @@ bool TrkDevice::open(const QString &port, QString *errorMessage)
|
|||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
connect(d->readerThread.data(), SIGNAL(dataReceived(char)),
|
connect(d->readerThread.data(), SIGNAL(dataReceived(char)),
|
||||||
this, SLOT(dataReceived(char)), Qt::QueuedConnection);
|
this, SLOT(dataReceived(char)), Qt::QueuedConnection);
|
||||||
#else
|
#endif
|
||||||
connect(d->readerThread.data(), SIGNAL(dataReceived(QByteArray)),
|
connect(d->readerThread.data(), SIGNAL(dataReceived(QByteArray)),
|
||||||
this, SLOT(dataReceived(QByteArray)), Qt::QueuedConnection);
|
this, SLOT(dataReceived(QByteArray)), Qt::QueuedConnection);
|
||||||
#endif
|
|
||||||
d->readerThread->start();
|
d->readerThread->start();
|
||||||
|
|
||||||
d->writerThread = QSharedPointer<WriterThread>(new WriterThread(d->deviceContext));
|
d->writerThread = QSharedPointer<WriterThread>(new WriterThread(d->deviceContext));
|
||||||
|
|||||||
Reference in New Issue
Block a user