How can write multi packet over QTcpSocket??



  • I want to send multi-packet and read receive data and appended to QTextEdit at same time, but it's Interchangeably and mixed when read receive data and appended to QtextEdit,how can send multi-packet without problem??

    QByteArray QTcp::Data{
    SendTCPData(send->inv());
        QByteArray rcv=readData();
        QString in(rcv.toHex());
        QTextCursor cursor = ui->textEdit_2->textCursor();
        QTextBlockFormat textBlockFormat = cursor.blockFormat();
        textBlockFormat.setAlignment(Qt::AlignLeft);
        cursor.mergeBlockFormat(textBlockFormat);
        ui->textEdit_2->setTextCursor(cursor);
        ui->textEdit_2->setTextColor(Qt::black);
     readData().clear();
    
        usleep(3000000);
    
            SendTCPData(send->SendWrt(epc));
            QByteArray rcv1=readData();
            QString wr(rcv1.toHex());
            textBlockFormat.setAlignment(Qt::AlignRight);
            ui->textEdit_2->setTextColor(Qt::darkGray);
                ui->textEdit_2->append(wr);
                readData().clear();
            }
        
      int QTcp::SendTCPData(QByteArray Data)
    {
        bool connected = (_socket->state() == QTcpSocket::ConnectedState);
        if(connected){
            _socket->write(Data, Data.size());
            qDebug()<<"Data send"<<Data.toHex();
            return _socket->waitForBytesWritten(5000);
        }
        else{
            qDebug()<<"Not Connected";
            return false;
        }
    
    QByteArray QTcp::readData()
    {
        QByteArray data= _socket->readAll();
        qDebug()<<"readData"<<data.toHex();
        return data;
    }
    
    }
    
    
    
    

  • Lifetime Qt Champion

    Hi,

    Your setup is a bit strange. Why don't you use the asynchronous nature of QTcpSocket to handle your data transmission ? You should use the QTcpSocket::readyRead to know when there's new data to read and then update your text edit with it.



  • yes I used connect(_server, SIGNAL(newConnection()), this, SLOT(NewConnection()));
    to printout when new connection receive and in NewConnection() function I set connect(_socket, SIGNAL(readyRead()),this, SLOT(readData()));
    but when read data,for example the answer of first sendTcp print out for second sendTcp data and Vice versa


  • Lifetime Qt Champion

    Except that from your sample code you have a pseudo synchronous system:

    • You send data to your TCP socket
    • You read from it,
    • Do some update
    • Sleep
    • Start again.

    That's not how it works. Since you are doing a two-way communication, you should send that data. And once you received the complete answer send the second wave of data.

    In ready read, buffer the data you received until you received everything and then parse it update your widgets.



  • @SGaist said:

    In ready read, buffer the data you received until you received everything and then parse it update your widgets

    you mean ReadyRead do this ???
    or I should do this??


  • Lifetime Qt Champion

    You should do this. You should have a slot that reads the data from the socket and append it to a buffer so that you can parse the answer once it's complete.



  • ok tnx I try this



  • I do this , if i send one package each time write data , read data and printout data is true but when appended data to QtextEdit first appended last read data then if send again appended new read data
    I use

    ui->textEdit->textCursor().insertText(pwr);
    ui->textEdit->textCursor().clearSelection();
    ui->textEdit->textCursor().deletePreviousChar();
    
    

    but still first show last appended


  • Lifetime Qt Champion

    Why deletePreviousChar ?



  • This post is deleted!

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.