PDA

View Full Version : error QIODevice::read called with maxSize <0



sanjuchopracool
20th January 2012, 15:43
Qextserialport error QIODevice::read called with maxSize <0
I am using qextserialport to get some data from serial port , everything is working but i am getting

QIODevice::read called with maxSize <0
QIODevice::read called with maxSize <0[/SIZE]

again and again
my files are as



main.cpp is

#include <QtCore/QCoreApplication>
#include"receiveport.h"
//#include<qextserialport.h>
#include<QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QextSerialPort *port =new QextSerialPort ;
port->setBaudRate(BAUD19200);
port->setDataBits(DATA_8);
port->setFlowControl(FLOW_OFF);
port->setStopBits(STOP_1);
port->setParity(PAR_NONE);
if(port->open(QIODevice::ReadWrite))
qDebug()<<"Successfully opened the port";
else
qDebug()<<"Failed";
ReceivePort *myport =new ReceivePort(port);
myport->startReceiving();
myport->start();
return a.exec();
}




receiveport.h is

#ifndef RECEIVEPORT_H
#define RECEIVEPORT_H
#include "files/qextserialport.h"
#include <QMutex>
#include<QDebug>
#include<QThread>
class ReceivePort :public QThread
{
Q_OBJECT
private:

bool stopped ;
QextSerialPort *receiveport ;
QMutex receivemutex ;

public:
ReceivePort(QextSerialPort *port);
void stopReceiving();
void startReceiving();

protected:
void run();
signals:
void BytesReceived_signal(const QByteArray &data);
};

#endif // RECEIVEPORT_H





receiveport.cpp is

#include "receiveport.h"

ReceivePort::ReceivePort(QextSerialPort *port)
{
receiveport = port ;
stopped = false ; //initially the receiving is disabled
}
void ReceivePort::run()
{
int numbytes = 0 ;
char data[1024] ;
QByteArray receivedData ;
while(1)
{
if(stopped)
{
stopped =false ;
break;
}
numbytes = receiveport->bytesAvailable();
if(numbytes)
{
receivemutex.lock();
receiveport->read(data,numbytes);
receivedData =data ;
receivemutex.unlock();
emit BytesReceived_signal(receivedData);
qDebug()<<data;
}
}

}
void ReceivePort::startReceiving()
{
stopped =false ;
}
void ReceivePort::stopReceiving()
{
stopped = true ;
}



Please sort out the problem and tell me how can i avoid it.

wysota
20th January 2012, 16:18
What is the value of numbytes in line #20 of receiveport.cpp?

sanjuchopracool
20th January 2012, 16:48
if there is some data availabe at serial port it would be positive integer otherwise it would be zero

Lesiok
20th January 2012, 17:43
My private opinion : this is bad habit in a situation such as this.
if(numbytes)
Better is :
if(numbytes > 0)

wysota
20th January 2012, 17:58
if there is some data availabe at serial port it would be positive integer otherwise it would be zero

I'm asking what is and not what would be. In my opinion it is -1 or some other negative value.