Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] QSerialPort close() throws Unknown error
Qt 6.11 is out! See what's new in the release blog

[Solved] QSerialPort close() throws Unknown error

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 7.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi,
    Im talking with my GSM-Modem with QSerialPort.
    At the beginning i do the following:
    @QSerialPort* pSerialPort = new QSerialPort(this);@

    I have a "start()" Funktion within i do this:

    @pSerialPort->setPortName(sPortName);

    if (pSerialPort->open(QIODevice::ReadWrite))
    {
        if (pSerialPort->setBaudRate(QSerialPort::Baud19200)
                && pSerialPort->setDataBits(QSerialPort::Data8)
                && pSerialPort->setParity(QSerialPort::NoParity)
                && pSerialPort->setStopBits(QSerialPort::OneStop)
                && pSerialPort->setFlowControl(QSerialPort::NoFlowControl))
        {
            connect(pSerialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(serialPortError(QSerialPort::SerialPortError)));
            connect(pSerialPort, SIGNAL(readyRead()), this, SLOT(serialRead()));
            stateMaschine(START);
            emit info("GsmCommunication::start(): COM-Schnittstelle erfolgreich initialisiert", GlobalVars::SI_COM_PORT_INIT_OK);
            pSmsCheckTimer->start();
    
        }
        else
        {
            emit error("GsmCommunication::start(): Kann COM-Schnittstelle nicht initialisieren");
        }
    }
    else
    {
        emit error("GsmCommunication::start(): Kann COM-Schnittstelle nicht oeffnen");
    }@
    

    I have a "stop()"-function within i do this:

    @pSerialPort->clear();
    pSerialPort->close();@

    And i have a reInit()-function, within i do this:

    @stop();
    start();@

    Everything works fine, but i get "Unknown error" from QSerialPort when i call my reInit()-Function. Seems like QSerialPort.close() causes the error, why is that?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kuzulis
      Qt Champions 2020
      wrote on last edited by
      #2
      1. Can you try it with the Terminal example (from the QtSerialPort package)?
        I mean just do clicks to the "connect" and "disconnect" buttons.

      2. What OS is used?

      3. What model of modem is used (what is device)?

      4. What version of QSerialPort you use? From the Git?

      5. The QObject::connect should be done once after creation of the QSerialPort's object, like:

      @
      QSerialPort* pSerialPort = new QSerialPort(this);

      connect(pSerialPort, SIGNAL(error(QSerialPort::SerialPortError)),
      this, SLOT(serialPortError(QSerialPort::SerialPortError)));

      connect(pSerialPort, SIGNAL(readyRead()), this, SLOT(serialRead()));
      @

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Ty for ur answer,
        I tried it with the terminal example, i added the following code to see any kind of errors:

        @void MainWindow::handleError(QSerialPort::SerialPortError error)
        {
        qDebug() << "ERROR: " << error; //I added this line
        if (error == QSerialPort::ResourceError) {
        QMessageBox::critical(this, tr("Critical Error"), serial->errorString());
        closeSerialPort();
        }
        }@
        Since then also the terminal-example shows "ERROR: 0", but not as I thought when close() is called, it appears when I click the "connect" button, also in my Program it appears in my start() function. I added "pSerialPort->clear()" just before "open()" is called, since then i get the following Error-String:
        "Das Handle ist ungültig." Which i would translate in english as "The Handle is not valid". Without the clear() its "Unknown Error".

        The OS is Microsoft Windows 7 32bit.
        The Device is a CT63 GSM-Modem conneted with a USB->RS232 connector.
        I use the build in Version of QSerialPort in QT 5.1.1
        (#include <QtSerialPort/QSerialPort>)
        I fixed the issue of wrong placed connect(), but that doesnt appear to make any changes.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kuzulis
          Qt Champions 2020
          wrote on last edited by
          #4

          bq. Since then also the terminal-example shows “ERROR: 0”

          It is NoError.

          bq. “pSerialPort->clear()” just before “open()” is called, since then i get the following Error-String

          It is because your port already closed, so the clear() method can't to be success.

          bq. USB->RS232 connector

          What is connector model?

          bq. I use the build in Version of QSerialPort in QT 5.1.1

          Please download and compile the Git version of QtSerialPort and try check again.

          Also:

          1. Please provide a minimal compilable project that reproduce a problem (it is better - console if possible).

          2. You can try to do debugging himself to help us.

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            I changed my Modification in the terminal example:

            @void MainWindow::handleError(QSerialPort::SerialPortError error)
            {
            qDebug() << "ERROR: " << error << "ErrorString: " << serial->errorString();
            if (error == QSerialPort::ResourceError) {
            QMessageBox::critical(this, tr("Critical Error"), serial->errorString());
            closeSerialPort();
            }
            }@
            Now the terminal example shows the following in debugging console:
            *ERROR: 0 ErrorString: "Unknown error" *
            Thats exactly the same behaivior as in my Program.

            The USB->RS232 converter is type U232-P9 made in Taiwan (-: dont have more information.

            I will have to read about the Git Version and how to compile it, never heard about that so far.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kuzulis
              Qt Champions 2020
              wrote on last edited by
              #6

              bq. *ERROR: 0 ErrorString: “Unknown error”

              This is not an error. Just ignore/skip SerialPortError::NoError error code.. :)

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved