QT学习---网络编程

为了方便调试,网上找来客户端例程,修改成上位机控制端。

 

QT学习---网络编程_第1张图片

QT学习---网络编程_第2张图片

 

1、pro文件

从这里,我们可以看到,必须要在.pro文件中添加QT += network才可以进行网络编程,否则是访问不到头文件的。
客户端读写相对简单,我们看一下代码头文件:
#-------------------------------------------------
#
# Project created by QtCreator 2019-08-25T14:10:09
#
#-------------------------------------------------

QT       += core gui
QT += network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = myFrist
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += \
        clientwidget.cpp \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        clientwidget.h \
        mainwindow.h

FORMS += \
        clientwidget.ui \
        mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

2、client.cpp

#include "clientwidget.h"
#include "ui_clientwidget.h"
#include
#include 
#include 
#include 
#include 

ClientWidget::ClientWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ClientWidget)
{
    ui->setupUi(this);
    //初始化套接字
    //tcpSocket = NULL;
    //分配空间,指定父对象
    tcpSocket = new QTcpSocket(this);
    //建立连接的信号槽
    connect(tcpSocket,&QTcpSocket::connected,
            [=]()
            {
                 ui->textEditRead->setText("恭喜,成功连接服务器!");
                 ui->textEditguid->setText("成功连接服务器!");

            }
            );
    //连接建立后收到服务器数据的信号槽
    connect(tcpSocket,&QTcpSocket::readyRead,
            [=]()
           {
         //获取对方发送的内容
            qint16 n =5;
        QByteArray array = tcpSocket->readAll();
       quint8 buf[64] = {0};
//        buf[0]  = array[1];
       QString str1 = "Welcome";
         // qint64 read(char *data, qint64 maxlen);
        if(array[7] == (char)3)
        {
            ui->textEditRead->append("参数设置为:");

 //           str1.sprintf( "year:%d\tmoth:%d\tday:%d", ((quint8)array[20]+(array[19]<<8)),
//                    array[22]+(array[21]<<8), array[24]+(array[23]<<8));
            str1.sprintf( "年:%d\t月:%d\t日:%d", ((quint8)array[20]+(array[19]<<8)),
                    array[22]+(array[21]<<8), array[24]+(array[23]<<8));
            ui->textEditRead->append(str1);
//            str1.sprintf( "V1:%d\tV2:%d\tspeed3:%d",array[10]+(array[9]<<8),
//                   (quint8)array[12]+(array[11]<<8),  array[14]+(array[13]<<8));
            str1.sprintf( "消毒桶:%d\t饮水桶:%d\t流速:%d",array[10]+(array[9]<<8),
                    (quint8)array[12]+(array[11]<<8),  array[14]+(array[13]<<8));
            ui->textEditRead->append(str1);
//            str1.sprintf( "dree:%d\tweigt:%d",array[16]+(array[15]<<8),
//                    array[18]+(array[17]<<8));
            str1.sprintf( "溶解度:%d\t药重:%d",array[16]+(array[15]<<8),
                    array[18]+(array[17]<<8));
            ui->textEditRead->append(str1);
            //sprintf(buf, "year:%d-moth:%d-day:%d", array[7], array[7], array[7]);

        }else
        {//显示到编辑框
        ui->textEditRead->append("Recv:");
        ui->textEditRead->append(array);//append,添加内容
        }


            }
            );
    //设置窗口标题
    setWindowTitle("TCP客户端");
}

ClientWidget::~ClientWidget()
{
    delete ui;
}

void ClientWidget::on_buttonConnect_clicked()
{
    //获取服务器IP和端口
    QString ip = ui->lineEditIP->text();
    quint16 port = ui->lineEditPort->text().toUShort();
    //主动连接服务器
    tcpSocket->connectToHost(QHostAddress(ip),port);
}

void ClientWidget::on_buttonSend_clicked()
{
    //获取编辑框内容
     QString str = ui->textEditWrite->toPlainText();
     //发送数据
     tcpSocket->write( str.toUtf8().data() );

}

void ClientWidget::on_buttonClose_clicked()
{ //主动断开连接
    tcpSocket->disconnectFromHost();
    ui->textEditguid->setText("已断开连接!");
}

QByteArray InnitialQByteArry(qint8 in[],int len)//输入int数组和数组长度,将数组中的值存入QByteArry中
{
    QByteArray byteArry;
    byteArry.resize(len);
    for (int i=0;iwrite( str.toUtf8().data() );
        qint8 buf[43] = {0x00,0x00,0x00,0x00,0x00,0x06,0x01,0x10,0x00,0x00,0x00,0x10,0x20};
        num = ui->lineEditset1->text().toShort();
        buf[n+0] = num>>8;
        buf[n+1] =(qint8) num;
        num = ui->lineEditset2->text().toShort();
        buf[n+2] = num>>8;
        buf[n+3] =(qint8) num;
        num = ui->lineEditset3->text().toShort();
        buf[n+4] = num>>8;
        buf[n+5] =(qint8) num;
        num = ui->lineEditset4->text().toShort();
        buf[n+6] = num>>8;
        buf[n+7] =(qint8) num;
        num = ui->lineEditset5->text().toShort();
        buf[n+8] = num>>8;
        buf[n+9] =(qint8) num;
        num = ui->lineEditset6->text().toShort();
        buf[n+10] = num>>8;
        buf[n+11] =(qint8) num;
        num = ui->lineEditset7->text().toShort();
        buf[n+12] = num>>8;
        buf[n+13] =(qint8) num;
        num = ui->lineEditset7_2->text().toShort();
        buf[n+14] = num>>8;
        buf[n+15] =(qint8) num;
        num = ui->lineEditset8->text().toShort();
        buf[n+16] = num>>8;
        buf[n+17] =(qint8) num;
        num = ui->lineEditset8_2->text().toShort();
        buf[n+18] = num>>8;
        buf[n+19] =(qint8) num;
        num = ui->lineEditset9->text().toShort();
        buf[n+20] = num>>8;
        buf[n+21] =(qint8) num;
        num = ui->lineEditset9_2->text().toShort();
        buf[n+22] = num>>8;
        buf[n+23] =(qint8) num;
        num = ui->lineEditset10->text().toShort();
        buf[n+24] = num>>8;
        buf[n+25] =(qint8) num;
        num = ui->lineEditset10_2->text().toShort();
        buf[n+26] = num>>8;
        buf[n+27] =(qint8) num;
        //lineEditset7_2


        num = 0;

        if(ui->checkBox_1->isChecked() ){
            qDebug("checkBox_1");
            num  |= 1;
        }
        if(ui->checkBox_2->isChecked() ){
            qDebug("checkBox_2");
            num  |= 1<<1;
        }
        if(ui->checkBox_3->isChecked() ){
            qDebug("checkBox_3");
            num  |= 1<<2;

        }
        if(ui->checkBox_4->isChecked() ){
            qDebug("checkBox_4");
            num  |= 1<<3;

        }
        if(ui->checkBox_5->isChecked() ){
            qDebug("checkBox_5");
            num  |= 1<<4;

        }
        if(ui->checkBox_6->isChecked() ){
            qDebug("checkBox_6");
            num  |= 1<<5;

        }
//         qDebug()<<"num:";
//        qDebug()<>8;
        buf[n+29] =(qint8) num;
        int siz =sizeof(buf)/sizeof(buf[0]);
       QByteArray sendM=InnitialQByteArry(buf,siz);
       tcpSocket->write( sendM);
}

void ClientWidget::on_buttonRead_clicked()
{

     //   qint16 addr = ui->lineEditAddr->text().toShort();
     ///   qint16 num = ui->lineEditNum->text().toShort();
        //发送数据
        //tcpSocket->write( str.toUtf8().data() );
        qint8 buf[12] = {0x00,0x00,0x00,0x00,0x00,0x06,0x01,0x03,0x00,0x00,0x00,0x20};
     //   buf[8] = addr>>8;
     //   buf[9] =(qint8) addr;
     //   buf[10] = num>>8;
     //   buf[11] =(qint8) num;
         int siz =sizeof(buf)/sizeof(buf[0]);
        QByteArray sendM=InnitialQByteArry(buf,siz);
        tcpSocket->write( sendM);
       ui->textEditRead->setText("读取设置内容为:");
}

3、UI文件



 ClientWidget
 
  
   
    0
    0
    946
    641
   
  
  
   
    0
    300
   
  
  
   Form
  
  
   
    
     
      
       450
       580
      
     
     
      
       450
       560
      
     
     
      数据读写
     
     
      
       true
      
      
       
        20
        30
        400
        500
       
      
      
       
        0
        0
       
      
      
       
        400
        500
       
      
      
       
        400
        500
       
      
      
       
        2
        0
       
      
      
       
        微软雅黑
        50
        false
       
      
      
       数据设置
      
      
       
        
         30
         30
         100
         30
        
       
       
        
         100
         20
        
       
       
        
         100
         30
        
       
       
        
         微软雅黑
         10
         50
         false
        
       
       
        消毒桶体积:
       
      
      
       
        
         130
         30
         71
         21
        
       
       
        45
       
      
      
       
        
         210
         60
         100
         30
        
       
       
        
         100
         20
        
       
       
        
         100
         30
        
       
       
        
         微软雅黑
         10
         50
         false
        
       
       
        药物重量:
       
      
      
       
        
         130
         70
         71
         21
        
       
       
        1000
       
      
      
       
        
         260
         390
         93
         71
        
       
       
        参数设置
       
      
      
       
        
         30
         90
         100
         30
        
       
       
        
         100
         20
        
       
       
        
         100
         30
        
       
       
        
         微软雅黑
         10
         50
         false
        
       
       
        水泵流量:
       
      
      
       
        
         30
         60
         100
         30
        
       
       
        
         100
         20
        
       
       
        
         100
         30
        
       
       
        
         微软雅黑
         10
         50
         false
        
       
       
        饮水桶体积:
       
      
      
       
        
         220
         30
         100
         30
        
       
       
        
         100
         20
        
       
       
        
         100
         30
        
       
       
        
         微软雅黑
         10
         50
         false
        
       
       
        稀释度:
       
      
      
       
        
         130
         100
         71
         21
        
       
       
        5
       
      
      
       
        
         300
         30
         71
         21
        
       
       
        2
       
      
      
       
        
         300
         70
         71
         21
        
       
       
        10000
       
      
      
       
        
         30
         140
         100
         30
        
       
       
        
         100
         20
        
       
       
        
         100
         30
        
       
       
        
         微软雅黑
         10
         50
         false
        
       
       
        加药年月日:
       
      
      
       
        
         130
         150
         51
         21
        
       
       
        2019
       
      
      
       
        
         190
         150
         51
         21
        
       
       
        8
       
      
      
       
        
         30
         210
         100
         30
        
       
       
        
         100
         20
        
       
       
        
         100
         30
        
       
       
        
         微软雅黑
         10
         50
         false
        
       
       
        限制时间1止:
       
      
      
       
        
         140
         220
         51
         21
        
       
       
        12
       
      
      
       
        
         140
         190
         51
         21
        
       
       
        9
       
      
      
       
        
         30
         180
         100
         30
        
       
       
        
         100
         20
        
       
       
        
         100
         30
        
       
       
        
         微软雅黑
         10
         50
         false
        
       
       
        限制时间1起:
       
      
      
       
        
         140
         250
         51
         21
        
       
       
        14
       
      
      
       
        
         30
         240
         100
         30
        
       
       
        
         100
         20
        
       
       
        
         100
         30
        
       
       
        
         微软雅黑
         10
         50
         false
        
       
       
        限制时间2起:
       
      
      
       
        
         250
         150
         51
         21
        
       
       
        26
       
      
      
       
        
         200
         190
         51
         21
        
       
       
        00
       
      
      
       
        
         200
         220
         51
         21
        
       
       
        30
       
      
      
       
        
         200
         250
         51
         21
        
       
       
        30
       
      
      
       
        
         30
         320
         91
         19
        
       
       
        
         微软雅黑
         10
         50
         false
        
       
       
        1单元
       
      
      
       
        
         30
         350
         91
         19
        
       
       
        
         微软雅黑
         10
         50
         false
        
       
       
        2单元
       
      
      
       
        
         30
         380
         91
         19
        
       
       
        
         微软雅黑
         10
         50
         false
        
       
       
        3单元
       
      
      
       
        
         30
         410
         91
         19
        
       
       
        
         微软雅黑
         10
         50
         false
        
       
       
        4单元
       
      
      
       
        
         30
         440
         91
         19
        
       
       
        
         微软雅黑
         10
         50
         false
        
       
       
        5单元
       
      
      
       
        
         30
         470
         91
         19
        
       
       
        
         微软雅黑
         10
         50
         false
        
       
       
        6单元
       
      
      
       
        
         20
         280
         120
         30
        
       
       
        
         120
         20
        
       
       
        
         100
         30
        
       
       
        
         微软雅黑
         10
         50
         false
        
       
       
        喂药单元选择:
       
      
     
    
   
   
    
     
      
       300
       560
      
     
     
      连接
     
     
      
       
        20
        250
        430
        300
       
      
      
       
        430
        300
       
      
      
       
        430
        300
       
      
      
       数据显示
      
      
       
        
         20
         20
         331
         200
        
       
       
        
         16777215
         200
        
       
       
        true
       
      
      
       
        
         20
         230
         331
         51
        
       
       
        
         16777215
         100
        
       
      
      
       
        
         360
         230
         71
         51
        
       
       
        发送数据
       
      
      
       
        
         360
         20
         71
         51
        
       
       
        读取参数
       
      
     
     
      
       
        20
        20
        421
        200
       
      
      
       
        400
        200
       
      
      
       
        16777215
        200
       
      
      
       连接
      
      
       
        
         210
         40
         93
         28
        
       
       
        连接服务器
       
      
      
       
        
         20
         30
         100
         30
        
       
       
        
         100
         20
        
       
       
        
         100
         30
        
       
       
        服务器端口:
       
      
      
       
        
         120
         40
         71
         21
        
       
       
        502
       
      
      
       
        
         20
         80
         93
         21
        
       
       
        服务器IP:
       
      
      
       
        
         110
         80
         254
         31
        
       
       
        192.168.0.200
       
      
      
       
        
         20
         120
         100
         30
        
       
       
        
         100
         20
        
       
       
        
         100
         30
        
       
       
        连接状态:
       
      
      
       
        
         110
         120
         201
         31
        
       
       
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'SimSun'; font-size:9pt; font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">未连接</p></body></html>
       
      
      
       
        
         310
         40
         93
         28
        
       
       
        关闭连接
       
      
     
    
   
  
 
 
 

 

 

 

 

 

 

你可能感兴趣的:(物联网)