for loop question

Hello ,
I try to get 25 data from a can bus in the same time using a Qt/c++ application.
So in every iteration I receive a frame I convert it then I fill an array Outputs[] .
I am a student , and I still learning c++ development .
So I will pass my code , And suggest me if I did a good design, I can get what i want with this code .
thank you
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
float myData;
    std::array<QString,25> Inputs{"real","double","double","double","double","double","double","double","double","double"
                                 ,"real","real","real","real","real","real","real","real","double","double","boolean","double"
                                 ,"uint32","boolean","boolean"};
    QByteArray payloadupload= QByteArray::fromHex("F504");
    QByteArray payloadupload8byte= QByteArray::fromHex("F508");
    QByteArray Testsend= QByteArray::fromHex("FF"); // réponse static
    QByteArray payloadrpm= QByteArray::fromHex("F600000000180010"); // Rpm @

    QString ID = "101";
    const uint frameId = ID.toUInt(nullptr, 16);

    QCanBusFrame firstframetosend;
    QCanBusFrame frameupload;

        QCanBusDevice *device = QCanBus::instance()->createDevice(
                    QStringLiteral("socketcan"), QStringLiteral("can0"));

        if(device->connectDevice()){ //changer avant la validation

            //Récuperation de Rpm
           //Envoie de l'@ Rpm

            firstframetosend.setFrameId(frameId);
            firstframetosend.setPayload(payloadrpm);
            device->writeFrame(firstframetosend);

            // fin @Rpm
           device->waitForFramesReceived(3000);
           QCanBusFrame rpmreponse= device->readFrame();
           QByteArray Testreponse = rpmreponse.payload();

         if(Testreponse==Testsend){            //test de la reponse

               for(size_t i=0;i<Inputs.size();i++){
                    //Envoie de la commande upload
                    qDebug() << "Input type" << Inputs[i];

                    if(Inputs[i]=="double") {
                        frameupload.setFrameId(frameId);

                        frameupload.setPayload(payloadupload8byte);
                        qDebug() << "type is double : upload commande is" << payloadupload8byte;
                        device->writeFrame(frameupload);

                    }

                    else{
                        frameupload.setFrameId(frameId);

                        frameupload.setPayload(payloadupload);
                        qDebug() << "type is not double : upload commande is" << payloadupload;
                        device->writeFrame(frameupload);

                    }

                    // fin de la commande upload

                 device->waitForFramesReceived(3000);
                 QCanBusFrame framereceived = device->readFrame(); // Reception 1ére données
                 QByteArray data = QByteArray::fromHex(framereceived.payload());

                qDebug() << QByteArray::fromHex(framereceived.payload()) << " Data Data&size" << QByteArray::fromHex(framereceived.payload()).size();

                  //  QByteArray data = QByteArray::fromHex("FF42480000");
                    QDataStream stream(data);
                    qDebug() << data.size() << " Data Data&size" ;
                    if (data.size()== 5){

                        stream.setFloatingPointPrecision(QDataStream::SinglePrecision);

                    }else {
                        stream.setFloatingPointPrecision(QDataStream::DoublePrecision);

                    }

                    quint8 byte;
                    stream >> byte; // skip first byte
                    stream >> myData; // read data
                    qDebug()  << "this is data"<< myData ;
                    QString DatatoString =QString::number(myData);
                    Outputs[i]=DatatoString;
                    qDebug() << "In outputs data" << Outputs[i];


                      //fin récuperation
}

        m_Valuespeed=Outputs[0].toDouble();
        m_ValueId=Outputs[1].toFloat();
        m_ValueIQ=Outputs[2].toFloat();
        m_ValueIdRef=Outputs[3].toFloat();
        m_ValueIQRef=Outputs[4].toFloat();
        m_ValueVD=Outputs[5].toFloat();
        m_ValueVQ=Outputs[6].toFloat();
        m_ValueVDRef=Outputs[7].toFloat();
        m_ValueVQRef=Outputs[8].toFloat();
        m_valueDCvolt=Outputs[9].toFloat();
        m_ValueCPU1=Outputs[10].toFloat();
        m_ValueCPU2=Outputs[11].toFloat();
        m_ValueCPU3=Outputs[12].toFloat();
        m_ValueCPU4=Outputs[13].toFloat();
        m_ValueCPU5=Outputs[14].toFloat();
        m_ValueCPU6=Outputs[15].toFloat();
        m_ValueCPU7=Outputs[16].toFloat();
        m_ValueCPU8=Outputs[17].toFloat();
        m_ValueTorque=Outputs[18].toFloat();
        m_ValueTorqueMax=Outputs[19].toFloat();
        m_directiontext=Outputs[20];
        m_motiontext=Outputs[21];
        m_nbrevoltext=Outputs[22];
        m_degradedtext=Outputs[23];
        m_dischtext=Outputs[24];

         }

}

}
some minor stuff:
Indents are off, see 117 and 119 for example of bracket misalignment.
line 2, consider having just one string with "double" that you reuse rather than repeat all that in a hard-coded list.
try to avoid unnecessary string to number or number to string conversions (eg fromhex). If you need it, you have to do it, but the less you do those, the better.

major: try to keep GUI elements and do-work code apart as much as possible. Ideally your canbus tools could read and write to the device and would work in a new program that was for the console, or used MFC, or whatever else, and there would be no references to anything QT in the canbus object. Provide an interface that QT can use to get what it needs into the GUI, but don't embed the QT in the tools.

its up to you to test it to see if it works. It looks reasonable apart from the above.
Last edited on
Topic archived. No new replies allowed.