簡體   English   中英

Arduino,在代碼執行過程中丟失變量值

[英]Arduino, losing value of variable during code execution

我在 arduino 草圖上遇到了一個奇怪的問題。 我的草圖很復雜,所以我決定在課程中寫作。 基本上它通過 JSON 格式的 TCP 連接接收一些數據,並根據解碼后的數據進行一些操作。

我使用庫“Arduino Json”在 CLASS“PacketManager”中解碼接收到的 JSON。 然后我將 JSON 對象傳遞給“Bollard”類以執行命令(代碼如下)。 奇怪的是,在方法“CommandExecutor”中的“Bollard”類上,我訪問了我傳遞的數據,我可以使用它們,但在同一方法的代碼中的某個時刻,數據變為 0。當然,我已經檢查數據沒有在方法中被覆蓋。 草圖沒有任何中斷或多線程。 似乎變量被取消分配的形式內存或內存塊被覆蓋。

/// 接收 JSON 的類數據包管理器的片段

void PacketManager::readPacket(){
resultRead reads=socket.readSocket();  //get data from soket
if(reads.mainCounter>0){  // if data
  delay(150);
  StaticJsonBuffer<200> jsonBuffer; //create a static json buffer form the library ArduinoJson.h
  JsonObject& incomingFrame = jsonBuffer.parseObject((char*)reads.frame); //craete a JSON oblect 
  delay(150);
  incomingFrame.printTo(Serial);  // print the Incoming JSON
  Serial.println();
  if(incomingFrame.success()){ // check if JSON get decoded correclty
    Serial.println("JSON DECODED SUCCESSFULLY");
    unsigned int  eventType=incomingFrame["eventType"];
    unsigned int  packetNumber=incomingFrame["packetNumber"];
    if(eventType==ACK_STANDARD_MESSAGE){ //  ACK standard case
      if(this->checkACK(packetNumber)){  // CHECK If the incoming ACK found a corrispondence with the packet sended
       #ifdef DEBUG_MODE
          Serial.println("ACK RECEIVED");
        #endif
        bollard.commandExecute(incomingFrame);  // if ack is in response of a sended command execute a command form the class bollard.
      }
    }
   ................ CODE CONTINUE. THE ERROR IS ON METHOD bollard.commandExecute(incomingFrame)

/////////////// 使用 JSON 對象的類系柱的片段

commandResult* BikeBollard::commandExecute(JsonObject& incomingFrame){
  unsigned int* statusResponse= new unsigned int;  // CREATE STATIC POINTER
  unsigned int* eventType= new unsigned int;
  unsigned int* commandType= new unsigned int;
  unsigned int* packetNumber= new unsigned int;
  // GET DATA FORM THE JSON OBJECT
  unsigned int statusResponse_volatile=(unsigned int)incomingFrame["status"];
  unsigned int eventType_volatile=(unsigned int)incomingFrame["eventType"]; 
  unsigned int commandType_volatile=(unsigned int)incomingFrame["commandType"]; 
  unsigned int packetNumber_volatile=(unsigned int)incomingFrame["packetNumber"];
  // ASSIGN TO THE STATIC POINTED VALUE
  *statusResponse=statusResponse_volatile;
  *eventType=eventType_volatile;
  *commandType=commandType_volatile;
  *packetNumber=packetNumber_volatile;
  commandResult* result;
  Serial.print("STATUS OF THE RESPONSE ");Serial.println(*statusResponse);  // correct value
  Serial.print("EVENT TYPE ");Serial.println(*eventType);  // correct value
  Serial.print("COMMAND TYPE ");Serial.println(*commandType); // correct value
  Serial.print("PACKET NUMBER ");Serial.println(*packetNumber); // correct value

    ///////////////////////////// FROM HERE SOMETIMES THE VALUES *commandType AND *eventType GO TO 0 WITHOUT A REASON /////////////////////////// 

  if(*eventType==ACK_STANDARD_MESSAGE){ //  ACK vase
     Serial.print("STATUS OF THE RESPONSE ");Serial.println(*statusResponse);  // correct value
     *eventType=*commandType;
     Serial.print("STATUS OF THE RESPONSE ");Serial.println(*statusResponse);  // correct value
     result->ackPriority=false;
  } 

  result->type=*eventType;
  #ifdef DEBUG_MODE
    Serial.print("BOLLARD: EXECUTE COMMAND "); Serial.println(*eventType);
  #endif
  switch(*eventType){ // wrong value pass form the correct value to 0 sometimes
    case MSG_UP:
    {
      Serial.println("ACK MSG_UP Received");
      result=this->executeMessageUp(*eventType, *packetNumber);  
      break;
    }
    case MSG_LATCH:
    {
      Serial.println("ACK MSG_LATCH Received");
      this->executeMessageLatch(*eventType, *packetNumber); 

      break;
    }
    case MSG_UP_OK:
    {
      // DO SOMETHING
      break;
    }
    case MSG_UP_ERROR:
    {
     // DO SOMETHING
      break;
    }
    case MSG_PRESENT:
    {
     // DO SOMETHING
      break;
    }
    case LOGIN_MESSAGE:
    {
      Serial.println("NOTICE: Logged IN");
      this->loggedIn=true;
      break;
    }  
    case MSG_CARD:
    {
      Serial.println("ACK MSG_CARD Received");
      if(*statusResponse==1){
        Serial.println("DO EXECUTE MSG-UP");
        result=this->executeMessageUp(*eventType, *packetNumber);
      }
      else {
        //DO SOMETHING
      }
      break;
    }
    case MSG_BOLLARD_ACTIVATION:
    {

     Serial.print("STATUS OF THE RESPONSE ");Serial.println(*statusResponse);  // 0  ERROR *statusResponse changed it's value.

     if(*statusResponse==1) {
      if(!this->isActive)checkBollardStatusWaitingTime=CHECK_BOLLARD_STATUS_TIME+1; // set bolard to active
      this->isActive=true;
     }
     else{
      if(this->isActive)checkBollardStatusWaitingTime=CHECK_BOLLARD_STATUS_TIME+1; // check againg the status
      this->isActive=false;
     }
      break;
    }
    default:{
      Serial.print("NOT CODED EVENT "); Serial.println(*eventType);
     result->type=0;
     #ifdef DEBUG_MODE
      Serial.println("Default Message Expired");
    #endif
      break;
    }
  }
  delete statusResponse;
  delete eventType;
  delete commandType;
  delete packetNumber;

  return result;
}

希望有人能幫忙。 Tx 為您的時間。

按照 TinyT 的建議,我將commandResult* result聲明為commandResult result = new commandResult; 並更改了結果的其余代碼,並在最后將其刪除以防止內存泄漏。 那是造成內存問題的變量。

Tx TinyT

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM