我开发了一个场景,首先车辆发送一个自我信息,并在接收到自我信息的车辆发送一条消息给RSU。
自消息代码是在initialize()方法中编写的。但在仿真过程中,车辆每秒钟向RSU发送消息。
我只想把消息发一次。我该怎么办?我已经附上了我的handleSelfmessage类的TraCIDemo11p.cc方法。
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());
}发布于 2017-04-03 13:40:40
您的方法似乎是正确的,但显然您在实现过程中遇到了一些问题。
或者,您可以创建一条新消息并将其发送给自己。
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...发布于 2017-04-03 18:34:39
修改@ answer 4786271的答复:
显然,该模块接收到的每个自消息--可能也不是WSMs --都要执行TraCIDemo11p.cc的TraCIDemo11p.cc方法。因此,如果您只是在那里添加了给定的代码,它将为所有这些自我消息发送一个WSM。因此,仅检查自消息类型是不够的.您需要创建一个新的消息类型并检查该类型,如@ need 4786271所示。
https://stackoverflow.com/questions/43182940
复制相似问题