Home > Enterprise >  "device not open" - QIODevice
"device not open" - QIODevice

Time:12-20

You see a code sector to open a device with QIODevice :

`

void MACH3::on_pushButton_clicked()
{
    serial = new QSerialPort(this);

    serial->setBaudRate(QSerialPort::Baud115200);
    serial->setDataBits(QSerialPort::Data8);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);
    serial->setFlowControl(QSerialPort::NoFlowControl);

    serial->open(QIODevice::ReadWrite);

    QString time = QTime::currentTime().toString();
    QString elde = ui->data->toPlainText();
    QString eldetut = ui->data->toPlainText();
    QByteArray inBytes;
    const char *cStrData;
    inBytes = elde.toUtf8();
    cStrData = inBytes.constData();
    QString qtStrData;
    qtStrData = QString::fromUtf8(cStrData);

    QString gelen = serial->readAll();

    if(gelen.length()>0)
    {
        QString eldetut = ui->data->toPlainText();
        QString time2 = QTime::currentTime().toString();
        ui->data->setText(eldetut "\n" time2   "\n"   "received-> " gelen   "\n");
    }
}

`

Could you check the code above?

As failure message came:

"QIODevice::read (QSerialPort): device not open"

CodePudding user response:

You should check the return value of the call to open() call according to the documentation: https://doc.qt.io/qt-6/qserialport.html#open

I think the main problem is that you did not specify a port name in your code (neither via the constructor nor via setPortName()/setPort())

CodePudding user response:

Wished Port

I am not sure - the device isn't found in the program. In addition, I do not know how can I read data from it yet.

  • Related