OMNet++ tictoc1

官网教程:OMNet++基础案例
打开软件找到tictoc的文件夹打开
打开软件找到tictoc的文件夹打开可以看到里面的所有代码

tictoc1

节点:tic toc

1.创建项目

创建项目
新建项目

2.添加NED文件(网络说明文件)

添加NED文件
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在源文件中输入代码
在这里插入图片描述

simple Txc1//设置节点
	{
	    gates:
	        input in;//节点输入门
	        output out;//节点输出门
	}
	
	//
	// Two instances (tic and toc) of Txc1 connected both ways.
	// Tic and toc will pass messages to one another.
	//
	network Tictoc1//网络名称
	{
	    submodules://设置两个子模块
	        tic: Txc1;
	        toc: Txc1;
	    connections:
	        tic.out --> {  delay = 100ms; } --> toc.in;//tic输出门延时100ms之后到达toc的输入门
	        tic.in <-- {  delay = 100ms; } <-- toc.out;//toc输出门延时100ms之后到达tic的输入门
	}

写完源文件之后就可以查看我们的网络图
在这里插入图片描述

3.加入C++文件

写TXC1节点的功能
在这里插入图片描述
新建文件
在这里插入图片描述
编写代码

#include <string.h>
#include <omnetpp.h>

using namespace omnetpp;

/**
 * Derive the Txc1 class from cSimpleModule. In the Tictoc1 network,
 * both the `tic' and `toc' modules are Txc1 objects, created by OMNeT++
 * at the beginning of the simulation.
 */
class Txc1 : public cSimpleModule//Txc1是cSimpleModule的子类
{
  protected://重新定义两个算法
    // The following redefined virtual function holds the algorithm.
    virtual void initialize() override;
    virtual void handleMessage(cMessage *msg) override;
};

// The module class needs to be registered with OMNeT++
Define_Module(Txc1);

void Txc1::initialize()//Txc1的初始化执行
{
    // Initialize is called at the beginning of the simulation.
    // To bootstrap the tic-toc-tic-toc process, one of the modules needs
    // to send the first message. Let this be `tic'.

    // Am I Tic or Toc?
    if (strcmp("tic", getName()) == 0) {//如果输入的信息是tic就执行以下代码
        // create and send first message on gate "out". "tictocMsg" is an
        // arbitrary string which will be the name of the message object.
        cMessage *msg = new cMessage("tictocMsg");//创造一个消息对象
        send(msg, "out");//从输出门发送消息对象
    }
}

void Txc1::handleMessage(cMessage *msg)//每次消息到达时都会执行handleMessage函数
{
    // The handleMessage() method is called whenever a message arrives
    // at the module. Here, we just send it to the other module, through
    // gate `out'. Because both `tic' and `toc' does the same, the message
    // will bounce between the two.
    send(msg, "out"); // send out the message当接收到消息时就将msg再次发送出去
}

所以上述代码就是初始化时,让tic首先发送一条消息tictocMsg,toc接受到消息之后会返回一条消息tictocMsg

4 添加omnetpp.ini

为了能够运行模拟器,我们需要创建omnetpp.ini文件,omnetpp.ini 告诉仿真程序的网络,你要模拟 (作为NED文件可能包含的几个网络),可以通过参数 该模型,明确指定的种子,随机数发生器,等等。
创建omnetpp.ini文件
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在源文件中输入我们需要仿真的网络名称
在这里插入图片描述

5 运行

运行omnetpp.ini文件,点击绿色的运行按钮
在这里插入图片描述
会生成这样一个框
在这里插入图片描述
应为我们在c++文件中没有写停止,所以信息会已知在tic和toc中传递,不会停止

6 调试

在这里插入图片描述
在这里插入图片描述
调试按钮
调试按钮主要是为了调试错误,所以我们先刻意引入一个错误
在这里插入图片描述
在txc1中多加一行这一行将会引起错误
在这里插入图片描述
错误信息如上图所示

调试窗口如上图所示
在这里插入图片描述
调试时在这个地方可以启动日志

运行完可以在results中看到日志
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值