

void KLW_3Test::readMsg()
{
   // qDebug()<<"have read";
    while(m_udpSocket->hasPendingDatagrams())
    {
        QByteArray datagram;
        qint64 length;
        QHostAddress targetIP;//IP
        INT16U targetPort;
        length =m_udpSocket->pendingDatagramSize();
        if(length != 142)
        {
            break;
        }
        datagram.resize(static_cast<int>(length));
        length = m_udpSocket->readDatagram(datagram.data(),600 ,&targetIP,&targetPort);
        if(length != 142)
        {
            break;
        }
        // ToolFunc::ByteArrayTocharArrCopy(rxdbuff,datagram,length);
        QString ip = targetIP.toString();
        ui->SendIPLEdit->setText(ip.mid(7));
        ui->SendPortLEdit->setText(QString::number(targetPort));
        //处理接收数据
        INT8U  KLW3_Rx_buffer[142];//接收缓冲区
        INT16U KLW3_Rx_counter=0;//接收字节数
        ToolFunc::ByteArrayTocharArrCopy(KLW3_Rx_buffer,datagram,length);
        //校验码
        INT16U  I_temp = 0; // 两个字节
        for(int i = 0;i<(length-2);i++)
        {
           I_temp += KLW3_Rx_buffer[i];
        }
        INT8U checkData1 = (INT8U)((I_temp>>8)&0x00ff);
        INT8U checkData2 = (INT8U)(I_temp&0x00ff);
        //协议处理
        if((checkData1 == KLW3_Rx_buffer[140])&&(checkData2 == KLW3_Rx_buffer[141]))
        {
           if((KLW3_Rx_buffer[0] == 0x53)&&(KLW3_Rx_buffer[1] == 0x54)&&(KLW3_Rx_buffer[2] == 0x41)&&(KLW3_Rx_buffer[3] == 0x54)&&(KLW3_Rx_buffer[4] == 0x45)&&(KLW3_Rx_buffer[25] == 0x8e))
           {//指令0x01
                  //显示数据
                  QString displayStr;
                  QString str;
                  QString TitleStr;
                  //time=====================================
                                   //---------------
                  TitleStr = QString("meter time：   ");
                  str = QString("%1%2-%3%4-%5%6  %7%8:%9%10:%11%12").arg(KLW3_Rx_buffer[7]-0x30,1,10,QLatin1Char('0')).arg(KLW3_Rx_buffer[8]-0x30,1,10,QLatin1Char('0')).\
                          arg(KLW3_Rx_buffer[10]-0x30,1,10,QLatin1Char('0')).arg(KLW3_Rx_buffer[11]-0x30,1,10,QLatin1Char('0')).\
                          arg(KLW3_Rx_buffer[13]-0x30,1,10,QLatin1Char('0')).arg(KLW3_Rx_buffer[14]-0x30,1,10,QLatin1Char('0')).\
                          arg(KLW3_Rx_buffer[16]-0x30,1,10,QLatin1Char('0')).arg(KLW3_Rx_buffer[17]-0x30,1,10,QLatin1Char('0')).\
                          arg(KLW3_Rx_buffer[19]-0x30,1,10,QLatin1Char('0')).arg(KLW3_Rx_buffer[20]-0x30,1,10,QLatin1Char('0')).\
                          arg(KLW3_Rx_buffer[22]-0x30,1,10,QLatin1Char('0')).arg(KLW3_Rx_buffer[23]-0x30,1,10,QLatin1Char('0'));
                  TitleStr   = TitleStr + str + QString("\n");
                  displayStr = displayStr+TitleStr;
                  //=================================
                                   //---------------
                  TitleStr = QString("point number： ");
                  str = QString("%1").arg(KLW3_Rx_buffer[27]);
                  TitleStr   = TitleStr + str + QString("\n");
                  displayStr = displayStr+TitleStr;
                  //=================================
                  //=================================
                                   //---------------
                  TitleStr = QString("cell number： ");
                  str = QString("%1").arg(KLW3_Rx_buffer[26]);
                  TitleStr   = TitleStr + str + QString("\n");
                  displayStr = displayStr+TitleStr;
                  //=================================
                                   //---------------
                  TitleStr = QString("weight valid： ");
                   if(KLW3_Rx_buffer[40]&0x20)
                   {
                       str = QString("true");
                   }
                   else {
                       str = QString("false");
                   }
                   TitleStr   = TitleStr + str + QString("\n");
                   displayStr = displayStr+TitleStr;
                  //=================================
                   //=================================
                                    //---------------
                   TitleStr = QString("weigh state：");
                    if(KLW3_Rx_buffer[40]&0x01)
                    {
                        str = QString("--s-- ");
                    }
                    else {
                        str = QString("      ");
                    }
                    TitleStr   = TitleStr + str;
                    if(KLW3_Rx_buffer[40]&0x02)
                    {
                        str = QString("over  ");
                    }
                    else {
                        str = QString("      ");
                    }
                    TitleStr   = TitleStr + str;
                    if(KLW3_Rx_buffer[40]&0x04)
                    {
                        str = QString(" steedy ");
                    }
                    else {
                        str = QString("        ");
                    }
                    TitleStr   = TitleStr + str;
                    if(KLW3_Rx_buffer[40]&0x08)
                    {
                        str = QString(" tare ");
                    }
                    else {
                        str = QString("      ");
                    }
                    TitleStr   = TitleStr + str;
                    if(KLW3_Rx_buffer[40]&0x10)
                    {
                        str = QString(" zero ");
                    }
                    else {
                        str = QString("      ");
                    }
                    TitleStr   = TitleStr + str;
                    displayStr = displayStr+TitleStr+ QString("\n");
                   //=================================
                   INT8U *p;
                   INT32S SL_temp;
                   p = (INT8U *)&SL_temp;
                   *(p+0) = KLW3_Rx_buffer[48];//================读毛重
                   *(p+1) = KLW3_Rx_buffer[49];
                   *(p+2) = KLW3_Rx_buffer[50];
                   *(p+3) = KLW3_Rx_buffer[51];
                   //=================================
                                    //---------------
                   TitleStr = QString("Gross：        ");
                   if(KLW3_Rx_buffer[40]&0x20)
                   {
                      str = QString("%1  (do not mark the point)").arg(SL_temp,7,10);
                   }
                   else
                   {
                       str = QString("");
                   }
                   TitleStr   = TitleStr + str + QString("\n");//================显示毛重
                   displayStr = displayStr+TitleStr;
                   //=================================
                   p = (INT8U *)&SL_temp;
                   *(p+0) = KLW3_Rx_buffer[52];
                   *(p+1) = KLW3_Rx_buffer[53];
                   *(p+2) = KLW3_Rx_buffer[54];
                   *(p+3) = KLW3_Rx_buffer[55];
                   //=================================
                                    //---------------
                   TitleStr = QString("Tare：         ");
                   if(KLW3_Rx_buffer[40]&0x20)
                   {
                      str = QString("%1  (do not mark the point)").arg(SL_temp,7,10);
                   }
                   else
                   {
                       str = QString("");
                   }
                   TitleStr   = TitleStr + str + QString("\n");
                   displayStr = displayStr+TitleStr;
                   //=================================
                   p = (INT8U *)&SL_temp;
                   *(p+0) = KLW3_Rx_buffer[56];
                   *(p+1) = KLW3_Rx_buffer[57];
                   *(p+2) = KLW3_Rx_buffer[58];
                   *(p+3) = KLW3_Rx_buffer[59];
                   //=================================
                                    //---------------
                   TitleStr = QString("Net：          ");
                   if(KLW3_Rx_buffer[40]&0x20)
                   {
                      str = QString("%1  (do not mark the point)").arg(SL_temp,7,10);
                   }
                   else
                   {
                       str = QString("");
                   }
                   TitleStr   = TitleStr + str + QString("\n");
                   displayStr = displayStr+TitleStr;
                   //=================================
                   //=================================
                                    //---------------
                   TitleStr = QString("cell state：    \n====================================================\n");
                   displayStr = displayStr+TitleStr;
                   //标题
                    TitleStr = QString("address     state      ISN   \n");
                    displayStr = displayStr+TitleStr;
                   for(int i=0;i<KLW3_Rx_buffer[26];i++)
                   {
                     TitleStr = QString("%1        ").arg(i+1,2,10);
                     if(KLW3_Rx_buffer[60+(i*5)] == 0)
                     {
                        TitleStr =TitleStr+ QString("comm error\n");
                     }
                     else if(KLW3_Rx_buffer[60+(i*5)] == 1)
                     {
                        TitleStr =TitleStr+ QString("password error\n");
                     }
                     else {
                        TitleStr =TitleStr+ QString(" normal ");
                        float F_temp;
                        p = (INT8U *)&F_temp;
                        *(p+0) = KLW3_Rx_buffer[61+(i*5)];
                        *(p+1) = KLW3_Rx_buffer[62+(i*5)];
                        *(p+2) = KLW3_Rx_buffer[63+(i*5)];
                        *(p+3) = KLW3_Rx_buffer[64+(i*5)];


                        if(F_temp<0)
                        {
                           SL_temp =  F_temp-0.5;
                        }
                        else
                        {
                           SL_temp =  F_temp+0.5;
                        }
                        TitleStr =TitleStr+ QString("    %1\n").arg(SL_temp,6,10);
                     }
                     displayStr = displayStr+TitleStr;
                   }
                   TitleStr = QString("====================================================\n");
                   displayStr = displayStr+TitleStr;


                  ui->readTedit->setText(displayStr);

           }
           else {//指令处理

           }

        }
    }
}

