QIODevice::write: device not open



  • Hi

    I have a problem with setting up my connection to a COM port have been stuck for awhile now hope you can help me.

    Do I have to have my mbed controller connected for it to work ?

    when i run my code it says:

    bq.
    Serial NOT Opened
    error code = 1
    error string = "No such file or directory"
    QIODevice::write: device not open

    the code I have is:
    @

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "modify_scenario.h"
    #include <QSerialPort>
    #include <QDebug>
    #include <QPushButton>
    #include <QWidget>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {

     ui->setupUi(this);
    
     serial = new QSerialPort(this);
     serial->setPortName("COM2");
     if(serial->open(QIODevice::ReadWrite))
     {
         qDebug("Serial Opened");
         serial->setBaudRate(QSerialPort::Baud9600);
         serial->setDataBits(QSerialPort::Data8);
         serial->setParity(QSerialPort::NoParity);
         serial->setStopBits(QSerialPort::OneStop);
         serial->setFlowControl(QSerialPort::NoFlowControl);
     }
     else
     {
         qDebug ("Serial NOT Opened");
         qDebug() << "error code = " << serial->error();
         qDebug() << "error string = " << serial->errorString();
     }
    
     connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
    

    @

    for the connect: (serialReceived())

    @
    void MainWindow::serialReceived()
    {
    QByteArray ba;
    ba = serial -> readAll();
    ui->label->setText(ba);
    qDebug() << ba;
    }
    @

    and in the header I have in private slots

    @

    void serialReceived();
    @

    and in private

    @

    private:
    Ui::MainWindow *ui;
    Modify_Scenario *modify;
    int counter;
    QSerialPort *serial;
    @

    really hope you can help me :)

    Regards Alex.



  • This means that COM2 is not exists on your PC (you can check it in Device Manager).



  • Hi Kuzulis

    Yes that is correct, checked it already.

    does that mean I have to setup a virtual com port?
    and how do I do that ?

    if I input my device to my computer, then my computer detects a COM port, but wanted to test it without the device.

    Alex.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Do you mean that you would like to make your code run even when there's no serial port available ?



  • used Eltima's virtual COM port to setup a virtual port and tested it with tera term and it works now :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.