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 to send the lecture of a Serial Port through a QTcpServer to a Client

How to send the lecture of a Serial Port through a QTcpServer to a Client

Scheduled Pinned Locked Moved Solved General and Desktop
qserialportqtcpserverqtcpsocket
5 Posts 3 Posters 1.2k 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.
  • DoohamD Offline
    DoohamD Offline
    Dooham
    wrote on last edited by
    #1

    Hello,
    I have to develop a code that can read the bytes from a device connected in a Serial Port (the device that I have is a Pixhawk, and it is constantly sending messages to the serial Port), and send this lecture to other program (that is the client) through a QTcpServer. With this goal in mind I designed the following classes to the QTcpServer and the SerialPort:

    serialport.h

    #ifndef MYSERIALPORT_H
    #define MYSERIALPORT_H
    #include <QObject>
    #include <QtSerialPort/QSerialPort>
    
    
    class MySerialPort: public QObject
    {
        Q_OBJECT
    public:
        MySerialPort();
    signals:
        void msgChanged();
    public slots:
        void openSerialPort();
        void closeSerialPort();
    
        void readData();
        QByteArray getMensaje();
    
    
    private:
    QSerialPort *serial;
    QByteArray data;
    
    QByteArray dataAux;
    
    
    
    };
    
    #endif // MYSERIALPORT_H
    
    

    serialport.cpp

    #include "myserialport.h"
    #include <QObject>
    #include <QtSerialPort/QSerialPort>
    #include <QDebug>
    
    
    MySerialPort::MySerialPort()
    {
        serial = new QSerialPort(this);
        data= "";
    
        connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
    
    
    }
    
    void MySerialPort::openSerialPort()
    {
        serial->setPortName("COM4");
        //serial->setBaudRate(QSerialPort::Baud9600);
        serial->setBaudRate(QSerialPort::Baud115200);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->open(QIODevice::ReadOnly);
    
    
    }
    
    void MySerialPort::closeSerialPort()
    {
        if(serial->isOpen()){
            serial->close();
        }
    }
    
    void MySerialPort::readData()
    {
    
       data = serial->readAll();
        dataAux=data.toHex();
        emit msgChanged();
    
    }
    
    QByteArray MySerialPort::getMensaje()
    {
        return data;
    }
    
    

    myserver.h

    #ifndef MYSERVER_H
    #define MYSERVER_H
    
    #include <QObject>
    #include <QDebug>
    #include <QTcpServer>
    #include <QTcpSocket>
    #include "myserialport.h"
    
    class MyServer: public QObject
    {
        Q_OBJECT
     public:
         explicit MyServer(QObject *parent = 0);
    
     signals:
    
     public slots:
         void newConnection();
         void getMsg();
    
    
     private:
         QTcpServer *server;
         MySerialPort *mipuerto;
         QTcpSocket*socket;
         QByteArray arr;
    };
    
    #endif // MYSERVER_H
    

    myserver.cpp

    #include "myserver.h"
    #include <QDataStream>
    
    
    
    
    MyServer::MyServer(QObject *parent)
    {
        server = new QTcpServer(this);
    
           
            connect(server, SIGNAL(newConnection()),
                    this, SLOT(newConnection()));
    
    
            if(!server->listen(QHostAddress::Any, 9999))
            {
                qDebug() << "Server could not start";
            }
            else
            {
                qDebug() << "Server started!";
            }
    }
    
    void MyServer::newConnection()
    {
        
          socket = server->nextPendingConnection();
          qDebug()<<"Hola";
         connect(mipuerto, SIGNAL(msgChanged()), this, SLOT(getMsg()));
    
    
           mipuerto=new MySerialPort;
           mipuerto->openSerialPort();
    
    
    
    
    
          socket->waitForBytesWritten(3000);
       
    }
    
    void MyServer::getMsg()
    {
        arr=mipuerto->getMensaje();
        socket->write(arr);
         socket->waitForBytesWritten(3000);
    }
    
    
    
    

    I know that the code for the serialPort is correct (I get the correct QByteArray) and I also know that the for the server is correct (I proved to send a "Hello client!" and the client received the message). But, my problem appears when I try to connect this two codes. I had to make signal-slot method, because the function readData is constantly reading data from the serial port; and, in order to send every package of messages when I receive them, I thought in this solution. But, when I call the connect, Qt doesn't pass of this point.

    The client has this code:

    mytcpsocket

    #ifndef MYTCPSOCKET_H
    #define MYTCPSOCKET_H
    
    #include <QObject>
    #include <QTcpSocket>
    #include <QAbstractSocket>
    #include <QDebug>
    
    class MyTcpSocket : public QObject
    {
        Q_OBJECT
    public:
        explicit MyTcpSocket(QObject *parent = 0);
    
        void doConnect();
    
    signals:
    
    public slots:
        void connected();
        void disconnected();
        void bytesWritten(qint64 bytes);
        void readyRead();
    
    private:
        QTcpSocket *socket;
    
    };
    
    #endif // MYTCPSOCKET_H
    

    mytcpsocket.cpp

    #include "mytcpsocket.h"
    
    MyTcpSocket::MyTcpSocket(QObject *parent) :
        QObject(parent)
    {
    }
    
    void MyTcpSocket::doConnect()
    {
        socket = new QTcpSocket(this);
    
        connect(socket, SIGNAL(connected()),this, SLOT(connected()));
        connect(socket, SIGNAL(disconnected()),this, SLOT(disconnected()));
        connect(socket, SIGNAL(bytesWritten(qint64)),this, SLOT(bytesWritten(qint64)));
        connect(socket, SIGNAL(readyRead()),this, SLOT(readyRead()));
    
        qDebug() << "connecting...";
    
        // this is not blocking call
        socket->connectToHost("localhost", 9999);
    
        // we need to wait...
        if(!socket->waitForConnected(5000))
        {
            qDebug() << "Error: " << socket->errorString();
        }
    }
    
    void MyTcpSocket::connected()
    {
        qDebug() << "connected...";
    
        // Hey server, tell me about you.
        socket->write("Hola");
    }
    
    void MyTcpSocket::disconnected()
    {
        qDebug() << "disconnected...";
    }
    
    void MyTcpSocket::bytesWritten(qint64 bytes)
    {
        qDebug() << bytes << " bytes written...";
    }
    
    void MyTcpSocket::readyRead()
    {
        qDebug() << "reading...";
    
        // read the data from the socket
        qDebug() << socket->readAll();
    }
    
    
    

    Thanks you for your help.

    jsulmJ 1 Reply Last reply
    0
    • DoohamD Dooham

      Hello,
      I have to develop a code that can read the bytes from a device connected in a Serial Port (the device that I have is a Pixhawk, and it is constantly sending messages to the serial Port), and send this lecture to other program (that is the client) through a QTcpServer. With this goal in mind I designed the following classes to the QTcpServer and the SerialPort:

      serialport.h

      #ifndef MYSERIALPORT_H
      #define MYSERIALPORT_H
      #include <QObject>
      #include <QtSerialPort/QSerialPort>
      
      
      class MySerialPort: public QObject
      {
          Q_OBJECT
      public:
          MySerialPort();
      signals:
          void msgChanged();
      public slots:
          void openSerialPort();
          void closeSerialPort();
      
          void readData();
          QByteArray getMensaje();
      
      
      private:
      QSerialPort *serial;
      QByteArray data;
      
      QByteArray dataAux;
      
      
      
      };
      
      #endif // MYSERIALPORT_H
      
      

      serialport.cpp

      #include "myserialport.h"
      #include <QObject>
      #include <QtSerialPort/QSerialPort>
      #include <QDebug>
      
      
      MySerialPort::MySerialPort()
      {
          serial = new QSerialPort(this);
          data= "";
      
          connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
      
      
      }
      
      void MySerialPort::openSerialPort()
      {
          serial->setPortName("COM4");
          //serial->setBaudRate(QSerialPort::Baud9600);
          serial->setBaudRate(QSerialPort::Baud115200);
          serial->setDataBits(QSerialPort::Data8);
          serial->setParity(QSerialPort::NoParity);
          serial->setStopBits(QSerialPort::OneStop);
          serial->setFlowControl(QSerialPort::NoFlowControl);
          serial->open(QIODevice::ReadOnly);
      
      
      }
      
      void MySerialPort::closeSerialPort()
      {
          if(serial->isOpen()){
              serial->close();
          }
      }
      
      void MySerialPort::readData()
      {
      
         data = serial->readAll();
          dataAux=data.toHex();
          emit msgChanged();
      
      }
      
      QByteArray MySerialPort::getMensaje()
      {
          return data;
      }
      
      

      myserver.h

      #ifndef MYSERVER_H
      #define MYSERVER_H
      
      #include <QObject>
      #include <QDebug>
      #include <QTcpServer>
      #include <QTcpSocket>
      #include "myserialport.h"
      
      class MyServer: public QObject
      {
          Q_OBJECT
       public:
           explicit MyServer(QObject *parent = 0);
      
       signals:
      
       public slots:
           void newConnection();
           void getMsg();
      
      
       private:
           QTcpServer *server;
           MySerialPort *mipuerto;
           QTcpSocket*socket;
           QByteArray arr;
      };
      
      #endif // MYSERVER_H
      

      myserver.cpp

      #include "myserver.h"
      #include <QDataStream>
      
      
      
      
      MyServer::MyServer(QObject *parent)
      {
          server = new QTcpServer(this);
      
             
              connect(server, SIGNAL(newConnection()),
                      this, SLOT(newConnection()));
      
      
              if(!server->listen(QHostAddress::Any, 9999))
              {
                  qDebug() << "Server could not start";
              }
              else
              {
                  qDebug() << "Server started!";
              }
      }
      
      void MyServer::newConnection()
      {
          
            socket = server->nextPendingConnection();
            qDebug()<<"Hola";
           connect(mipuerto, SIGNAL(msgChanged()), this, SLOT(getMsg()));
      
      
             mipuerto=new MySerialPort;
             mipuerto->openSerialPort();
      
      
      
      
      
            socket->waitForBytesWritten(3000);
         
      }
      
      void MyServer::getMsg()
      {
          arr=mipuerto->getMensaje();
          socket->write(arr);
           socket->waitForBytesWritten(3000);
      }
      
      
      
      

      I know that the code for the serialPort is correct (I get the correct QByteArray) and I also know that the for the server is correct (I proved to send a "Hello client!" and the client received the message). But, my problem appears when I try to connect this two codes. I had to make signal-slot method, because the function readData is constantly reading data from the serial port; and, in order to send every package of messages when I receive them, I thought in this solution. But, when I call the connect, Qt doesn't pass of this point.

      The client has this code:

      mytcpsocket

      #ifndef MYTCPSOCKET_H
      #define MYTCPSOCKET_H
      
      #include <QObject>
      #include <QTcpSocket>
      #include <QAbstractSocket>
      #include <QDebug>
      
      class MyTcpSocket : public QObject
      {
          Q_OBJECT
      public:
          explicit MyTcpSocket(QObject *parent = 0);
      
          void doConnect();
      
      signals:
      
      public slots:
          void connected();
          void disconnected();
          void bytesWritten(qint64 bytes);
          void readyRead();
      
      private:
          QTcpSocket *socket;
      
      };
      
      #endif // MYTCPSOCKET_H
      

      mytcpsocket.cpp

      #include "mytcpsocket.h"
      
      MyTcpSocket::MyTcpSocket(QObject *parent) :
          QObject(parent)
      {
      }
      
      void MyTcpSocket::doConnect()
      {
          socket = new QTcpSocket(this);
      
          connect(socket, SIGNAL(connected()),this, SLOT(connected()));
          connect(socket, SIGNAL(disconnected()),this, SLOT(disconnected()));
          connect(socket, SIGNAL(bytesWritten(qint64)),this, SLOT(bytesWritten(qint64)));
          connect(socket, SIGNAL(readyRead()),this, SLOT(readyRead()));
      
          qDebug() << "connecting...";
      
          // this is not blocking call
          socket->connectToHost("localhost", 9999);
      
          // we need to wait...
          if(!socket->waitForConnected(5000))
          {
              qDebug() << "Error: " << socket->errorString();
          }
      }
      
      void MyTcpSocket::connected()
      {
          qDebug() << "connected...";
      
          // Hey server, tell me about you.
          socket->write("Hola");
      }
      
      void MyTcpSocket::disconnected()
      {
          qDebug() << "disconnected...";
      }
      
      void MyTcpSocket::bytesWritten(qint64 bytes)
      {
          qDebug() << bytes << " bytes written...";
      }
      
      void MyTcpSocket::readyRead()
      {
          qDebug() << "reading...";
      
          // read the data from the socket
          qDebug() << socket->readAll();
      }
      
      
      

      Thanks you for your help.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Dooham said in How to send the lecture of a Serial Port through a QTcpServer to a Client:

      Qt doesn't pass of this point.

      What connect do you mean and what is the error message?
      Also I would pass serial data as parameter in signal and remove the GetMessage() method.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      DoohamD 1 Reply Last reply
      3
      • jsulmJ jsulm

        @Dooham said in How to send the lecture of a Serial Port through a QTcpServer to a Client:

        Qt doesn't pass of this point.

        What connect do you mean and what is the error message?
        Also I would pass serial data as parameter in signal and remove the GetMessage() method.

        DoohamD Offline
        DoohamD Offline
        Dooham
        wrote on last edited by
        #3

        @jsulm

          connect(mipuerto, SIGNAL(msgChanged()), this, SLOT(getMsg()));
        

        This connect, the code of the server works well until reaching this point.
        Passing the message in the signal is a good idea, I will try it later.

        1 Reply Last reply
        0
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by
          #4

          @Dooham said in How to send the lecture of a Serial Port through a QTcpServer to a Client:

          connect(mipuerto, SIGNAL(msgChanged()), this, SLOT(getMsg()));

             mipuerto=new MySerialPort;
             mipuerto->openSerialPort();
          

          I cannot see any other time when you initialize mipuerto. So that connect is trying to connect to an invalid object as that variable is undefined at the time of the connect.

          C++ is a perfectly valid school of magic.

          DoohamD 1 Reply Last reply
          3
          • fcarneyF fcarney

            @Dooham said in How to send the lecture of a Serial Port through a QTcpServer to a Client:

            connect(mipuerto, SIGNAL(msgChanged()), this, SLOT(getMsg()));

               mipuerto=new MySerialPort;
               mipuerto->openSerialPort();
            

            I cannot see any other time when you initialize mipuerto. So that connect is trying to connect to an invalid object as that variable is undefined at the time of the connect.

            DoohamD Offline
            DoohamD Offline
            Dooham
            wrote on last edited by
            #5

            @fcarney
            You were right. I changed the line where I initialize the serialport to a position before the connect and that works. Thanks!

            1 Reply Last reply
            0

            • Login

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