我正在使用Arduino IDE和things库来创建一个LoRa mote。
我已经创建了一个类,它应该处理所有与LoRa相关的函数。在这个类中,如果收到下行链路消息,我需要处理回调。ttn库有一个onMessage函数,我想在init函数中设置它,并解析另一个函数,这个函数是一个类成员,名为message。我得到了“无效使用非静态成员函数”的错误。
// File: LoRa.cpp
#include "Arduino.h"
#include "LoRa.h"
#include <TheThingsNetwork.h>
TheThingsNetwork ttn(loraSerial,debugSerial,freqPlan);
LoRa::LoRa(){
}
void LoRa::init(){
// Set the callback
ttn.onMessage(this->message);
}
// Other functions
void LoRa::message(const uint8_t *payload, size_t size, port_t port)
{
// Stuff to do when reciving a downlink
}和头文件
// File: LoRa.h
#ifndef LoRa_h
#define LoRa_h
#include "Arduino.h"
#include <TheThingsNetwork.h>
// Define serial interface for communication with LoRa module
#define loraSerial Serial1
#define debugSerial Serial
// define the frequency plan - EU or US. (TTN_FP_EU868 or TTN_FP_US915)
#define freqPlan TTN_FP_EU868
class LoRa{
// const vars
public:
LoRa();
void init();
// other functions
void message(const uint8_t *payload, size_t size, port_t port);
private:
// Private functions
};
#endif我试过:
ttn.onMessage(this->message);
ttn.onMessage(LoRa::message);
ttn.onMessage(message);然而,他们中没有一个像我所期望的那样工作。
发布于 2017-06-06 09:04:39
我通过使消息函数成为类外的正常函数来解决这个问题。不确定这是否是一个好的实践,但它是有效的。
// File: LoRa.cpp
#include "Arduino.h"
#include "LoRa.h"
#include <TheThingsNetwork.h>
TheThingsNetwork ttn(loraSerial,debugSerial,freqPlan);
void message(const uint8_t *payload, size_t size, port_t port)
{
// Stuff to do when reciving a downlink
}
LoRa::LoRa(){
}
void LoRa::init(){
// Set the callback
ttn.onMessage(message);
}发布于 2017-06-04 14:42:27
在不使用类成员的情况下,您试图调用一个成员函数(即属于类类型成员的函数)。这意味着,通常要做的是先实例化类LoRa的一个成员,然后将其称为:
LoRa loraMember;
loraMember.message();由于您试图从类本身调用该函数,而没有类的一个成员调用init(),所以必须使函数静态如下:
static void message(const uint8_t *payload, size_t size, port_t port);然后您可以在任何地方使用LoRa:: message (),只要它是公共的,但是这样调用它会给您带来另一个编译器错误,因为消息接口要求"const uint8_t *有效负载、size_t大小、port_t端口“。所以你要做的就是给消息打电话,就像:
LoRa::message(payloadPointer, sizeVar, portVar);`当您调用ttn.onMessage(functionCall)时,所发生的情况是对函数调用进行计算,然后将该函数返回的内容放入括号中,并使用该括号调用ttn.onMessage。因为您的LoRa::message函数不返回任何(void),您将在这里得到另一个错误。
我推荐一本关于C++基础知识的好书,让你开始- book list。
祝好运!
发布于 2017-06-04 14:15:52
您应该向massage传递参数,如其原型所示:
void message(const uint8_t *payload, size_t size, port_t port);
由于massage返回void,因此不应将其用作其他函数的参数。
https://stackoverflow.com/questions/44354908
复制相似问题