QT5之UDP/TCP通信上位机

这个项目的内容主要是读取一个光学解调仪的数据并解析成波长和画出光谱图,包含向解调仪发送请求和接收数据。

Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget),_lastPointKey(0)
{
    ui->setupUi(this);
    setFixedSize( this->width(),this->height()); //窗口定大小
    setWindowTitle(tr("IGBT测温系统8通道")); //设置窗口标题

    udpSocket = new QUdpSocket(this);//UDP
    udpSocket->bind(8888); //绑定端口
    connect(udpSocket,&QUdpSocket::readyRead,this,&Widget::dealMsg); //读取udp数据
    connect(ui->buttonSend,SIGNAL(clicked()),this,SLOT(on_buttonSend_clicked()));//发送按钮
    connect(ui->buttonSendL,SIGNAL(clicked()),this,SLOT(on_btnLightPicture()));//发送按钮

    serialport = new QSerialPort; //串口
    windows_init();
    // 显示坐标
    textItem = new QCPItemText(ui->widget);
    connect(ui->widget, &QCustomPlot::mouseMove, this, &Widget::onMouseMove);
}

下面是发送数据和接收数据的Func

//发送按钮,向采集器发送指令:010A5500
void Widget::on_buttonSend_clicked()
{
    temp = true;
    lightPicture = false;
    QString ip = "192.168.0.119"; //对方IP
    quint16 port = 4010;  //对方端口
    //QString ip = ui->lineEditIP->text();              //对方IP
    //quint16 port = ui->lineEditPort->text().toInt();  //对方端口
    //QString str = ui->textEditSend->toPlainText();    //获取文本框的内容

    QByteArray arr;
    arr[0] = 0x01;
    arr[1] = 0x0A;
    arr[2] = 0x55;
    arr[3] = 0x00;
    //将信息发送给指定的IP
    udpSocket->writeDatagram(arr,(QHostAddress)ip,port);
}


//发送请求光谱图的数据0116X(X=0-7)
void Widget::on_btnLightPicture()
{
    temp = false;
    lightPicture = true;
    roadNumber = ui->lineEditRoad->text().toInt();
    QString ip = "192.168.0.119"; //对方IP
    quint16 port = 4010;  //对方端口
    QByteArray arr;
    arr[0] = 0x01;
    arr[1] = 0x16;
    switch (roadNumber)
    {
        case 1:
            arr[2] = 0x00;
            break;
        case 2:
            arr[2] = 0x01;
            break;
        case 3:
            arr[2] = 0x02;
            break;
        case 4:
            arr[2] = 0x03;
            break;
        case 5:
            arr[2] = 0x04;
            break;
        case 6:
            arr[2] = 0x05;
            break;
        case 7:
            arr[2] = 0x06;
            break;
        case 8:
            arr[2] = 0x07;
            break;
        default:
            break;
    }
    //将信息发送给指定的IP
    udpSocket->writeDatagram(arr,(QHostAddress)ip,port);
}

下面是接收和解析解调仪发来的数据,供参考。

//读取采集器发送的内容
void Widget::dealMsg()
{
    while(udpSocket->hasPendingDatagrams()) //拥有等待的数据报,while循环
    {

        QByteArray buf;                     //用于存放接收的数据报
        buf.resize(udpSocket->pendingDatagramSize());
        udpSocket->readDatagram(buf.data(),buf.size());
        QByteArray byteArr  = buf.toHex();   //将数据转换成16进制字符串显示出来
        QString str;
        str+=tr(byteArr);
        int tenArr[4000];
        //qDebug()<<"****str"<<"="<<str<<"*****"<<endl;

        //温度
        if((temp == true)&&(buf.size()>4))
        {
            lightPicture = false;
            qDebug()<<"测量温度";

            QString str11 = str.mid(8,2);//截取字符串
            QString str12 = str.mid(10,2);//截取字符串
            bool ok1;
            bool ok2;
            double num1 = str11.toInt(&ok1,16);
            double num2 = str12.toInt(&ok2,16);
            bochang01 = (num1 + (num2)*256)/1000 + 1520; //波长01
            if(bochang01!=1520) //只有在插入光纤传感器的时候进行下列计算
            {
                wenduJiSuan(ui->k11->text().toDouble(),ui->b1->text().toDouble(),1);
            }

            QString str21 = str.mid(132,2);//截取字符串
            QString str22 = str.mid(134,2);//截取字符串
            bool ok3;
            bool ok4;
            double num3 = str21.toInt(&ok3,16);
            double num4 = str22.toInt(&ok4,16);
            bochang02 = (num3 + (num4)*256)/1000 + 1520; //波长02
            //qDebug()<<"num3"<<num3;
            //qDebug()<<"波长为"<<bochang02;
            if(bochang02!=1520)
            {
                wenduJiSuan(ui->k21->text().toDouble(),ui->b2->text().toDouble(),2);
            }


            QString str31 = str.mid(256,2);//截取字符串
            QString str32 = str.mid(258,2);//截取字符串
            bool ok5;
            bool ok6;
            double num5 = str31.toInt(&ok5,16);
            double num6 = str32.toInt(&ok6,16);
            bochang03 = (num5 + (num6)*256)/1000 + 1520; //波长03
            if(bochang03!=1520)
            {
                wenduJiSuan(ui->k31->text().toDouble(),ui->b3->text().toDouble(),3);
            }


            QString str41 = str.mid(380,2);//截取字符串
            QString str42 = str.mid(382,2);//截取字符串
            bool ok7;
            bool ok8;
            double num7 = str41.toInt(&ok7,16);
            double num8 = str42.toInt(&ok8,16);
            bochang04 = (num7 + (num8)*256)/1000 + 1520; //波长04
            if(bochang04!=1520)
            {
                wenduJiSuan(ui->k41->text().toDouble(),ui->b4->text().toDouble(),4);
            }


            QString str51 = str.mid(504,2);//截取字符串
            QString str52 = str.mid(506,2);//截取字符串
            bool ok9;
            bool ok10;
            double num9 = str51.toInt(&ok9,16);
            double num10 = str52.toInt(&ok10,16);
            bochang05 = (num9 + (num9)*256)/1000 + 1520; //波长05
            if(bochang05!=1520)
            {
                wenduJiSuan(ui->k51->text().toDouble(),ui->b5->text().toDouble(),5);
            }


            QString str61 = str.mid(628,2);//截取字符串
            QString str62 = str.mid(630,2);//截取字符串
            bool ok11;
            bool ok12;
            double num11 = str61.toInt(&ok11,16);
            double num12 = str62.toInt(&ok12,16);
            bochang06 = (num11 + (num12)*256)/1000 + 1520; //波长06
            if(bochang06!=1520)
            {
                wenduJiSuan(ui->k61->text().toDouble(),ui->b6->text().toDouble(),6);
            }


            QString str71 = str.mid(752,2);//截取字符串
            QString str72 = str.mid(752,2);//截取字符串
            bool ok13;
            bool ok14;
            double num13 = str71.toInt(&ok13,16);
            double num14 = str72.toInt(&ok14,16);
            bochang07 = (num13 + (num14)*256)/1000 + 1520; //波长07
            if(bochang07!=1520)
            {
                wenduJiSuan(ui->k71->text().toDouble(),ui->b7->text().toDouble(),7);
            }


            QString str81 = str.mid(132,2);//截取字符串
            QString str82 = str.mid(134,2);//截取字符串
            bool ok15;
            bool ok16;
            double num15 = str81.toInt(&ok15,16);
            double num16 = str82.toInt(&ok16,16);
            bochang08 = (num15 + (num16)*256)/1000 + 1520; //波长08
            if(bochang08!=1520)
            {
                wenduJiSuan(ui->k81->text().toDouble(),ui->b8->text().toDouble(),8);
            }
            lcdDisplay();  //lcdDisplay显示当前通道波长

        }
        //光谱图
        if(lightPicture == true&&buf.size()>=800)//size>800的目的是不让温度的数据造成影响
        {
            temp = false;
            qDebug()<<"光谱图";

            if(str[7]=='0'||str[7]=='1'||str[7]=='2'||str[7]=='3')
            {
                str.remove(0,8);
                newStr.append(str);
            }
            if(str[7]=='4')
            {
                for(int i = 0;i<4000;i++)
                {
                    QString strr = newStr.mid(2*i,2);//截取字符串
                    bool ok;
                    int num = strr.toInt(&ok,16);
                    tenArr[i] = num;
                    //qDebug()<<"****tenArr"<<i<<"="<<tenArr[i]<<"*****"<<endl;
                }
                QVector<double> x(2000),y(2000); //x为波长个数,y代表强度
                for(int i = 0;i<2000;i++)
                {
                    x[i] = 1528.8+i*0.02;
                    y[i] = ((short int)(tenArr[2*i]+256*(tenArr[2*i+1])))/10;

                }
                ui->widget->graph(0)->setData(x,y); //画图,给图设置数据
                ui->widget->replot();       //画图
                newStr.clear();
            }
        }
    }
}

 

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值