mercredi 18 mars 2020

C++ object does not save variable values

I am a beginner C++ programmer and currently trying to modify an existing C++ project to be more flexible. A communication from a raspberry pi hat to sensors should be established:

Communication.cpp

 vector<PortMax> ports;
 void Communication::Communication_setup() {
    // Create hardware setup
    hardware = HardwareRaspberry();
    hardware.begin();

    // Create drivers
    Max *pDriver01 = new Max(DRIVER01, &hardware);
    Max *pDriver23 = new Max(DRIVER23, &hardware);
    // Create ports
    ports.push_back(PortMax(pDriver01, PORT0PORT));
    ports.push_back(PortMax(pDriver01, PORT1PORT));
    // Start communication
    for(auto nr: ports) {
        nr.begin();
    }

uint8_t Communication::getSequence(int port_nr) {
     uint8_t retVal = ports.at(port_nr).getSequencePort();
}

PortMax.cpp

void PortMax::begin() {
     uint8_t pData[3];
     readDirectParameterPage(0x03, pData);
     mSequenceType_ = uint8_t((pData[0] >> 1) & 0x07);
     cout << "MSequenceType: " << mSequenceType_ << endl;
}
uint8_t PortMax::getSequencePort() {
     return mSequenceType_;
}

PortMax.h

class PortMax {
     uint8_t mSequenceType_ = 0;
     void begin();
     uint8_t getSequencePort();
}

I have the problem that my variable (in this case mSequenceType_) is saved correctly under the name when the connection is established and also shows the correct value at cout. But as soon as I jump out of the begin() function and want to get the mSequenceType_ from port0 with getSequence() only the initial value is returned. For this reason I think that my variable is only assigned locally and not for my whole object. Do you see what's wrong with my code?

Aucun commentaire:

Enregistrer un commentaire