没有调用回调函数吗?

时间:2019-07-24 16:53:56

标签: c++ arduino

我在Arduino文件(.ino,c ++)中有2个回调函数:

void incomingPacket(const char* subject, const char* message){
  Serial.print("subject: ");
  Serial.println(subject);
  Serial.print("message: ");
  Serial.println(message);
}

void connectedProtocol(bool connected){
  if(connected){
    Serial.println("protocol server connected");  
  }else{
    Serial.println("protocol server disconnected");  
  }
}

在我的Arduino文件中,我有一个自定义类UDPProtocol protocol(Udp),该类在某些事件发生后调用回调函数,例如:

 _connectedcb(setTo);    //this calls connectedProtocol callback in Arduino from UDPProtocol class

以上方法工作正常,它使用正确的布尔值调用回调函数connectedProtocol()。但是,当尝试调用incomingPacket()回调函数时,我的Arduino文件中没有任何反应。调用回调的方法:

//this is from UDPProtocol class
void UDPProtocol::readPacketContents(char* content){
        if(strlen(content)){                                //if content size is greater than 0
        const char* somemessage = "Hellomessage";
        const char* subject;
        const char* message;
        subject = &somemessage[0];                          //this depends on the actual format of udp packet
        message = &somemessage[1];
        _packetcb(subject,message);
        }
        _buffer[0] = 0;
    }

无论何时运行此方法,都不会在Arduino中调用回调。即使我尝试伪造该消息,例如:

void UDPProtocol::readPacketContents(char* content){
        if(strlen(content)){                                //if content size is greater than 0
            const char* somemessage = "Hellomessage";
            const char* subject;
            const char* message;
            subject = &somemessage[0];                          //this depends on the actual format of udp packet
            message = &somemessage[1];
            _packetcb(subject,message);
        }
        _buffer[0] = 0;
    }

仍然没有任何反应(如果(strlen(content))等于true)。有什么建议吗?

编辑:

UDPProtocol.h文件:

typedef void (*PackedCallback)(const char* subject, const char* message);
PackedCallback _packetcb;

UDPProtocol.cpp文件:

void UDPProtocol::setPacketCallback(PackedCallback func){
    this->_packetcb = func;
}

在Arduino.ino中:

protocol.setPacketCallback(incomingPacket);    //defined in this question above

编辑2:

我用完整的代码创建了要点:

https://gist.github.com/aliamid93/64ba2ed0e06b1f9b4401474646f8083e

0 个答案:

没有答案