我在arduino草图上遇到了一个奇怪的问题。 我的草图非常复杂,所以我决定写一些类。 基本上,它通过JSON格式的TCP连接接收一些数据,并根据解码数据进行一些操作。
我使用库“Arduino Json”解码CLASS“PacketManager”中收到的JSON。然后我将JSON对象传递给类“Bollard”以执行命令(代码将跟随)。奇怪的是,在“CommandExecutor”方法中的“Bollard”类中,我访问了我传递的数据,我可以使用它们,但在同一方法的代码中,数据转到0的某个时刻。 当然,我已经检查过该方法中的数据没有被覆盖。草图没有任何中断或多线程。好像变量得到了解除分配形式的内存或者内存块被覆盖了。
///收到JSON的CLASS PACKETMANAGER的片段
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对象的类别BOLLARD的碎片
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 :(得分:0)
根据TinyT的建议,我已将commandResult* result
声明为commandResult result = new commandResult;
,并更改了结果的其余代码,并在最后删除它以防止内存泄漏。
这是造成内存问题的变量。
Tx TinyT