Inet 4.4:如何在.cc文件设置通信范围及ini命令调用逻辑
在INET 4.4中通过代码设置通信范围(米为单位)
要在.cc文件中直接以米为单位设置通信范围,需通过获取Radio模块的Transmitter实例并调用其setter函数,步骤如下:
- 获取Radio模块引用:在宿主模块(如Host)的初始化函数中,通过子模块路径定位到WLAN对应的Radio模块。
- 转换为具体Transmitter实现:INET默认使用
SimpleTransmitter,需将通用的ITransmitter指针转为该具体类。 - 调用setter设置范围:直接传入米为单位的数值(double类型)。
示例代码(在Host的initialize函数中):
#include "inet/physicallayer/wireless/common/radio/Radio.h" #include "inet/physicallayer/wireless/common/radio/transmitter/SimpleTransmitter.h" void YourHostModule::initialize(int stage) { if (stage == INITSTAGE_PHYSICAL_LAYER) { // 获取wlan[0]下的Radio模块 auto radio = getSubmodule<inet::physicallayer::Radio>("wlan[0]"); // 转为SimpleTransmitter实例 auto transmitter = dynamic_cast<inet::physicallayer::SimpleTransmitter*>(radio->getTransmitter()); if (transmitter) { // 设置通信范围为250米 transmitter->setCommunicationRange(250.0); } } }
注意:需确保初始化阶段为INITSTAGE_PHYSICAL_LAYER,避免模块未完成初始化;若使用其他Transmitter实现(如DimensionalTransmitter),需替换对应的类名和头文件。
OMNeT++配置命令对应的函数调用链
当执行*.host*.wlan[0].radio.transmitter.communicationRange = 250m时,具体调用逻辑如下:
- 参数解析与目标定位:OMNeT++配置系统会定位到每个Host的
wlan[0].radio.transmitter模块,默认该模块类型为SimpleTransmitter。 - 调用setter函数:OMNeT++会直接调用
SimpleTransmitter的setCommunicationRange(double range)函数,该函数负责更新内部的communicationRange成员变量,并同步更新相关传输特性。
相关文件与函数:
- 头文件:
inet/physicallayer/wireless/common/radio/transmitter/SimpleTransmitter.h,声明了setCommunicationRange()函数和communicationRange成员变量。 - 实现文件:
inet/physicallayer/wireless/common/radio/transmitter/SimpleTransmitter.cc,实现setCommunicationRange()函数,处理范围值的更新逻辑。
若使用其他Transmitter实现(如DimensionalTransmitter),则对应文件和函数会有所不同,但核心逻辑均为OMNeT++调用模块参数对应的setter函数。
内容的提问来源于stack exchange,提问作者Maryam
相关产品推荐
相关产品推荐

