QSerialPort::open causing QSerialPort::NotOpen error although returning True and working fine
-
Hello,
in my app I'm using a serial port like this:port_->setPortName(name); port_->open(QIODevice::ReadWrite)
Opening the port like this returns True like expected and I'm able to work with the port later. However this function call causes emitting of a
QSerial::errorOccurred
signal with an errorQSerialPort::NotOpen
("This error occurs when an operation is executed that can only be successfully performed if the device is open."). I commented out every other function call which works with the port, left just these two statements and an error handling slot. The signal is still emitted, which makes no sense to me. According to the docs, an error occures only if open returns False. I'm using Qt 5.10. Can anyone explain this behaviour to me? -
Hi
Could you try with
http://doc.qt.io/qt-5/qtserialport-terminal-example.html
and see if that also says it ? -
-
- Is it UnknownError or NotOpenError?
- What is OS do you use (version, kernel)?
- What serial port (type, chip) do you use?
-
@kuzulis If I'm catching the
errorOccured
signal in my error handling slot (slot with a switch for every QSerialPort error), it's catched as NotOpen error. If I check the error like this:if (port_->open(QIODevice::ReadWrite)) { qDebug() << port_->errorString() << port_->error(); return true; }
It returns
"Unknown error" QSerialPort::SerialPortError<NoError>
.I'm using Win7, kernel version 6.1.7601.23915, USB serial port (COM6) (USB 2.0). I don't know much about the device itself, only that I had to manually install fdti driver for it (downloaded form the official pages).
@JonB
I would expect "No error", not "Unknown error". But what bothers me more is thisNotOpen
error. -
Hi, @sykac
please show us more of your code, this seems to me, like you have a local
QSerialPort *port_
variable that overwrites the pointer to the member variable you, may have.That would explain why the port opens succesfully but, when you write to it, your try to access the one thats not open.
-
@sykac
Given that theopen()
did not return false, I'd expecterrorString()
just as much to return "some undefined left over value" as your expected "No error", as i don't see the docs state what it will return if no error has occurred.For your problem, though, have a look at https://stackoverflow.com/questions/34780535/qserialport-error-signal-on-open-but-open-returns-true. Maybe the "not open" is something internal?
Also http://doc.qt.io/qt-5/qserialport.html#error-prop:
The error code is set to the default QSerialPort::NoError after a call to clearError()
Perhaps call
clearError()
before you start?Finally, you could not establish the
errorOccurred
slot until after theopen()
, as I don't think you actually need it till the port has been opened? -
@J-Hilk
Actually there isn't much to show, I really commented out everything else. With the object creation and connections this is it:serial_ = new QSerialPort(this); connect(serial_, SIGNAL(readyRead()), this, SLOT(handleReadyRead())); //the slot is empty connect(serial_, SIGNAL(errorOccurred(QSerialPort::SerialPortError)), this, SLOT(handleError(QSerialPort::SerialPortError))); // a switch for every error with no real functionality, just printing the case value serial_->setPortName(port_); if (serial_->open(QIODevice::ReadWrite)) { qDebug() << serial_->errorString() << serial_->error(); return true; }
And that's all, I don't access the port object anywhere else in the code. I also run the example from an above comment, the NotOpen error occures also there.
@JonB
Actually when I addclearError()
, theNotOpen
error fired twice. But I checked the links you posted and played with the switch in the errorHandle slot a little bit and I discovered thatQSerialPort::NoError
andQSerialPort::NotOpen
have the same enum value which is zero. If I implement the switch with both of them, it won't build saying "case value 0 already used". So I gues it's some kind of bug in Qt? Or how can this be explained? Because this is the cause of my problem,NoError
is fired asNotOpen
error. I noticed it only now because I didn't haveNoError
in the switch before as I didn't want to handleNoError
. -
@JonB
thats from QIODevice, the base class