[Solved] QSerialPort close() throws Unknown error
-
Hi,
Im talking with my GSM-Modem with QSerialPort.
At the beginning i do the following:
@QSerialPort* pSerialPort = new QSerialPort(this);@I have a "start()" Funktion within i do this:
@pSerialPort->setPortName(sPortName);
if (pSerialPort->open(QIODevice::ReadWrite)) { if (pSerialPort->setBaudRate(QSerialPort::Baud19200) && pSerialPort->setDataBits(QSerialPort::Data8) && pSerialPort->setParity(QSerialPort::NoParity) && pSerialPort->setStopBits(QSerialPort::OneStop) && pSerialPort->setFlowControl(QSerialPort::NoFlowControl)) { connect(pSerialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(serialPortError(QSerialPort::SerialPortError))); connect(pSerialPort, SIGNAL(readyRead()), this, SLOT(serialRead())); stateMaschine(START); emit info("GsmCommunication::start(): COM-Schnittstelle erfolgreich initialisiert", GlobalVars::SI_COM_PORT_INIT_OK); pSmsCheckTimer->start(); } else { emit error("GsmCommunication::start(): Kann COM-Schnittstelle nicht initialisieren"); } } else { emit error("GsmCommunication::start(): Kann COM-Schnittstelle nicht oeffnen"); }@
I have a "stop()"-function within i do this:
@pSerialPort->clear();
pSerialPort->close();@And i have a reInit()-function, within i do this:
@stop();
start();@Everything works fine, but i get "Unknown error" from QSerialPort when i call my reInit()-Function. Seems like QSerialPort.close() causes the error, why is that?
-
-
Can you try it with the Terminal example (from the QtSerialPort package)?
I mean just do clicks to the "connect" and "disconnect" buttons. -
What OS is used?
-
What model of modem is used (what is device)?
-
What version of QSerialPort you use? From the Git?
-
The QObject::connect should be done once after creation of the QSerialPort's object, like:
@
QSerialPort* pSerialPort = new QSerialPort(this);connect(pSerialPort, SIGNAL(error(QSerialPort::SerialPortError)),
this, SLOT(serialPortError(QSerialPort::SerialPortError)));connect(pSerialPort, SIGNAL(readyRead()), this, SLOT(serialRead()));
@ -
-
Ty for ur answer,
I tried it with the terminal example, i added the following code to see any kind of errors:@void MainWindow::handleError(QSerialPort::SerialPortError error)
{
qDebug() << "ERROR: " << error; //I added this line
if (error == QSerialPort::ResourceError) {
QMessageBox::critical(this, tr("Critical Error"), serial->errorString());
closeSerialPort();
}
}@
Since then also the terminal-example shows "ERROR: 0", but not as I thought when close() is called, it appears when I click the "connect" button, also in my Program it appears in my start() function. I added "pSerialPort->clear()" just before "open()" is called, since then i get the following Error-String:
"Das Handle ist ungültig." Which i would translate in english as "The Handle is not valid". Without the clear() its "Unknown Error".The OS is Microsoft Windows 7 32bit.
The Device is a CT63 GSM-Modem conneted with a USB->RS232 connector.
I use the build in Version of QSerialPort in QT 5.1.1
(#include <QtSerialPort/QSerialPort>)
I fixed the issue of wrong placed connect(), but that doesnt appear to make any changes. -
bq. Since then also the terminal-example shows “ERROR: 0”
It is NoError.
bq. “pSerialPort->clear()” just before “open()” is called, since then i get the following Error-String
It is because your port already closed, so the clear() method can't to be success.
bq. USB->RS232 connector
What is connector model?
bq. I use the build in Version of QSerialPort in QT 5.1.1
Please download and compile the Git version of QtSerialPort and try check again.
Also:
-
Please provide a minimal compilable project that reproduce a problem (it is better - console if possible).
-
You can try to do debugging himself to help us.
-
-
I changed my Modification in the terminal example:
@void MainWindow::handleError(QSerialPort::SerialPortError error)
{
qDebug() << "ERROR: " << error << "ErrorString: " << serial->errorString();
if (error == QSerialPort::ResourceError) {
QMessageBox::critical(this, tr("Critical Error"), serial->errorString());
closeSerialPort();
}
}@
Now the terminal example shows the following in debugging console:
*ERROR: 0 ErrorString: "Unknown error" *
Thats exactly the same behaivior as in my Program.The USB->RS232 converter is type U232-P9 made in Taiwan (-: dont have more information.
I will have to read about the Git Version and how to compile it, never heard about that so far.
-
bq. *ERROR: 0 ErrorString: “Unknown error”
This is not an error. Just ignore/skip SerialPortError::NoError error code.. :)