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. How I listen serial port with writing client code?
QtWS25 Last Chance

How I listen serial port with writing client code?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qserialportqtserialportcppserialportvirtual
6 Posts 2 Posters 955 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.
  • E Offline
    E Offline
    elypheldia
    wrote on 16 Aug 2021, 12:28 last edited by
    #1

    I am working on communication of virtual serial ports. I opened and wrote the data to the virtual serial port. Then, I listened with using socket.

    cat /dev/tnt0
    

    But now, I want to listen the tnt0 serial port over qt client code. How can I do that? If you give some examples, it would be nice.
    main.cpp

    #include <QCoreApplication>
    #include "myserialport.h"
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        MySerialPort s;
        s.openSerialPort();
    
        return a.exec();
    }
    

    myserialport.h

    #ifndef MYSERIALPORT_H
    #define MYSERIALPORT_H
    
    #include <QObject>
    #include <QSerialPort>
    
    class MySerialPort : public QObject
    {
        Q_OBJECT
    public:
        explicit MySerialPort(QObject *parent = nullptr);
        void openSerialPort();
        void closeSerialPort();
        void writeData(const QByteArray &);
    
    
    signals:
    
    public slots:
        void readData();
        void handleError(QSerialPort::SerialPortError);
    private:
        QSerialPort *serial;
    };
    
    #endif // MYSERIALPORT_H
    

    myserialport.cpp

    #include "myserialport.h"
    #include <QSerialPort>
    #include <QDebug>
    #include <QSerialPortInfo>
    MySerialPort::MySerialPort(QObject *parent) : QObject(parent)
    {
        serial=new QSerialPort(this);
        connect(serial,SIGNAL(readyRead()),this,SLOT(readData()));
        connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this,
        SLOT(handleError(QSerialPort::SerialPortError)));
    
    }
    void MySerialPort::openSerialPort()
    {
        serial->setPortName("/dev/tnt0");
        serial->setBaudRate(QSerialPort::Baud9600);
        serial->setDataBits(QSerialPort::Data8);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setParity(QSerialPort::NoParity);
        if(serial->open(QIODevice::ReadWrite))
        {
            qDebug()<<"opened";
            QByteArray data="hello";
            writeData(data);
    
        }
        else
        {
            qDebug()<<QSerialPort::PermissionError;
        }
    
    }
    void MySerialPort::closeSerialPort()
    {
        if(serial->isOpen())
        {
            serial->close();
        }
    }
    void MySerialPort::writeData(const QByteArray &data)
    {
        qDebug() << "Bytes Written:" << serial->write(data);
    
    }
    void MySerialPort::readData()
    {
        serial->readAll();
    
    }
    void MySerialPort::handleError(QSerialPort::SerialPortError error)
    {
        if (error == QSerialPort::NoError) {
            qDebug()<<"no error";
        }
    }
    
    
    K 1 Reply Last reply 16 Aug 2021, 12:40
    0
    • E elypheldia
      16 Aug 2021, 12:28

      I am working on communication of virtual serial ports. I opened and wrote the data to the virtual serial port. Then, I listened with using socket.

      cat /dev/tnt0
      

      But now, I want to listen the tnt0 serial port over qt client code. How can I do that? If you give some examples, it would be nice.
      main.cpp

      #include <QCoreApplication>
      #include "myserialport.h"
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          MySerialPort s;
          s.openSerialPort();
      
          return a.exec();
      }
      

      myserialport.h

      #ifndef MYSERIALPORT_H
      #define MYSERIALPORT_H
      
      #include <QObject>
      #include <QSerialPort>
      
      class MySerialPort : public QObject
      {
          Q_OBJECT
      public:
          explicit MySerialPort(QObject *parent = nullptr);
          void openSerialPort();
          void closeSerialPort();
          void writeData(const QByteArray &);
      
      
      signals:
      
      public slots:
          void readData();
          void handleError(QSerialPort::SerialPortError);
      private:
          QSerialPort *serial;
      };
      
      #endif // MYSERIALPORT_H
      

      myserialport.cpp

      #include "myserialport.h"
      #include <QSerialPort>
      #include <QDebug>
      #include <QSerialPortInfo>
      MySerialPort::MySerialPort(QObject *parent) : QObject(parent)
      {
          serial=new QSerialPort(this);
          connect(serial,SIGNAL(readyRead()),this,SLOT(readData()));
          connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this,
          SLOT(handleError(QSerialPort::SerialPortError)));
      
      }
      void MySerialPort::openSerialPort()
      {
          serial->setPortName("/dev/tnt0");
          serial->setBaudRate(QSerialPort::Baud9600);
          serial->setDataBits(QSerialPort::Data8);
          serial->setFlowControl(QSerialPort::NoFlowControl);
          serial->setStopBits(QSerialPort::OneStop);
          serial->setParity(QSerialPort::NoParity);
          if(serial->open(QIODevice::ReadWrite))
          {
              qDebug()<<"opened";
              QByteArray data="hello";
              writeData(data);
      
          }
          else
          {
              qDebug()<<QSerialPort::PermissionError;
          }
      
      }
      void MySerialPort::closeSerialPort()
      {
          if(serial->isOpen())
          {
              serial->close();
          }
      }
      void MySerialPort::writeData(const QByteArray &data)
      {
          qDebug() << "Bytes Written:" << serial->write(data);
      
      }
      void MySerialPort::readData()
      {
          serial->readAll();
      
      }
      void MySerialPort::handleError(QSerialPort::SerialPortError error)
      {
          if (error == QSerialPort::NoError) {
              qDebug()<<"no error";
          }
      }
      
      
      K Offline
      K Offline
      KroMignon
      wrote on 16 Aug 2021, 12:40 last edited by
      #2

      @elypheldia What is the problem with the code you have submitted?

      NOTE: I would use new connect syntax to avoid failures about signals/slots not existing:

      //OLD syntax
      connect(serial,SIGNAL(readyRead()), this,SLOT(readData()));
      connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this,
          SLOT(handleError(QSerialPort::SerialPortError)));
      
      // NEW syntax
      connect(serial, &QSerialPort::readyRead, this, &MySerialPort::readData);
      connect(serial, &QSerialPort::error, this, &MySerialPort::handleError);
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      E 1 Reply Last reply 16 Aug 2021, 12:50
      0
      • K KroMignon
        16 Aug 2021, 12:40

        @elypheldia What is the problem with the code you have submitted?

        NOTE: I would use new connect syntax to avoid failures about signals/slots not existing:

        //OLD syntax
        connect(serial,SIGNAL(readyRead()), this,SLOT(readData()));
        connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this,
            SLOT(handleError(QSerialPort::SerialPortError)));
        
        // NEW syntax
        connect(serial, &QSerialPort::readyRead, this, &MySerialPort::readData);
        connect(serial, &QSerialPort::error, this, &MySerialPort::handleError);
        
        E Offline
        E Offline
        elypheldia
        wrote on 16 Aug 2021, 12:50 last edited by
        #3

        @KroMignon Thx for editing. There is no problem that I have submitted. I just wanna read tnt0 serial port by writing client code, not using socket. (That's why I submitted the code that I listen with socket.)

        K 1 Reply Last reply 16 Aug 2021, 12:58
        0
        • E elypheldia
          16 Aug 2021, 12:50

          @KroMignon Thx for editing. There is no problem that I have submitted. I just wanna read tnt0 serial port by writing client code, not using socket. (That's why I submitted the code that I listen with socket.)

          K Offline
          K Offline
          KroMignon
          wrote on 16 Aug 2021, 12:58 last edited by KroMignon
          #4

          @elypheldia said in How I listen serial port with writing client code?:

          I just wanna read tnt0 serial port by writing client code, not using socket. (That's why I submitted the code that I listen with socket.)

          I don't understand this, which socket are you talking about?
          You code is base on QSerialPort, so you are using serial port interface not any socket.

          void MySerialPort::readData()
          {
              QByteArray data = serial->readAll();
              QDebug() << "Received" << data.size() << "bytes:" << QString(data.toHex(':'));
          }
          

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          E 1 Reply Last reply 16 Aug 2021, 13:05
          0
          • K KroMignon
            16 Aug 2021, 12:58

            @elypheldia said in How I listen serial port with writing client code?:

            I just wanna read tnt0 serial port by writing client code, not using socket. (That's why I submitted the code that I listen with socket.)

            I don't understand this, which socket are you talking about?
            You code is base on QSerialPort, so you are using serial port interface not any socket.

            void MySerialPort::readData()
            {
                QByteArray data = serial->readAll();
                QDebug() << "Received" << data.size() << "bytes:" << QString(data.toHex(':'));
            }
            
            E Offline
            E Offline
            elypheldia
            wrote on 16 Aug 2021, 13:05 last edited by elypheldia
            #5

            @KroMignon Sorry if used the wrong term. I am new on qt. I wanted to indicate that I read the data I wrote to the tnt0 serial port from terminal with using cat /dev/tnt0. But How can I do this with client code instead of reading from terminal. I just wanna open the tnt1 serial port from code., and I wanna read from there.

            K 1 Reply Last reply 16 Aug 2021, 13:20
            0
            • E elypheldia
              16 Aug 2021, 13:05

              @KroMignon Sorry if used the wrong term. I am new on qt. I wanted to indicate that I read the data I wrote to the tnt0 serial port from terminal with using cat /dev/tnt0. But How can I do this with client code instead of reading from terminal. I just wanna open the tnt1 serial port from code., and I wanna read from there.

              K Offline
              K Offline
              KroMignon
              wrote on 16 Aug 2021, 13:20 last edited by
              #6

              @elypheldia said in How I listen serial port with writing client code?:

              Sorry if used the wrong term. I am new on qt. I wanted to indicate that I read the data I wrote to the tnt0 serial port from terminal with using cat /dev/tnt0. But How can I do this with client code instead of reading from terminal. I just wanna open the tnt1 serial port from code., and I wanna read from there.

              You are confusing to me.
              Do you want to use /dev/tnt0 or /dev/tnt1?

              With the code example you have provided, you are writing hello on serial device /dev/tnt0. Is this working?

              You have defined a slot MySerialPort::readData() , which is called when data are ready to be read from serial port.
              I have proposed you some changes to display debug message when slot is called. Is this slot called?

              I don't know:

              • what kind of device is connected to this serial port?
              • if the serial port configuration is correct according to attached device?
              • the attached device communication protocol: It is a binary or text protocol? Are carriage return or line feed required?

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              0

              3/6

              16 Aug 2021, 12:50

              • Login

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