Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QSerialPort Open() return error: No such file or directory

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

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
raspberry piserial porterrorttyusb0raspbian
6 Posts 2 Posters 4.9k Views
  • 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.
  • M Offline
    M Offline
    Myg0t
    wrote on 13 Apr 2016, 21:01 last edited by
    #1

    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!!!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 13 Apr 2016, 22:05 last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Myg0t
        wrote on 14 Apr 2016, 13:44 last edited by
        #3

        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!

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 14 Apr 2016, 15:58 last edited by
          #4

          Do you have the rights to access that port ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Myg0t
            wrote on 14 Apr 2016, 17:57 last edited by
            #5

            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...

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 14 Apr 2016, 18:09 last edited by
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              6/6

              14 Apr 2016, 18:09

              • Login

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