You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

25节点NS2无线仿真代码运行报错求助:请求修复实现无报错运行

Fixing NS2 Wireless Simulation Errors (25 Nodes, 802.15.4)

Let's break down and fix the two issues you're facing with your NS2 simulation script:

1. Critical Node Creation Errors (Fatal)

The num_nodes is set 0 and _o14 cmd addr errors are caused by three key mistakes in your script:

a. Invalid Routing Protocol Configuration

You can't list multiple routing protocols in val(rp) — NS2 only supports one ad-hoc routing protocol per simulation. Replace the multi-protocol line with a single valid option:

# Before (wrong)
set val(rp) AODV/AOMDV/DSR/DumbAgent

# After (choose one, e.g., AODV)
set val(rp) AODV

You can swap AODV with AOMDV, DSR, or DumbAgent based on your needs.

b. Incorrect GOD Node Creation

The create-god (1) line is hardcoding the node count to 1, which conflicts with your 25-node setup. Update it to use your node count variable:

# Before (wrong)
create-god (1)

# After (correct)
create-god $val(nn)

This tells NS2's GOD (Global Operations Director) that there are 25 nodes to manage, eliminating the num_nodes is set 0 warning.

c. Trailing Backslash in Node Config

The final line of your $ns node-config block has an unnecessary trailing backslash, which causes NS2 to interpret the following for loop as part of the node configuration (invalid syntax):

# Before (wrong)
-movementTrace OFF \
for {set i 0} {$i < $val(nn) } {incr i} {

# After (correct)
-movementTrace OFF
for {set i 0} {$i < $val(nn) } {incr i} {

Remove the backslash to properly terminate the node-config command.

2. Tclsh Version Warning (Non-Fatal)

The warning about tclsh version mismatch is just a heads-up — it won't break your simulation if the version difference is minor. To resolve it:

  • Run the script using NS2's built-in interpreter: ns your_script_name.tcl (this ensures you use the same tclsh version NS was compiled with)
  • Alternatively, explicitly call the correct tclsh version: tclsh8.6 your_script_name.tcl (if 8.6 is the version NS was configured with)

Fixed Full Script

#channel type
set val(chan) Channel/WirelessChannel;
set val(prop) Propagation/TwoRayGround
set val(netif) Phy/WirelessPhy/802_15_4
set val(mac) Mac/802_15_4
set val(ifq) Queue/DropTail/PriQueue
set val(ll) LL
set val(ant) Antenna/OmniAntenna
set val(ifqlen) 50
set val(nn) 25
set val(rp) AODV ;# Changed to single valid routing protocol
set val(x) 50
set val(y) 50
set ns [new Simulator]
set dist(5m) 7.69113e-06
set dist(9m) 2.37381e-06
set dist(10m) 1.92278e-06
set dist(11m) 1.58908e-06
set dist(12m) 1.33527e-06
set dist(13m) 1.13774e-06
set dist(14m) 9.81011e-07
set dist(15m) 8.54570e-07
set dist(16m) 7.51087e-07
set dist(20m) 4.80696e-07
set dist(25m) 3.07645e-07
set dist(30m) 2.13643e-07
set dist(35m) 1.56962e-07
set dist(40m) 1.20174e-07
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
set f [open out.tr w]
$ns trace-all $f
set f1 [open out0.tr w]
set f2 [open out1.tr w]
set f3 [open out2.tr w]
create-god $val(nn) ;# Fixed to use node count variable
$ns node-config -adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-topoInstance $topo \
-channelType $val(chan) \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace OFF ;# Removed trailing backslash
for {set i 0} {$i < $val(nn) } {incr i} {
 set node_($i) [$ns node]
 $node_($i) random-motion 0 ;
}
$node_(0) set X_ 26.608377307314
$node_(0) set Y_ 49.446991827566
$node_(0) set Z_ 0.000000000000
$node_(1) set X_ 49.337311778721
$node_(1) set Y_ 48.582820874924
$node_(1) set Z_ 0.000000000000
$node_(2) set X_ 27.437938215931
$node_(2) set Y_ 30.091658830425
$node_(2) set Z_ 0.000000000000
$node_(3) set X_ 10.568766842667
$node_(3) set Y_ 47.453389414657
$node_(3) set Z_ 0.000000000000
$node_(4) set X_ 32.496269526711
$node_(4) set Y_ 2.257450139045
$node_(4) set Z_ 0.000000000000
$node_(5) set X_ 26.476535726599
$node_(5) set Y_ 38.646396828920
$node_(5) set Z_ 0.000000000000
$node_(6) set X_ 49.430596917301
$node_(6) set Y_ 31.320042376073
$node_(6) set Z_ 0.000000000000
$node_(7) set X_ 20.045665842364
$node_(7) set Y_ 32.537008965783
$node_(7) set Z_ 0.000000000000
$node_(8) set X_ 13.641089465783
$node_(8) set Y_ 28.871428688349
$node_(8) set Z_ 0.000000000000
$node_(9) set X_ 2.125856848941
$node_(9) set Y_ 9.049644538277
$node_(9) set Z_ 0.000000000000
$node_(10) set X_ 0.596871605178
$node_(10) set Y_ 0.816174228621
$node_(10) set Z_ 0.000000000000
$node_(11) set X_ 8.828197591068
$node_(11) set Y_ 34.402175912553
$node_(11) set Z_ 0.000000000000
$node_(12) set X_ 18.759762507870
$node_(12) set Y_ 25.069997591097
$node_(12) set Z_ 0.000000000000
$node_(13) set X_ 13.277017351151
$node_(13) set Y_ 5.522149009306
$node_(13) set Z_ 0.000000000000
$node_(14) set X_ 28.935083056450
$node_(14) set Y_ 37.156742480258
$node_(14) set Z_ 0.000000000000
$node_(15) set X_ 23.507724673396
$node_(15) set Y_ 3.566772846479
$node_(15) set Z_ 0.000000000000
$node_(16) set X_ 20.236128396148
$node_(16) set Y_ 45.484419961171
$node_(16) set Z_ 0.000000000000
$node_(17) set X_ 45.709636471608
$node_(17) set Y_ 48.353113573382
$node_(17) set Z_ 0.000000000000
$node_(18) set X_ 45.610349971557
$node_(18) set Y_ 49.787732692400
$node_(18) set Z_ 0.000000000000
$node_(19) set X_ 24.345058881159
$node_(19) set Y_ 19.614188426582
$node_(19) set Z_ 0.000000000000
$node_(20) set X_ 35.972132273531
$node_(20) set Y_ 10.620805728046
$node_(20) set Z_ 0.000000000000
$node_(21) set X_ 49.626765649851
$node_(21) set Y_ 14.028905860151
$node_(21) set Z_ 0.000000000000
$node_(22) set X_ 14.914806804685
$node_(22) set Y_ 24.094071765795
$node_(22) set Z_ 0.000000000000
$node_(23) set X_ 28.443020411336
$node_(23) set Y_ 24.367299319338
$node_(23) set Z_ 0.000000000000
$node_(24) set X_ 48.102836431793
$node_(24) set Y_ 22.394995544608
$node_(24) set Z_ 0.000000000000
set namtrace [open wireless.nam w]
$ns namtrace-all-wireless $namtrace $val(x) $val(y)
proc finish {} {
 global ns f namtrace f1 f2 f3
 $ns flush-trace
 close $f
 close $namtrace
 close $f1
 close $f2
 close $f3
 exec nam wireless.nam & 
 exit 0
}
proc record { } {
 global sink1 f1 f2 f3
 set ns [Simulator instance]
 set time 1.0
 set now [$ns now]
 set b0 [$sink1 set bytes_]
 set b1 [$sink1 set nlost_ ]
 set b2 [$sink1 set npkts_ ]
 set bw0 [expr $b0/$time*8/1000]
 puts $f2 "$now $bw0"
 puts $f1 "$now $b1"
 puts $f3 "$now $b2"
 $sink1 set bytes_ 0
 $ns at [expr $now+$time] "record"
}
set udp1 [new Agent/UDP]
$ns attach-agent $node_(10) $udp1
set sink1 [new Agent/LossMonitor]
$ns attach-agent $node_(0) $sink1
$ns connect $udp1 $sink1
$udp1 set fid_ 2
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1
$cbr1 set type_ CBR
$cbr1 set packet_size_ 70
$cbr1 set rate_ 120k
$cbr1 set random_ false
#set cbr2 [new Application/Traffic/Pareto]
#$cbr2 attach-agent $udp1
#$cbr2 set type_ Pareto
#$cbr2 set packet_size_ 70
#$cbr2 set rate_ 10k
#$cbr2 set random_ false
#set cbr3 [new Application/Traffic/Exponential]
#$cbr3 attach-agent $udp1
#$cbr3 set type_ Exponential
#$cbr3 set packet_size_ 70
#$cbr3 set rate_ 10k
#$cbr3 set random_ false
for {set i 0} {$i < $val(nn)} {incr i} {
 $ns initial_node_pos $node_($i) 2
}
$ns at 0.0 "record"
$ns at 0.1 "$cbr1 start"
$ns at 20.1 "$cbr1 stop"
#$ns at 21.1 "$cbr2 start"
#$ns at 41.1 "$cbr2 stop"
#$ns at 42.1 "$cbr3 start"
#$ns at 62.1 "$cbr3 stop"
$ns at 63.0 "finish"
$ns run

How to Run

Save the fixed script (e.g., fixed_wireless.tcl) and execute it with:

ns fixed_wireless.tcl

This will launch NAM after the simulation completes, and generate all trace files as expected.

内容的提问来源于stack exchange,提问作者Kent Vapen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:14:32