A plot of transmitted or received packets with respect to time for any network device can be obtained from the packet trace log file. Explained below are the steps to plot the received packets with respect to time:

  • Simulate with the Packet Trace log enabled. Open the log post-simulation from the results window, as shown below:
 A screenshot of a computer Description automatically generated

A screenshot of a computer 
Description automatically generated
  • Open the Packet Trace. Go to the Developer menu. If you do not find this menu, follow the steps mentioned in the article How to enable Developer tab in MS-Excel? to enable it.
  • Under the Developer tab go to Visual Basic or press Alt + F11.
A screenshot of a computer 
Description automatically generated
  • Go to the Insert Menu and select Module

  • Copy and paste the following VBA code in the code editor
Sub GeneratePacketChart()
    ' Declare variables for clarity and to avoid repetition
    Dim wsPacketTrace As Worksheet
    Dim wsSummary As Worksheet
    Dim pt As PivotTable
    Dim pfEndTime As PivotField
    Dim pfReceiverID As PivotField
    Dim pfPacketStatus As PivotField
    Dim chartRange As Range
    Dim packetChart As ChartObject
    
    ' User-defined variables for simulation time and window size
    Dim simulationTimeEnd As Double
    Dim windowSize As Double
    
    ' Set user-defined values for simulation time and window size
    windowSize = 500000 ' Set your desired value

    ' Set references to worksheets
    Set wsPacketTrace = ThisWorkbook.Sheets("Packet Trace")
    Set wsSummary = ThisWorkbook.Sheets.Add

    ' Check if there is an existing table in the specified location and delete it
    On Error Resume Next
    wsSummary.PivotTables("PivotTable2").TableRange2.Clear
    On Error GoTo 0

    ' Create PivotTable
    Set pt = wsSummary.PivotTableWizard(SourceType:=xlDatabase, SourceData:=wsPacketTrace.UsedRange, TableDestination:=wsSummary.Range("A3"), TableName:="PivotTable2")

    ' Set PivotTable source data
'    pt.SetSourceData wsPacketTrace.UsedRange

    ' Set PivotTable layout
    Set pfEndTime = pt.PivotFields("PHY_LAYER_END_TIME(µS)")
    pfEndTime.Orientation = xlRowField
    pfEndTime.Position = 1

    Set pfReceiverID = pt.PivotFields("RECEIVER_ID")
    pfReceiverID.Orientation = xlPageField
    pfReceiverID.Position = 1

    Set pfPacketStatus = pt.PivotFields("PACKET_STATUS")
    pfPacketStatus.Orientation = xlDataField
    pfPacketStatus.Function = xlCount
    pfPacketStatus.Name = "Count of PACKET_STATUS"

    ' Group rows in the PivotTable
    With pt.PivotFields("PHY_LAYER_END_TIME(µS)")
    ' Ensure direct access to the PivotField object
        Set field = .PivotItems(1) ' Or use a more specific item retrieval method if needed
    
        ' Apply grouping directly to the PivotField object
        pt.PivotFields("PHY_LAYER_END_TIME(µS)").DataRange.Cells(1).Select
        Selection.Group Start:=0, End:=True, By:=windowSize
1   End With
    ' Exclude grand total from the PivotTable
    pt.ColumnGrand = False
    pt.RowGrand = False

    ' Copy data for chart
    Set chartRange = wsSummary.Range("A4").CurrentRegion
    chartRange.Copy

    Set packetChart = wsSummary.ChartObjects.Add(Left:=100, Width:=375, Top:=75, Height:=225)
    packetChart.Chart.SetSourceData Source:=chartRange
    packetChart.Chart.ChartType = xlLine

    ' Set x-axis and y-axis titles
    packetChart.Chart.Axes(xlCategory, xlPrimary).HasTitle = True
    packetChart.Chart.Axes(xlCategory, xlPrimary).AxisTitle.Text = "Simulation Time(µS)"

    packetChart.Chart.Axes(xlValue, xlPrimary).HasTitle = True
    packetChart.Chart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Packets Received"
    
    ' Set chart title
    packetChart.Chart.HasTitle = True
    packetChart.Chart.ChartTitle.Text = "Packets Received vs Simulation Time"

    ' Adjust chart position
    packetChart.Left = packetChart.Left + 65.25
    packetChart.Top = packetChart.Top - 15.75
End Sub
  • The attached VBA Script file packet_received_vs_simulation_time.bas can also be downloaded and imported alternatively.
  • In the code, the window size parameter can be modified if required. Click on the Run button to generate the plot
A screenshot of a computer 
Description automatically generated
  • A new sheet with the data and plot gets created as shown below:

A screenshot of a computer 
Description automatically generated
 
Receiver ID can be filtered to the desired device for which the plot is to be obtained.