QSerialPort Open() return error: No such file or directory



  • Hi there, Currently I'm using a Raspberry Pi 2 & 3 with the latest version of Rasbian (Jessie). I'm struggling to get the serial port working in QT. I'm using Xbee radio transmitter/receivers. I know for a fact that they do work because I can communicate between them using cutecom. But everytime I run my code (which is largely based on of the tutorials) I receive an error in the console saying "Error: No such file or directory" and this pops up when I run serial->open(QSerialPort:ReadWrite). here is my code:

    XbeeCom.cpp:

    #include "xbeecom.h"
    
    #include <QSerialPortInfo>
    #include <QSerialPort>
    
    #include <QDebug>
    #include <QList>
    
    XbeeCom::XbeeCom(QObject *parent) :
        QObject(parent)
    {
            serial = new QSerialPort(this);
    
            QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts();
    
            for(int i = 0; i < ports.size(); i++){
                qDebug() << ports[i].portName();
            }
    
            QSerialPortInfo info("ttyUSB0");
            qDebug() << "Name :" << info.portName();
            qDebug() << "Description : " << info.description();
            qDebug() << "Manufacturer :" << info.manufacturer();
    
            // Example use QSerialPort
            serial->setPortName(info.portName());
    //        serial->setBaudRate(9600);
    //        serial->setDataBits(QSerialPort::Data8);
    //        serial->setStopBits(QSerialPort::OneStop);
    //        serial->setParity(QSerialPort::NoParity);
    //        serial->setFlowControl(QSerialPort::NoFlowControl);
    
            connect(serial,
                    &QSerialPort::bytesWritten,
                    this,
                    &XbeeCom::handleBytesWritten
                    );
    
            connect(serial,
                    static_cast<void (QSerialPort::*)(QSerialPort::SerialPortError)>(&QSerialPort::error),
                    this,
                    &XbeeCom::handleError
                    );
    
            connect(serial,
                    &QSerialPort::readyRead,
                    this,
                    &XbeeCom::readData
                    );
    
            if (serial->open(QSerialPort::ReadWrite)){
                serial->setBaudRate(9600);
                serial->setDataBits(QSerialPort::Data8);
                serial->setStopBits(QSerialPort::OneStop);
                serial->setParity(QSerialPort::NoParity);
                serial->setFlowControl(QSerialPort::NoFlowControl);
                qDebug() << "Hello world";
                QByteArray data(QString("Hello world").toStdString().c_str());
                bytesWritten = serial->write(data);
    
                if(bytesWritten == -1){
                    qDebug() << QString("Failed to write data to port %1, error: %2").arg(serial->portName()).arg(serial->errorString());
    
                }
                else if(bytesWritten != serial->size()){
                    qDebug() << "Failed to write all data";
                }
    
                serial->close();
            }
            qDebug() << "After if statement";
    }
    
    void XbeeCom::readData(){
        QByteArray data = serial->readAll();
        qDebug() << QString(data);
    }
    
    void XbeeCom::handleBytesWritten(qint64 bytes){
        bytesWritten += bytes;
        if(bytesWritten == serial->size()){
            bytesWritten = 0;
            qDebug() << ("Data successfully sent to port");
        }
    }
    
    void XbeeCom::handleError(QSerialPort::SerialPortError serialPortError){
        if(serialPortError == QSerialPort::WriteError){
            qDebug() << QString("An I/O error occurred while writing the data to port %1, error: %2")
                        .arg(serial->portName())
                        .arg(serial->errorString());
        }
        else
            qDebug() << QString("Some other error on port %1, error: %2")
                        .arg(serial->portName())
                        .arg(serial->errorString());
    }
    
    

    And here is my XbeeCom.h:

    #ifndef XBEECOM_H
    #define XBEECOM_H
    
    #include <QObject>
    #include <QSerialPort>
    
    
    class XbeeCom : public QObject
    {
        Q_OBJECT
    
    public:
        explicit XbeeCom(QObject *parent = 0);
    
    private:
        QSerialPort *serial;
        qint64 bytesWritten;
        QByteArray writeData;
    
    
    signals:
    
    private slots:
        void handleBytesWritten(qint64 bytes);
        void handleError(QSerialPort::SerialPortError serialPortError);
        //void handleTimeout();
        void readData();
    
    public slots:
    
    
    };
    
    #endif // XBEECOM_H
    
    

    I'm not trying to do anything super special here.. Just send some sample data and hope that I receive it on the other end...

    Let me know if you guys need more information.

    Thank you!!!


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Might be a silly question but are you sure that your QSerialPortInfo is valid ?

    I'd compare it with what availableReports returns.



  • As far as I am aware, yes it should be a valid port. I know that the XBee is connected on ttyUSB0. And when this portion of code is ran:

    QSerialPortInfo info("ttyUSB0");
            qDebug() << "Name :" << info.portName();
            qDebug() << "Description : " << info.description();
            qDebug() << "Manufacturer :" << info.manufacturer();
    

    My output is:
    Name: "ttyUSB0"
    Description : "FT231 USB UART"
    Manufacturer: "FTDI"

    I assume this means its a valid port?

    Thanks!


  • Lifetime Qt Champion

    Do you have the rights to access that port ?



  • Yes, I believe that I do. I'm logged in as the pi user, and I'm able to run cutecom (without sudo) and send and receive messages on the port. So I assume that the pi user has permissions to do so. The weird thing is that the serial examples work fine. So I assume that I must be missing something that they are doing. But I'm not seeing it...


  • Lifetime Qt Champion

    Then I'd recommend taking a look the example code and rewrite yours to match it.



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