2017-04-03 267 views
0

我已经开发了一个场景,首先车辆发送自己的消息,并在接收到自己的消息时向车辆局发送消息。只发送一次消息,而不是定期发送消息

自我讯息码写在initialize()方法中。但在模拟过程中,车辆每秒都会将消息发送给RSU。

我希望邮件只发送一次。我该怎么办? 我附上了我的TraciDemo11p.cc类的handleSelfmessage方法。

if(msg->isSelfMessage()==true) 
    { 
     cModule *tmpMobility = getParentModule()->getSubmodule("veinsmobility"); 
     mobility = dynamic_cast<Veins::TraCIMobility*>(tmpMobility); 
     ASSERT(mobility); 
     t_channel channel = dataOnSch ? type_SCH : type_CCH; 
     WaveShortMessage* wsm = prepareWSM("data", dataLengthBits, channel, dataPriority, -1,2); 

     wsm->setSenderAddress(myAddress); 
     wsm->setRecipientAddress(1001); 
     sendMessage(wsm->getWsmData()); 
    } 
+1

也包含'initialize()'函数的代码 – user4786271

+0

你究竟把这个代码放在哪里?它是否在[TraCIDemo11p.cc]的'handleSelfMsg'中(https://github.com/sommer/veins/blob/veins-4.5/src/veins/modules/application/traci/TraCIDemo11p.cc)? –

回答

2

你的方法似乎是正确的,但显然你在实现中有一些问题。

或者,你可以创建一个新的消息,并将其发送给自己

myOneTimeMsg = new cMessage("OneTimeMsg"); 
scheduleAt(simTime()+1.0, myOneTimeMsg); // this will send the message at t=currentTime+1.0 seconds 

然后你可以如下处理该消息:

if(msg->isSelfMessage()==true){ 
    if (msg == myOneTimeMsg) { 
     // do what you need next... 
1

修订@ user4786271的答案:

的​​3210方法显然是针对这个模块接收到的每个自身消息执行的 - 可能还有非WSM。因此,如果您只是在那里添加了给定的代码,它将为每个这些自我消息发送一个WSM。因此,只检查自我消息类型是不够的。您需要创建一个新的消息类型并检查该类型,如@ user4786271所示。