Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QSerialPort cannot open rfcomm socket



  • Hi,
    I try to open rfcomm socket (bluetooth -> serial port), but QSerialPort library cannot open it. QSerialPort::open() returns false and QSerialPort::errorString() returns "Unknown Error" (error code 0). But when I open this socket by GtkTerm 0.99.7-rc1 everything works ok.
    I use Qt 5.3.1 (GCC 4.6.1, 64 bit) on Ubuntu 14.04 64 bit
    There is my piece of code that I use to connect to my socket:
    @
    serialPort->setPortName(ui->comboBoxPorts->currentText()); //"rfcomm0"
    serialPort->setBaudRate(ui->lineEditBaud->text().toInt()); //250000
    serialPort->setDataBits(QSerialPort::Data8);
    serialPort->setParity(QSerialPort::NoParity);
    serialPort->setStopBits(QSerialPort::OneStop);
    serialPort->setFlowControl(QSerialPort::NoFlowControl);

    if (!serialPort->open(QIODevice::ReadWrite)) {
        qDebug() << "Error: " << serialPort->error(); //returns 0
        ui->labelStatus->setText("Error: " + serialPort->errorString()); //returns "Unknown error"
        return;
    }
    

    @
    Have you any idea what may be wrong?
    Thanks in advance,
    Radek


  • Moderators

    What does QSerialPortInfo::availablePorts() report? Is rfcomm0 a valid serial port according to it?



  • Thanks for reply.
    QSerialPortInfo::availablePorts() detect the "rfcomm0" correctly. QSerialPortInfo::isValid() returns true.



  • Are you using the same serial port settings in GtkTerm?

    Try using different settings, i.e. Baud9600, ...
    Is it possible to only open for read-only or write-only?



  • Thanks for help. It's working on standard baud rates (9600, 115200, 230400...). QSerialPort accepts only standard baud rates??



  • Actually it does accept all.

    I did find a huge mistake your up to. You have to first open the port then apply the settings. Otherwise the settings will be applied to "default" on open. Try:

    @serialPort->setPortName(...)
    if(serialPort->open(QIODevice::ReadWrite))
    {
      serialPort->setBaudRate(ui->lineEditBaud->text().toInt()); //250000
      serialPort->setDataBits(QSerialPort::Data8);
      serialPort->setParity(QSerialPort::NoParity);
      serialPort->setStopBits(QSerialPort::OneStop);
      serialPort->setFlowControl(QSerialPort::NoFlowControl);
    }@


  • Thanks a lot. I have one another question about sockets. I don't understand why I get opening error "Device or resocues is busy" (error code 2) while QSerialPort::isBusy() returns "false" and QSerialPort::isOpen() returns "false". This occur when I open rfcomm0 port with GtkTerm, close it and then try to open it with my aplication. Is there any way to reset socket?



  • QSerialPort::isOpen():
    Returns true if the device is open; otherwise returns false. A device is open if it can be read from and/or written to.

    This means if your application did open the port and is readable/writable isOpen() returns true.

    QSerialPortInfo::isBusy():
    Not sure about this, if this really returns true if the port is still locked by another application.

    QSerialPort::open()
    This returns true if the port cannot be opened and false if not. So if you cannot open it, it probably is still locked.


Log in to reply