Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Sending Multiple Strings over TCP



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


  • Qt Champions 2019

    @GCE_ said in Sending Multiple Strings over TCP:

    initPorts();

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


  • Moderators

    @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



  • @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();
    

  • Moderators

    @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



  • @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?


  • Lifetime Qt Champion

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



  • Thank you guys! The problem is solved now!


Log in to reply