2016-10-06 13 views
-1

我在arduino草图上遇到了一个奇怪的问题。 我的素描相当复杂,所以我决定在课堂上写作。 基本上它通过JSON格式的TCP连接接收一些数据,并根据解码数据做一些操作。Arduino,代码执行期间失去变量值

我使用库“Arduino Json”解码CLASS“PacketManager”中接收到的JSON。然后,我将JSON对象传递给类“Bollard”以执行命令(代码将随后显示)。奇怪的是,在“CommandExecutor”方法的“Bollard”类中,我访问了我通过的数据,并且可以使用它们,但是在数据变为0的相同方法的代码期间的某个点。 Ofcourse i'已经检查过数据没有被覆盖在方法中。草图没有任何中断或多线程。看起来像变量得到解除分配的形式内存或内存块得到覆盖。 CLASS PACKETMANAGER接收了JSON

/// FRAGMENT

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) 
CLASS护柱

///////////////片段使用JSON OBJECT

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为您的时间。

+0

你写在你的代码'* EVENTTYPE = *命令类型的一行;'。做你希望它是价值的'commandType'或'eventType'事后指​​出?另外,为什么你通过未初始化的指针将'eventType'中存储的值赋给'commandResult'成员? 'commandResult * result;'和'result-> ackPriority = false;'之间没有对象的初始化。如果你在你的实际代码中检查'result-> type'而不是'* eventType',那么应该会导致这个问题。 – starturtle

+0

Tx为您的答复,我inizializing commandResult *结果;上delcaration: [代码](结构commandResult { 字符串消息= “DEFAULT ”; 字符串卡=“”; 布尔sendResponse = FALSE; 整型= 0; INT ackEventType; INT ackStatus; INT ackPacketNumber; bool ackPriority = false; };) 您认为这仍然存在问题吗? – groot

+0

是的,我想要将commandType的值赋给eventType中的eventType = * commandType; – groot

回答

0

继TinyT的建议之后,我宣布commandResult* resultcommandResult result = new commandResult;,并更改了结果的其余代码,并在最后删除它以防止内存泄漏。 这是造成内存问题的变量。

的Tx TinyT