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. Sending Multiple Strings over TCP

Sending Multiple Strings over TCP

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 696 Views 1 Watching
  • 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.
  • G Offline
    G Offline
    GCE_
    wrote on last edited by
    #1

    Hi guys!
    I am working on a measurement module, which is controlled via TCP commands.
    First I need to initialize 4 ports as output and after that I need to set the ports to either high or low (0 or 1).
    My Problem is, that the first 2 commands are recognized by the module and the other two commands not and I don't know why.
    I tried everything and still do not have any solution for it. I hope you guys can help me.
    Here is my source:

    #include "tcpsocket.h"
    
    tcpsocket::tcpsocket(QObject *parent) : QObject(parent)
    {
        socket = new QTcpSocket(this);
        IpMoonCard="192.168.0.153";
        connect(socket, &QTcpSocket::connected,this, &tcpsocket::connected);
        connect(socket, &QTcpSocket::disconnected, this, &tcpsocket::disconnected);
        connect(socket, &QTcpSocket::readyRead, this, &tcpsocket::readyRead);
        connect(socket,&QTcpSocket::bytesWritten, this, &tcpsocket::bytesWritten);
    }
    
    void tcpsocket::connectSocket()
    {
        socket->connectToHost(IpMoonCard,33330);
        initPorts();
    }
    
    void tcpsocket::writeMoonCard()
    {
        /*if(q<=15){
            q+=1;
        }
        else {
            q=0;
        }*/
        /*value= QString("%1").arg(q,4,2,QChar('0'));
        socket->write(("PORT:SET:IO29:"+value[0]).toUtf8());
        QThread::msleep(100);
        socket->write(("PORT:SET:IO4:"+value[1]).toUtf8());
        QThread::msleep(100);
        socket->write(("PORT:SET:IO41:"+value[2]).toUtf8());
        QThread::msleep(100);*/
        socket->write("PORT:SET:42:1");
        socket->write("PORT:SET:41:1");
        socket->write("PORT:SET:04:1");
        socket->write("PORT:SET:29:1");
    }
    
    void tcpsocket::initPorts()
    {
        socket->write("PORT:DIR:42:output");
        socket->write("PORT:DIR:41:output");
        socket->write("PORT:DIR:04:output");
        socket->write("PORT:DIR:29:output");
        writeMoonCard();
    }
    
    void tcpsocket::connected()
    {
        qDebug()<<"Socket connected";
    }
    
    void tcpsocket::disconnected()
    {
        qDebug()<<"Socket disconnected";
    }
    
    void tcpsocket::readyRead()
    {
        qDebug()<<"Bytes Avalable: "<<socket->bytesAvailable();
    
        if(socket->bytesAvailable()>0){
            recBuf+=socket->readAll();
            qDebug()<<"\n"<<recBuf<<"\n";
            return;
        }
        else {
            qDebug()<<recBuf;
            recBuf="";
            return;
        }
    
    }
    
    void tcpsocket::bytesWritten(qint64 bytes)
    {
        qDebug()<<bytes<<" bytes written\n";
    }
    

    This is my source for main.cpp:

    #include <QCoreApplication>
    #include <bitset>
    #include <QDebug>
    #include <iostream>
    #include "tcpsocket.h"
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        tcpsocket socket;
        socket.connectSocket();
        return a.exec();
    }
    
    

    For your information: I am using QT 5.15, Windows10 Pro, Qt Creator 4.12.4

    Thank you in advance!

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • G GCE_

      Hi guys!
      I am working on a measurement module, which is controlled via TCP commands.
      First I need to initialize 4 ports as output and after that I need to set the ports to either high or low (0 or 1).
      My Problem is, that the first 2 commands are recognized by the module and the other two commands not and I don't know why.
      I tried everything and still do not have any solution for it. I hope you guys can help me.
      Here is my source:

      #include "tcpsocket.h"
      
      tcpsocket::tcpsocket(QObject *parent) : QObject(parent)
      {
          socket = new QTcpSocket(this);
          IpMoonCard="192.168.0.153";
          connect(socket, &QTcpSocket::connected,this, &tcpsocket::connected);
          connect(socket, &QTcpSocket::disconnected, this, &tcpsocket::disconnected);
          connect(socket, &QTcpSocket::readyRead, this, &tcpsocket::readyRead);
          connect(socket,&QTcpSocket::bytesWritten, this, &tcpsocket::bytesWritten);
      }
      
      void tcpsocket::connectSocket()
      {
          socket->connectToHost(IpMoonCard,33330);
          initPorts();
      }
      
      void tcpsocket::writeMoonCard()
      {
          /*if(q<=15){
              q+=1;
          }
          else {
              q=0;
          }*/
          /*value= QString("%1").arg(q,4,2,QChar('0'));
          socket->write(("PORT:SET:IO29:"+value[0]).toUtf8());
          QThread::msleep(100);
          socket->write(("PORT:SET:IO4:"+value[1]).toUtf8());
          QThread::msleep(100);
          socket->write(("PORT:SET:IO41:"+value[2]).toUtf8());
          QThread::msleep(100);*/
          socket->write("PORT:SET:42:1");
          socket->write("PORT:SET:41:1");
          socket->write("PORT:SET:04:1");
          socket->write("PORT:SET:29:1");
      }
      
      void tcpsocket::initPorts()
      {
          socket->write("PORT:DIR:42:output");
          socket->write("PORT:DIR:41:output");
          socket->write("PORT:DIR:04:output");
          socket->write("PORT:DIR:29:output");
          writeMoonCard();
      }
      
      void tcpsocket::connected()
      {
          qDebug()<<"Socket connected";
      }
      
      void tcpsocket::disconnected()
      {
          qDebug()<<"Socket disconnected";
      }
      
      void tcpsocket::readyRead()
      {
          qDebug()<<"Bytes Avalable: "<<socket->bytesAvailable();
      
          if(socket->bytesAvailable()>0){
              recBuf+=socket->readAll();
              qDebug()<<"\n"<<recBuf<<"\n";
              return;
          }
          else {
              qDebug()<<recBuf;
              recBuf="";
              return;
          }
      
      }
      
      void tcpsocket::bytesWritten(qint64 bytes)
      {
          qDebug()<<bytes<<" bytes written\n";
      }
      

      This is my source for main.cpp:

      #include <QCoreApplication>
      #include <bitset>
      #include <QDebug>
      #include <iostream>
      #include "tcpsocket.h"
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
          tcpsocket socket;
          socket.connectSocket();
          return a.exec();
      }
      
      

      For your information: I am using QT 5.15, Windows10 Pro, Qt Creator 4.12.4

      Thank you in advance!

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

      @GCE_ said in Sending Multiple Strings over TCP:

      initPorts();

      You should call it in connected() as connectToHost() takes some time.

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

      1 Reply Last reply
      4
      • G GCE_

        Hi guys!
        I am working on a measurement module, which is controlled via TCP commands.
        First I need to initialize 4 ports as output and after that I need to set the ports to either high or low (0 or 1).
        My Problem is, that the first 2 commands are recognized by the module and the other two commands not and I don't know why.
        I tried everything and still do not have any solution for it. I hope you guys can help me.
        Here is my source:

        #include "tcpsocket.h"
        
        tcpsocket::tcpsocket(QObject *parent) : QObject(parent)
        {
            socket = new QTcpSocket(this);
            IpMoonCard="192.168.0.153";
            connect(socket, &QTcpSocket::connected,this, &tcpsocket::connected);
            connect(socket, &QTcpSocket::disconnected, this, &tcpsocket::disconnected);
            connect(socket, &QTcpSocket::readyRead, this, &tcpsocket::readyRead);
            connect(socket,&QTcpSocket::bytesWritten, this, &tcpsocket::bytesWritten);
        }
        
        void tcpsocket::connectSocket()
        {
            socket->connectToHost(IpMoonCard,33330);
            initPorts();
        }
        
        void tcpsocket::writeMoonCard()
        {
            /*if(q<=15){
                q+=1;
            }
            else {
                q=0;
            }*/
            /*value= QString("%1").arg(q,4,2,QChar('0'));
            socket->write(("PORT:SET:IO29:"+value[0]).toUtf8());
            QThread::msleep(100);
            socket->write(("PORT:SET:IO4:"+value[1]).toUtf8());
            QThread::msleep(100);
            socket->write(("PORT:SET:IO41:"+value[2]).toUtf8());
            QThread::msleep(100);*/
            socket->write("PORT:SET:42:1");
            socket->write("PORT:SET:41:1");
            socket->write("PORT:SET:04:1");
            socket->write("PORT:SET:29:1");
        }
        
        void tcpsocket::initPorts()
        {
            socket->write("PORT:DIR:42:output");
            socket->write("PORT:DIR:41:output");
            socket->write("PORT:DIR:04:output");
            socket->write("PORT:DIR:29:output");
            writeMoonCard();
        }
        
        void tcpsocket::connected()
        {
            qDebug()<<"Socket connected";
        }
        
        void tcpsocket::disconnected()
        {
            qDebug()<<"Socket disconnected";
        }
        
        void tcpsocket::readyRead()
        {
            qDebug()<<"Bytes Avalable: "<<socket->bytesAvailable();
        
            if(socket->bytesAvailable()>0){
                recBuf+=socket->readAll();
                qDebug()<<"\n"<<recBuf<<"\n";
                return;
            }
            else {
                qDebug()<<recBuf;
                recBuf="";
                return;
            }
        
        }
        
        void tcpsocket::bytesWritten(qint64 bytes)
        {
            qDebug()<<bytes<<" bytes written\n";
        }
        

        This is my source for main.cpp:

        #include <QCoreApplication>
        #include <bitset>
        #include <QDebug>
        #include <iostream>
        #include "tcpsocket.h"
        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
            tcpsocket socket;
            socket.connectSocket();
            return a.exec();
        }
        
        

        For your information: I am using QT 5.15, Windows10 Pro, Qt Creator 4.12.4

        Thank you in advance!

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @GCE_
        Additionally to what @jsulm said, give your os time operate the port/flush the data.

        you're already listening to the bytesWritten signal. So instead of writing all commands directly one after an other, write one, and wait until bytesWritten-signal signals the correct amount if bytes written and then send the next command


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        G 1 Reply Last reply
        2
        • J.HilkJ J.Hilk

          @GCE_
          Additionally to what @jsulm said, give your os time operate the port/flush the data.

          you're already listening to the bytesWritten signal. So instead of writing all commands directly one after an other, write one, and wait until bytesWritten-signal signals the correct amount if bytes written and then send the next command

          G Offline
          G Offline
          GCE_
          wrote on last edited by
          #4

          @J-Hilk said in Sending Multiple Strings over TCP:

          @GCE_
          Additionally to what @jsulm said, give your os time operate the port/flush the data.

          you're already listening to the bytesWritten signal. So instead of writing all commands directly one after an other, write one, and wait until bytesWritten-signal signals the correct amount if bytes written and then send the next command

          Ok I did what @jsulm said and moved my command.
          So @J-Hilk you mean I should do this after every single command?

          socket->write("PORT:SET:42:1");
              socket->waitForBytesWritten();
          
          J.HilkJ 1 Reply Last reply
          0
          • G GCE_

            @J-Hilk said in Sending Multiple Strings over TCP:

            @GCE_
            Additionally to what @jsulm said, give your os time operate the port/flush the data.

            you're already listening to the bytesWritten signal. So instead of writing all commands directly one after an other, write one, and wait until bytesWritten-signal signals the correct amount if bytes written and then send the next command

            Ok I did what @jsulm said and moved my command.
            So @J-Hilk you mean I should do this after every single command?

            socket->write("PORT:SET:42:1");
                socket->waitForBytesWritten();
            
            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @GCE_ said in Sending Multiple Strings over TCP:

            So @J-Hilk you mean I should do this after every single command?
            socket->write("PORT:SET:42:1");
            socket->waitForBytesWritten();

            NOUGH!

            something like this:

            QVector<QString> m_commands{"PORT:SET:42:1","PORT:SET:41:1","PORT:SET:04:1","PORT:SET:29:1"};
            int commandIndex(0);
            
            
            void tcpsocket::bytesWritten(qint64 bytes)
            {
                qDebug()<<bytes<<" bytes written\n";
                m_bytesWrittenSofar + bytes;
                if(m_bytesWrittenSofar == m_commands.at(commandIndex).size()){
                     if(++commandIndex < m_commands.size())
                      socket->write( m_commands.at(commandIndex));
                }
            }
            ....
            
            
            
            

            Beware mockup code, will not compile, but you get the idea


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            G 1 Reply Last reply
            5
            • J.HilkJ J.Hilk

              @GCE_ said in Sending Multiple Strings over TCP:

              So @J-Hilk you mean I should do this after every single command?
              socket->write("PORT:SET:42:1");
              socket->waitForBytesWritten();

              NOUGH!

              something like this:

              QVector<QString> m_commands{"PORT:SET:42:1","PORT:SET:41:1","PORT:SET:04:1","PORT:SET:29:1"};
              int commandIndex(0);
              
              
              void tcpsocket::bytesWritten(qint64 bytes)
              {
                  qDebug()<<bytes<<" bytes written\n";
                  m_bytesWrittenSofar + bytes;
                  if(m_bytesWrittenSofar == m_commands.at(commandIndex).size()){
                       if(++commandIndex < m_commands.size())
                        socket->write( m_commands.at(commandIndex));
                  }
              }
              ....
              
              
              
              

              Beware mockup code, will not compile, but you get the idea

              G Offline
              G Offline
              GCE_
              wrote on last edited by
              #6

              @J-Hilk Thank you very much, it worked!
              You are awesome guys :D

              another question:
              when naming a variable, why it begins with m_ ?
              Is it meant for membervariable?

              mrjjM 1 Reply Last reply
              1
              • G GCE_

                @J-Hilk Thank you very much, it worked!
                You are awesome guys :D

                another question:
                when naming a variable, why it begins with m_ ?
                Is it meant for membervariable?

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @GCE_
                Hi
                Yes m_ is used to indicate its a member of the class.

                1 Reply Last reply
                4
                • G Offline
                  G Offline
                  GCE_
                  wrote on last edited by
                  #8

                  Thank you guys! The problem is solved now!

                  1 Reply Last reply
                  1

                  • Login

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