help solve QIODevice::write (QSerialPort): device not open
-
hello everyone
I've been trying to write data sent by Arduino it says QIODevice::write (QSerialPort): device not open please help me.#include "mainwindow.h"
#include <QPushButton>
#include "ui_mainwindow.h"
#include "form.h"
#include <QtSerialPort/QSerialPort>
#include <QDebug>
#include <QList>
#include <QSerialPortInfo>
#include <QMessageBox>MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{ui->setupUi(this); this->setStyleSheet("MainWindow {background-image:url(:/img/background.png)}"); arduino = new QSerialPort(this); setWindowTitle("ชุดทดลองควบคุมมอเตอร์ด้วยภาษาC++"); resize(500,400); QPushButton *btn1 = new QPushButton(this); btn1->setText("Start"); btn1->setFont(QFont("Time", 14)); btn1->setGeometry(90, 470, 111, 41); btn1->setStyleSheet("background-color: #a5a5a5"); connect(btn1, SIGNAL(clicked()), this, SLOT(on_Start_clicked())); QPushButton *btn2 = new QPushButton(this); btn2->setText("Stop"); btn2->setFont(QFont("Time", 14)); btn2->setGeometry(240, 470, 111, 41); btn2->setStyleSheet("background-color: #a5a5a5"); connect(btn2, SIGNAL(clicked()), this, SLOT(on_Stop_clicked())); QPushButton *btn3 = new QPushButton(this); btn3->setText("Properties"); btn3->setFont(QFont("Time", 14)); btn3->setGeometry(90, 540, 111, 41); btn3->setStyleSheet("background-color: #a5a5a5"); btn3->show(); connect(btn3, SIGNAL(clicked()), this, SLOT(on_pushbutton_clicked())); QPushButton *btn4 = new QPushButton(this); btn4->setText("Members"); btn4->setFont(QFont("Time", 14)); btn4->setGeometry(240, 540, 111, 41); btn4->setStyleSheet("background-color: #a5a5a5"); btn4->show(); connect(btn4, SIGNAL(clicked()), this, SLOT(on_pushbutton1_clicked())); spinbox = new QDoubleSpinBox(this); spinbox->setGeometry(170, 390, 111, 41); spinbox->setStyleSheet("background-color: #a5a5a5"); spinbox->setMaximum(50); connect(spinbox, SIGNAL(valueChanged()), this, SLOT(on_spinbox1())); arduino_is_available = false; arduino_port_name = ""; arduino = new QSerialPort; foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){ if(serialPortInfo.hasVendorIdentifier() && serialPortInfo.hasProductIdentifier()){ if(serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id){ if(serialPortInfo.productIdentifier() == arduino_uno_product_id){ arduino_port_name = serialPortInfo.portName(); arduino_is_available = true; } } } } if(arduino_is_available){ QMessageBox::information(this, "Serial Port connected", "Found the arduino port"); arduino = new QSerialPort(this); arduino->setPortName(arduino_port_name); arduino->open(QSerialPort::ReadWrite); arduino->open(QIODevice::ReadWrite); arduino->setBaudRate(QSerialPort::Baud115200); arduino->setDataBits(QSerialPort::Data8); arduino->setFlowControl(QSerialPort::NoFlowControl); arduino->setParity(QSerialPort::NoParity); arduino->setStopBits(QSerialPort::OneStop); }else{ qDebug() << "Couldn't find the correct port for the arduino.\n"; QMessageBox::information(this, "Serial Port Error", "serial port not conected to arduino."); } }MainWindow::~MainWindow()
{
if(arduino->isOpen()){arduino->close(); } delete ui;}
void MainWindow::on_pushbutton_clicked()
{
form = new Form(this);
form->show();
this->hide();
}void MainWindow::on_pushbutton1_clicked()
{
member = new Member(this);
member->show();
this->hide();
}void MainWindow::on_Start_clicked()
{
arduino->open(QIODevice::ReadWrite);
arduino->write("456");
qDebug() << "Couldn't write to serial!";
}void MainWindow::on_Stop_clicked()
{
if(arduino->isWritable()){
arduino->write("456");
}else{
qDebug() << "Couldn't write to serial!";
}
}void MainWindow::spinbox1()
{
spinbox-> value();
}void MainWindow::readSerial()
{}
-
hello everyone
I've been trying to write data sent by Arduino it says QIODevice::write (QSerialPort): device not open please help me.#include "mainwindow.h"
#include <QPushButton>
#include "ui_mainwindow.h"
#include "form.h"
#include <QtSerialPort/QSerialPort>
#include <QDebug>
#include <QList>
#include <QSerialPortInfo>
#include <QMessageBox>MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{ui->setupUi(this); this->setStyleSheet("MainWindow {background-image:url(:/img/background.png)}"); arduino = new QSerialPort(this); setWindowTitle("ชุดทดลองควบคุมมอเตอร์ด้วยภาษาC++"); resize(500,400); QPushButton *btn1 = new QPushButton(this); btn1->setText("Start"); btn1->setFont(QFont("Time", 14)); btn1->setGeometry(90, 470, 111, 41); btn1->setStyleSheet("background-color: #a5a5a5"); connect(btn1, SIGNAL(clicked()), this, SLOT(on_Start_clicked())); QPushButton *btn2 = new QPushButton(this); btn2->setText("Stop"); btn2->setFont(QFont("Time", 14)); btn2->setGeometry(240, 470, 111, 41); btn2->setStyleSheet("background-color: #a5a5a5"); connect(btn2, SIGNAL(clicked()), this, SLOT(on_Stop_clicked())); QPushButton *btn3 = new QPushButton(this); btn3->setText("Properties"); btn3->setFont(QFont("Time", 14)); btn3->setGeometry(90, 540, 111, 41); btn3->setStyleSheet("background-color: #a5a5a5"); btn3->show(); connect(btn3, SIGNAL(clicked()), this, SLOT(on_pushbutton_clicked())); QPushButton *btn4 = new QPushButton(this); btn4->setText("Members"); btn4->setFont(QFont("Time", 14)); btn4->setGeometry(240, 540, 111, 41); btn4->setStyleSheet("background-color: #a5a5a5"); btn4->show(); connect(btn4, SIGNAL(clicked()), this, SLOT(on_pushbutton1_clicked())); spinbox = new QDoubleSpinBox(this); spinbox->setGeometry(170, 390, 111, 41); spinbox->setStyleSheet("background-color: #a5a5a5"); spinbox->setMaximum(50); connect(spinbox, SIGNAL(valueChanged()), this, SLOT(on_spinbox1())); arduino_is_available = false; arduino_port_name = ""; arduino = new QSerialPort; foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){ if(serialPortInfo.hasVendorIdentifier() && serialPortInfo.hasProductIdentifier()){ if(serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id){ if(serialPortInfo.productIdentifier() == arduino_uno_product_id){ arduino_port_name = serialPortInfo.portName(); arduino_is_available = true; } } } } if(arduino_is_available){ QMessageBox::information(this, "Serial Port connected", "Found the arduino port"); arduino = new QSerialPort(this); arduino->setPortName(arduino_port_name); arduino->open(QSerialPort::ReadWrite); arduino->open(QIODevice::ReadWrite); arduino->setBaudRate(QSerialPort::Baud115200); arduino->setDataBits(QSerialPort::Data8); arduino->setFlowControl(QSerialPort::NoFlowControl); arduino->setParity(QSerialPort::NoParity); arduino->setStopBits(QSerialPort::OneStop); }else{ qDebug() << "Couldn't find the correct port for the arduino.\n"; QMessageBox::information(this, "Serial Port Error", "serial port not conected to arduino."); } }MainWindow::~MainWindow()
{
if(arduino->isOpen()){arduino->close(); } delete ui;}
void MainWindow::on_pushbutton_clicked()
{
form = new Form(this);
form->show();
this->hide();
}void MainWindow::on_pushbutton1_clicked()
{
member = new Member(this);
member->show();
this->hide();
}void MainWindow::on_Start_clicked()
{
arduino->open(QIODevice::ReadWrite);
arduino->write("456");
qDebug() << "Couldn't write to serial!";
}void MainWindow::on_Stop_clicked()
{
if(arduino->isWritable()){
arduino->write("456");
}else{
qDebug() << "Couldn't write to serial!";
}
}void MainWindow::spinbox1()
{
spinbox-> value();
}void MainWindow::readSerial()
{}
@Ryuzo
I see at least 3 occurrences ofarduino = new QSerialPort(...)I also see at least one place where this
arduinoserial port is opened twice in a row one immediately after the other.So it is hardly surprising if at least one time it reports that the serial port is not open.
-
@JonB said in help solve QIODevice::write (QSerialPort): device not open:
see at least 3 occurrences of
I fixed it but it's still not good. this code
#include "mainwindow.h"
#include <QPushButton>
#include "ui_mainwindow.h"
#include "form.h"
#include <QtSerialPort/QSerialPort>
#include <QDebug>
#include <QList>
#include <QSerialPortInfo>
#include <QMessageBox>MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{ui->setupUi(this); this->setStyleSheet("MainWindow {background-image:url(:/img/background.png)}"); setWindowTitle("ชุดทดลองควบคุมมอเตอร์ด้วยภาษาC++"); resize(500,400); QPushButton *btn1 = new QPushButton(this); btn1->setText("Start"); btn1->setFont(QFont("Time", 14)); btn1->setGeometry(90, 470, 111, 41); btn1->setStyleSheet("background-color: #a5a5a5"); connect(btn1, SIGNAL(clicked()), this, SLOT(on_Start_clicked())); QPushButton *btn2 = new QPushButton(this); btn2->setText("Stop"); btn2->setFont(QFont("Time", 14)); btn2->setGeometry(240, 470, 111, 41); btn2->setStyleSheet("background-color: #a5a5a5"); connect(btn2, SIGNAL(clicked()), this, SLOT(on_Stop_clicked())); QPushButton *btn3 = new QPushButton(this); btn3->setText("Properties"); btn3->setFont(QFont("Time", 14)); btn3->setGeometry(90, 540, 111, 41); btn3->setStyleSheet("background-color: #a5a5a5"); btn3->show(); connect(btn3, SIGNAL(clicked()), this, SLOT(on_pushbutton_clicked())); QPushButton *btn4 = new QPushButton(this); btn4->setText("Members"); btn4->setFont(QFont("Time", 14)); btn4->setGeometry(240, 540, 111, 41); btn4->setStyleSheet("background-color: #a5a5a5"); btn4->show(); connect(btn4, SIGNAL(clicked()), this, SLOT(on_pushbutton1_clicked())); spinbox = new QDoubleSpinBox(this); spinbox->setGeometry(170, 390, 111, 41); spinbox->setStyleSheet("background-color: #a5a5a5"); spinbox->setMaximum(50); connect(spinbox, SIGNAL(valueChanged()), this, SLOT(on_spinbox1())); arduino_is_available = false; arduino_port_name = ""; foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){ if(serialPortInfo.hasVendorIdentifier() && serialPortInfo.hasProductIdentifier()){ if(serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id){ if(serialPortInfo.productIdentifier() == arduino_uno_product_id){ arduino_port_name = serialPortInfo.portName(); arduino_is_available = true; } } } } if(arduino_is_available){ QMessageBox::information(this, "Serial Port connected", "Found the arduino port"); arduino = new QSerialPort(this); arduino->setPortName(arduino_port_name); arduino->open(QSerialPort::ReadWrite); arduino->open(QIODevice::ReadWrite); arduino->setBaudRate(QSerialPort::Baud9600); arduino->setDataBits(QSerialPort::Data8); arduino->setFlowControl(QSerialPort::NoFlowControl); arduino->setParity(QSerialPort::NoParity); arduino->setStopBits(QSerialPort::OneStop); }else{ qDebug() << "Couldn't find the correct port for the arduino.\n"; QMessageBox::information(this, "Serial Port Error", "serial port not conected to arduino."); } }MainWindow::~MainWindow()
{
if(arduino->isOpen()){
arduino->close();
}
delete ui;}
void MainWindow::on_pushbutton_clicked()
{
form = new Form(this);
form->show();
this->hide();
}void MainWindow::on_pushbutton1_clicked()
{
member = new Member(this);
member->show();
this->hide();
}void MainWindow::on_Start_clicked()
{arduino->write("456"); qDebug() << "Couldn't write to serial!";}
void MainWindow::on_Stop_clicked()
{
if(arduino->isWritable()){
arduino->write("456");
}else{
qDebug() << "Couldn't write to serial!";
}
}void MainWindow::spinbox1()
{
spinbox-> value();
}void MainWindow::readSerial()
{}

@JonB said in help solve QIODevice::write (QSerialPort): device not open:
@Ryuzo
I see at least 3 occurrences ofarduino = new QSerialPort(...)I also see at least one place where this
arduinoserial port is opened twice in a row one immediately after the other.So it is hardly surprising if at least one time it reports that the serial port is not open.
-
@Ryuzo
I see at least 3 occurrences ofarduino = new QSerialPort(...)I also see at least one place where this
arduinoserial port is opened twice in a row one immediately after the other.So it is hardly surprising if at least one time it reports that the serial port is not open.
@Ryuzo
Please put blocks of code like this in the forum's Code tags to help those trying to help you.@JonB said in help solve QIODevice::write (QSerialPort): device not open:
I also see at least one place where this arduino serial port is opened twice in a row one immediately after the other.
Unaddressed.
You do not check the return result from the
open()s, so if they fail (as I am sure they do) you will ignore this and try to write to them anyway just as the error tells you, and you do not look aterror(). Error checking is fundamental part of development.You also show a bunch of other error messages which you have not addressed.
-
you need to open that port
arduino = new QSerialPort(this); arduino->setPortName(arduino_port_name); arduino->setBaudRate(QSerialPort::Baud115200); arduino->setDataBits(QSerialPort::Data8); arduino->setFlowControl(QSerialPort::NoFlowControl); arduino->setParity(QSerialPort::NoParity); arduino->setStopBits(QSerialPort::OneStop); arduino->open(QIODevice::ReadWrite) -
you need to open that port
arduino = new QSerialPort(this); arduino->setPortName(arduino_port_name); arduino->setBaudRate(QSerialPort::Baud115200); arduino->setDataBits(QSerialPort::Data8); arduino->setFlowControl(QSerialPort::NoFlowControl); arduino->setParity(QSerialPort::NoParity); arduino->setStopBits(QSerialPort::OneStop); arduino->open(QIODevice::ReadWrite)Hi,
@Emre-MUTLU said in help solve QIODevice::write (QSerialPort): device not open:
you need to open that port
arduino = new QSerialPort(this); arduino->setPortName(arduino_port_name); arduino->setBaudRate(QSerialPort::Baud115200); arduino->setDataBits(QSerialPort::Data8); arduino->setFlowControl(QSerialPort::NoFlowControl); arduino->setParity(QSerialPort::NoParity); arduino->setStopBits(QSerialPort::OneStop); arduino->open(QIODevice::ReadWrite)You are missing error checking and reporting.