Receive Data QserialPort
-
Hello,
I'm trying to recieve data from a GPS; here is my code below., but i receive anything
I don't know where the problem lies
Thank you.
serialPort = new QSerialPort(devPath,this); serialPort->setBaudRate(115200); serialPort->setDataBits(QSerialPort::Data8); serialPort->setParity(QSerialPort::NoParity); serialPort->setStopBits(QSerialPort::OneStop); serialPort->setFlowControl(QSerialPort::NoFlowControl); if (!serialPort->open(QIODevice::ReadWrite)) { log_warning("sensors","failed to open device file \"%s\", GPS measures will be unavailable",qPrintable(serialPort->portName())); return; } QObject::connect(this->serialPort,SIGNAL(readyRead()),this,SLOT(readUbxNavPos())); QObject::connect(serialPort, &::QSerialPort::errorOccurred,this,&Gps::handleError); pollingTimer->start(200);
}
void Gps::handleError(QSerialPort::SerialPortError error)
{
if (error == QSerialPort::ResourceError){
qDebug()<<"Handle Error"<<error;
serialPort->close();
}
}
void Gps::readUbxNavPos()
{
rcvData.append(serialPort->readAll());
qDebug()<<"DataGps"<<rcvData;
} -
@manel-sam said in Receive Data QserialPort:
void Gps::handleError(QSerialPort::SerialPortError error)
{
if (error == QSerialPort::ResourceError){
qDebug()<<"Handle Error"<<error;
serialPort->close();
}Do you get any other error here?
Also, why do you use the old connect style for readyRead()?
-
@manel-sam said in Receive Data QserialPort:
So, Could you please give me the new style of Redyread
One of many references is https://wiki.qt.io/New_Signal_Slot_Syntax
The "new" style is already ten-odd years old, it's really time all these examples on the web with old style got removed/updated, but I guess it's never going to happen... :(QObject::connect(serialPort, &::QSerialPort::errorOccurred,this,&Gps::handleError);
So either you do know about "new style", or you have added this to your code. I am surprised this line as-is compiles OK, but maybe it does.
No, I don't have any errors displayed.
@jsulm asked you "Do you get any other error here?", because your code as shown would not display anything other than a
ResourceError
, so how would you know? -
@manel-sam said in Receive Data QserialPort:
and no error is displayed
You're only checking for one of possible errors here:
void Gps::handleError(QSerialPort::SerialPortError error) { if (error == QSerialPort::ResourceError){ qDebug()<<"Handle Error"<<error; serialPort->close(); } }
That's why I asked whether you get any other errors. Can you change it to:
void Gps::handleError(QSerialPort::SerialPortError error) { qDebug() << "Error occured" << error; if (error == QSerialPort::ResourceError){ qDebug()<<"Handle Error"<<error; serialPort->close(); } }
?
-
@manel-sam
Can it be you need to send it something for the GPS to reply ? -
It's coming back to me. I once had the same problem with an IMU sensor that I could not solve under windows.
But once under linux with the same code, I receive the data perfectly.
Same thing, I just tested under linux for the GPS, I receive the data but not under Windows.
To this day I don't know where the problem comes from