1. 发送 WSM 消息
新建一条WSM消息并广播,代码如下:
TraCIDemo11pMessage* newWSM = new TraCIDemo11pMessage();
populateWSM(newWSM);
std::string newWSM_str = "A new WSM type message.";
newWSM->setSenderAddress(myId);
newWSM->setDemoData(newWSM_str.data());
sendDown(newWSM->dup());
std::cout << this->myId << ": " << newWSM->getDemoData() << std::endl;
效果如下:
2. 接收 WSM 消息
接收WSM消息,代码如下:
void TraCIDemo11p::onWSM(BaseFrame1609_4* frame)
{
TraCIDemo11pMessage* wsm = check_and_cast<TraCIDemo11pMessage*>(frame);
std::string revWSM_str = wsm->getDemoData();
std::cout << "Car[" << this->myId << "] recieve the car["
<< wsm->getSenderAddress() << "] message:" << revWSM_str << std::endl;
}
效果如下: