【发布时间】:2021-11-26 08:14:34
【问题描述】:
我正在尝试实现一个小例子:RSU 广播自己的 ID 和其他信息,车辆接收 RSU 的 ID 并记录它。 我新建了一个名为 BeaconRSU.msg 的 msg 文件,RSU 和车辆的应用层 cc 文件如下所示:
//MyVeinsAppRSU.cc
#include "veins/modules/application/traci/MyVeinsAppRSU.h"
#include "veins/modules/application/traci/BeaconRSU_m.h"
using namespace veins;
Define_Module(veins::MyVeinsAppRSU);
void MyVeinsAppRSU::initialize(int stage)
{
DemoBaseApplLayer::initialize(stage);
if (stage == 0) {
sendBeacon= new cMessage("send Beacon");
EV << "Initializing " << par("appName").stringValue() << std::endl;
}
else if (stage == 1) {
// Initializing members that require initialized other modules goes here
if (sendBeacon->isScheduled())
{
cancelEvent(sendBeacon);
}
scheduleAt(simTime()+5,sendBeacon);
}
}
void MyVeinsAppRSU::handleSelfMsg(cMessage* msg)
{
if (msg == sendBeacon){
BeaconRSU* rsuBeacon = new BeaconRSU();
rsuBeacon->setRSUId(this->getParentModule()->getIndex());
rsuBeacon->setMyDemoData("RSU message!!");
BaseFrame1609_4* WSM = new BaseFrame1609_4();
WSM->encapsulate(rsuBeacon);
populateWSM(WSM);
send(WSM,lowerLayerOut);
EV << "rsu send success" <<endl;
if (simTime() < 2000) {
scheduleAt(simTime()+1,sendBeacon);
}
return;
}
}
//MyVeinsAppCar.cc
#include "veins/modules/application/traci/MyVeinsAppCar.h"
#include "veins/modules/application/traci/BeaconRSU_m.h"
#include "veins/modules/application/traci/MyVeinsAppRSU.h"
using namespace veins;
Define_Module(veins::MyVeinsAppCar);
void MyVeinsAppCar::initialize(int stage)
{
DemoBaseApplLayer::initialize(stage);
if (stage == 0) {
// Initializing members and pointers of your application goes here
EV << "Initializing " << par("appName").stringValue() << std::endl;
int a = INT_MIN;
}
else if (stage == 1) {
// Initializing members that require initialized other modules goes here
RSUIndex.setName("test");
int a= INT_MIN;
EV << "MyVeinsAppCar is initializing" << std::endl;
}
}
void MyVeinsAppCar::handleLowerMsg(cMessage* msg)
{
BaseFrame1609_4* WSM = check_and_cast<BaseFrame1609_4*>(msg);
cPacket* enc = WSM->getEncapsulatedPacket();
BeaconRSU* bc = dynamic_cast<BeaconRSU*>(enc);
EV << "receive message !!!" << endl;
if(a!=bc->getRSUId())
{
RSUIndex.record(bc->getRSUId());
a=bc->getRSUId();
}
EV << "my message = " <<bc->getMyDemoData()<<endl;
EV <<"send message RSU id:" <<bc->getRSUId() << " Receive successfully !!!!!!!!!!!" << endl; }
当我运行模拟时,我可以看到 RSU 发送的 msg 成功,但节点无法接收 msg(MyVeinsAppCar::handleLowerMsg(cMessage* msg) 中的内容没有打印出来)。 日志中有一些错误: enter image description here
为什么会这样?有人帮助我吗?提前谢谢!
//BeaconRSU.msg
cplusplus{{
#import "veins/base/utils/Coord.h"
#import "veins/modules/utility/Consts80211p.h"
#include "veins/modules/messages/BaseFrame1609_4_m.h"
#include "veins/base/utils/SimpleAddress.h"
}};
namespace veins;
// TODO generated message class
class noncobject Coord;
class BaseFrame1609_4;
packet BeaconRSU extends BaseFrame1609_4
{
//id of the originator
int RSUId = 0;
Coord position[100];
double beaconrate[100];
string myDemoData;
Coord slotpos;
simtime_t timestamp=0;
}
部分调试模式日志如下所示:
【问题讨论】: