[Solved] QSerialPort - error message: QIODevice::read: device not open
-
I have STM Discovery board. It sends some dummy data over virtual COM port. When I start terminal (Tera Term), I can choose "COM5: STMicroelectronics Virtual COM Port (COM5)". And I see dummy data displayed in terminal.
Now, I want to create application which will read data from that serial port (later I will process that data and display it in some other format - but it is irrelevant for now).
But, there is a problem with serial port. I got an error "QIODevice::read: device not open".
Ofcourse: when I start my application, TeraTerm terminal is closed.
I am using QtCreator (3.2.1) in Windows environment.I noticed that function "open" always return "false".
This is a my code segment:
@
QSerialPort *serial1;
char buffer[50];MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);serial1 = new QSerialPort(this); serial1->setPortName("COM5"); if (serial1->open(QIODevice::ReadWrite)) { qDebug("TRUE") ; serial1->setBaudRate(QSerialPort::Baud9600); serial1->setDataBits(QSerialPort::Data8); serial1->setParity(QSerialPort::NoParity); serial1->setStopBits(QSerialPort::OneStop); serial1->setFlowControl(QSerialPort::NoFlowControl); } else { qDebug("FALSE") ; } serial1->read(buffer, 50); ui->plainTextEdit->insertPlainText("QWERTY!\n");
}
@What am I doing wrong?
-
You should to know a reason why a port is not opened. So, for this need to print out an error string or an error code.
-
This are error code and error string.
They are the same regardles what I put as parameter in function "open". I tried: QIODevice::ReadWrite, QIODevice::ReadOnly, QIODevice::WriteOnly. I even tried QSerialPort::ReadWrite, QSerialPort::ReadOnly,QSerialPort::WriteOnlyerror code = 10
error string = "The parameter is incorrect." -
kuzulis is correct, there are a couple class functions for getting the last error which would be your port error.
Use errorString() and print it out to qDebug() or use error() and check the enumeration value which can be found in the documentation for QSerialPort.
Edit:
QSerialPort::UnsupportedOperationError - 10 - The requested device operation is not supported or prohibited by the running operating system.is QSerialPort *serial in your MainWindow header?
-
I do not undestand this:
is QSerialPort *serial in your MainWindow header?This is all code from mainwindow.h
@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private:
Ui::MainWindow *ui;
};#endif // MAINWINDOW_H
@ -
You are using the allocated pointer as a static parameter. I am not sure if this is your problem, but try putting them as private members in your main window class header. So,
@
class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private:
Ui::MainWindow *ui;
QSerialPort *serial1;
char buffer[50];
};
@ -
I put it as private members, but it did not helped.
Same error as before:
error code = 10
error string = "The parameter is incorrect."Also, I did something with QSerialPortInfo, and I got this
Number of serial ports: 3
Name: "COM5"
Description: "STMicroelectronics Virtual COM Port"
Manufactures: "STMicroelectronics."
Unable to open port, error code 10It seems virtual serial port exist, but I still can not open it...
-
Probably that a port (a driver of port) does not support of some things at opening. You need to check yourself what is it.. E.g. you should to debug of QSerialPortPrivate::open() and QSerialPortPrivate::initialize() method. This problem can be resolve only with debugging on your side.
UPD: or, you can provide a team-viewer session and then I can try to help.
-
I was unable to debug those functions (QSerialPortPrivate::open() and QSerialPortPrivate::initialize()). To be honest, I did not see them. I see only QSerialPort::open(). Without this "Private" part.
In debug mode, when I use "step into" during execution of "open" I can get only assembler code. And it is quite difficult to understand and debug...
-
You should do debugging from the QtSerialPort sources.
Just try to:
- download a sources
- open a project from the QtCreator
- choose the "debug" target
- add/write to the make step options the "all" text (this allow to build the examples)
- try to rebuild and be convinced that an examples also has been compiled
- choose from the QtCreator the "terminal" example to run
- setup a breack point on QSerialPortPrivate::open()
- run the "terminal" application (from QtCreator)
- open in the "terminal" your serial port and to click on "connect" button
- go to break point..
-
I do not know what happened...
Suddenly, code is working without any problem.
There was no major change in code...@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSerialPort>
#include <QDebug>MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);initComPort(); connect(serial1, SIGNAL(readyRead()), this, SLOT(readDataFromDiscovery()));
}
MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::readDataFromDiscovery()
{
serial1->readLine(buffer,50);
ui->plainTextEdit->insertPlainText(buffer);
}void MainWindow::initComPort(void)
{
serial1 = new QSerialPort(this);
serial1->setPortName("COM5");if (serial1->open(QSerialPort::ReadOnly)) { qDebug("SERIAL PORT - OPENED") ; serial1->setBaudRate(QSerialPort::Baud9600); serial1->setDataBits(QSerialPort::Data8); serial1->setParity(QSerialPort::NoParity); serial1->setStopBits(QSerialPort::OneStop); serial1->setFlowControl(QSerialPort::NoFlowControl); } else { qDebug("SERIAL PORT - NOT OPENED") ; qDebug() << "error code = " << serial1->error(); qDebug() << "error string = " << serial1->errorString(); }
}@