当前位置:网站首页>OMNeT学习之新建工程
OMNeT学习之新建工程
2022-04-23 16:27:00 【RongLin02】
OMNeT学习之新建工程
前言
之前学习了OMNeT的安装与运行官方的实例代码,这篇文章记录一下,OMNeT如何创建一个新的项目。
本人为初学者,如有错误望批评指正!
本文原创,创作不易,转载请注明!
新建工程
打开OMNeT的安装根路径,打开mingwenv.cmd,输入omnetpp,打开omnet ide。
左上角 File – New – OMNeT++ Project...,然后输入一个项目名称,然后Next,然后选择 – Empty project with 'src' and 'simulations' folder – Finish.

文件目录如下,

新建cc文件
开始我们新建一个cc文件,src – New – Source File

然后在弹出来的界面输入 test.cc 新建一个cc文件来实现简单模块的功能
然后我们输入以下代码:
/*
* test.cc
*
* Created on: 2022年4月21日
* Author: Ronglin
*/
#include <string.h> //字符串功能函数
//这两行代码固定
#include <omnetpp.h> //必要的头文件导入
using namespace omnetpp; //使用命令空间omnetpp
/**
*创建一个test类,此类继承cSimpleModule
*在网络中,我们新建的tic和toc模块都是Test1对象 由omnet++在模拟开始时创建
*同时我们要重写虚函数initialize()和handleMessage(cMessage *msg)方法
*来实现我们的自定义功能
*/
class Test1 : public cSimpleModule
{
protected:
virtual void initialize() override;
virtual void handleMessage(cMessage *msg) override;
};
//要将Test1类注册到工程中
Define_Module(Test1);
/**
*重写初始化函数
*初始化函数主要是实现在一个模块刚创建时要完成的逻辑功能
*在本函数中,实现的逻辑功能为
*如果本模块的名称为"tic"则创建一条Message并通过out口发送
*/
void Test1::initialize()
{
if (strcmp("tic", getName()) == 0) {
cMessage *msg = new cMessage("tictocMsg");
send(msg, "out");
}
}
/**
*此方法主要是当收到消息的时候
*本模块应执行的逻辑功能
*在本函数中,实现的逻辑功能为
*收到消息之后,将消息通过out口发送出去
*/
void Test1::handleMessage(cMessage *msg)
{
send(msg, "out");
}
简单讲解一下,按照也可以对照注释查看,首先要定义一个类,然后此类要继承自 cSimpleModule类然后重写函数 initialize()和 handleMessage(cMessage *msg),同时要注意将类注册到omnet中。
写完之后build一遍,菜单栏 – Projec – Build Project没有报错即可完成。

为了方便复制这里先搞一份没有注释的
#include <omnetpp.h>
using namespace omnetpp;
class Test1 : public cSimpleModule
{
protected:
virtual void initialize() override;
virtual void handleMessage(cMessage *msg) override;
};
Define_Module(Test1);
void Test1::initialize()
{
}
void Test1::handleMessage(cMessage *msg)
{
}
配置文件
然后就是配置文件的修改,主要是两个文件,一个是 omnetpp.ini,还有一个是ned文件,都在 simulations文件夹下。
omnetpp.ini
这个文件需要改的地方比较少,只需要输入网络(Network)名称即可,我这里叫 test_network

package.ned
点开 simulations文件夹下的 package.ned文件,接下来,我们来创建一个网络。
GUI
首先提供GUI模式下的创建网络
点开package.ned,在右侧 Type选择第三个 Network,点击一下,然后鼠标移动到左侧,再点击一下,即可创建出来一个网络。

对着新创建的网络右键,第一个 Properties...,这里可以设置这个网络的名称,图标,位置等等,我们只需要修改一下名称即可,效果如图,此名称要对应omnetpp.ini中的网络名称。

然后就要创建模型,点开下面的 Source,然后输入代码,用来定义模型的门结构
simple Test1
{
gates:
input in;
output out;
}
然后点回Design ,鼠标点住 Test1 ,往网络中的灰框框拖动,因为需要两个,我们拖动两次。

然后,选中灰框框里边的 Test1,右键 , 第一个 Properties...,只需要更改Name,我这里一个叫tic,一个叫toc。
然后建立连接,右侧 palette – Connection,然后点击tic再点击toc,然后选择tic.out-->toc.in,建立了一条从tic到toc的连接路线,连接建立后,因为还需要设置延时,选中线,右键,Properties...– Type 选择 DelayChannel – OK 如图。

然后设置延时时间,选中线,右键 – Parameters... ,然后点击 Value,输入 100ms然后 ok。

因为是tic和toc的双向通信,我们需要连接两次,同理,从toc再点击tic建立双向连接,再设置延迟,完成后如下图:

至此,一个简单的工程创建完毕。
然后点击工具栏的 Run然后弹出来一个新的界面,再点击一次 Run可以看到消息的传递了
Source
有的时候GUI虽然直观但是修改属性找起来很麻烦,直接用代码简单,这里直接附上tictoc1 的 ned代码
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
//
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
{
@display("bgb=641,262");
submodules:
tic: Txc1 {
@display("p=98,146");
}
toc: Txc1 {
@display("p=381,61");
}
connections:
tic.out --> { delay = 100ms; } --> toc.in;
tic.in <-- { delay = 100ms; } <-- toc.out;
}
simple Test1
{
parameters: //定义该模块的参数
gates: // 定义该模块的输入和输出口
}
总结
OMNeT很强大,但是缺点是网络上能查到的资料太少了,只能一步一步的去自己摸索了。
版权声明
本文为[RongLin02]所创,转载请带上原文链接,感谢
https://blog.csdn.net/RongLin02/article/details/124316760
边栏推荐
- On the value, breaking and harvest of NFT project
- Loading order of logback configuration file
- Ice -- source code analysis
- GRBL学习(二)
- Algorithem_ ReverseLinkedList
- 捡起MATLAB的第(8)天
- G008-hwy-cc-estor-04 Huawei Dorado V6 storage simulator configuration
- Nanny Anaconda installation tutorial
- Set the color change of interlaced lines in cells in the sail software and the font becomes larger and red when the number is greater than 100
- File system read and write performance test practice
猜你喜欢

Day (9) of picking up matlab

Day 10 abnormal mechanism

Ali developed three sides, and the interviewer's set of combined punches made me confused on the spot

第九天 static 抽象类 接口

JMeter setting environment variable supports direct startup by entering JMeter in any terminal directory

G008-HWY-CC-ESTOR-04 华为 Dorado V6 存储仿真器配置

Government cloud migration practice: Beiming digital division used hypermotion cloud migration products to implement the cloud migration project for a government unit, and completed the migration of n

Sail soft implements a radio button, which can uniformly set the selection status of other radio buttons

You need to know about cloud disaster recovery

Meaning and usage of volatile
随机推荐
G008-HWY-CC-ESTOR-04 华为 Dorado V6 存储仿真器配置
Method 2 of drawing ROC curve in R language: proc package
Qipengyuan horizon credible meta universe social system meets diversified consumption and social needs
最詳細的背包問題!!!
Day (3) of picking up matlab
About JMeter startup flash back
Nacos detailed explanation, something
Ali developed three sides, and the interviewer's set of combined punches made me confused on the spot
299. 猜数字游戏
Cloudy data flow? Disaster recovery on cloud? Last value content sharing years ago
G008-hwy-cc-estor-04 Huawei Dorado V6 storage simulator configuration
100 deep learning cases | day 41 - convolutional neural network (CNN): urbansound 8K audio classification (speech recognition)
Win11/10家庭版禁用Edge的inprivate浏览功能
Download and install mongodb
Hypermotion cloud migration completes Alibaba cloud proprietary cloud product ecological integration certification
Summary according to classification in sail software
Sail soft segmentation solution: take only one character (required field) of a string
Set the color change of interlaced lines in cells in the sail software and the font becomes larger and red when the number is greater than 100
Cloud migration practice in the financial industry Ping An financial cloud integrates hypermotion cloud migration solution to provide migration services for customers in the financial industry
Day 10 abnormal mechanism