QTcpSocket發送結構體


我需要發送的結構體

struct NetDataHeader_t {
    int nDataType;
    int nDataSize;
};

struct NetDataBase_t {
    NetDataHeader_t dataHeader;
    double hValue;
    double lValue;
    double temperature;
};

 

我的運行函數

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 #include <QDebug>
 4 #include "sensordata.h"
 5 #include <cstring>
 6 
 7 MainWindow::MainWindow(QWidget *parent) :
 8     QMainWindow(parent),
 9     ui(new Ui::MainWindow)
10 {
11     ui->setupUi(this);
12 
13     mySocket = new QTcpSocket();
14     QObject::connect(mySocket, &QTcpSocket::readyRead, this, &MainWindow::MySocketReadData);
15     QObject::connect(mySocket, &QTcpSocket::disconnected, this, &MainWindow::MySocketDisconnected);
16 
17     ui->btnSend->setEnabled(false);
18     ui->lineEdit_IP->setText("127.0.0.1");
19     ui->lineEdit_Port->setText("5000");
20 }
21 
22 MainWindow::~MainWindow()
23 {
24     delete mySocket;
25     delete ui;
26 }
27 
28 void MainWindow::MySocketReadData()
29 {
30     QByteArray buffer;
31 
32     buffer = mySocket->readAll();
33     struct NetDataBase_t myData;
34     if(!buffer.isEmpty()) {
35         char *src = buffer.data();
36         memcpy(&myData, src, sizeof(struct NetDataBase_t));
37         ui->textEdit_Recv->setText(QString("hValue:%1,lValue%2,temp:%3")
38                 .arg(QString::number(myData.hValue, 'g', 12))
39                 .arg(QString::number(myData.lValue, 'g', 12)).arg(myData.temperature));
40     }
41 }
42 
43 void MainWindow::MySocketDisconnected()
44 {
45     ui->btnSend->setEnabled(false);
46 
47     ui->btnConnect->setText("連接");
48     qDebug()<<"Disconnected!";
49 }
50 
51 void MainWindow::on_btnSend_clicked()
52 {
53     struct NetDataBase_t myData;
54     myData.dataHeader.nDataSize = 1;
55     myData.dataHeader.nDataSize = sizeof(struct NetDataBase_t) - sizeof(struct NetDataHeader_t);
56     myData.hValue = -1.234567890;
57     myData.lValue = 0.123456789;
58     myData.temperature = 23.1;
59 
60     QByteArray data;
61     data.resize(sizeof(struct NetDataBase_t));
62     memcpy(data.data(), &myData, sizeof(struct NetDataBase_t));
63 
64     mySocket->write(data);
65     mySocket->flush();
66 }
67 
68 void MainWindow::on_btnConnect_clicked()
69 {
70     if(ui->btnConnect->text() == tr("連接")) {
71         QString ip;
72         int port;
73         ip = ui->lineEdit_IP->text();
74         port = ui->lineEdit_Port->text().toInt();
75 
76         mySocket->abort();
77         mySocket->connectToHost(ip, port);
78         if(!mySocket->waitForConnected(30000)) {
79             qDebug()<<"Connection failed!";
80             return;
81         }
82         qDebug()<<"Connect successfully!";
83 
84         ui->btnSend->setEnabled(true);
85         ui->btnConnect->setText("斷開連接");
86     } else {
87         mySocket->disconnectFromHost();
88         ui->btnConnect->setText("連接");
89         ui->btnSend->setEnabled(false);
90     }
91 }

同時使用QBytearray的data函數轉換成字符串形式。

然后用memcpy進行復制和轉換。

 

--------------------------------

或許還可以這樣,沒試過:把結構體轉換成一個QString型的就可以write了,然后就可以了。
用QString::fromascii()去轉換吧,或者是QString::fromlocal8bit()去轉換


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM