Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

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

    Mobile and Embedded
    raspberry pi serial port error ttyusb0 raspbian
    2
    6
    4363
    Loading More Posts
    • 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.
    • Myg0t
      Myg0t last edited by

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • Myg0t
          Myg0t last edited by

          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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 0
            • Myg0t
              Myg0t last edited by

              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 Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                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 Reply Quote 0
                • First post
                  Last post