Applicable VersionsNetSim StandardNetSim Pro


Applicable Releasesv9
v10   
v11v12v13


fprintf() statement can be used to print to NetSim console using the default file descriptor stderr

fprintf(stderr, "The value of the variable X is %d\n", X);

You may insert such a line at a suitable place. Note that NetSim is a discrete event simulator and hence you may see this line printed multiple times, each time that particular event (where the fprintf is written) is called.


Example 1: Printing remaining power of sensors in WSN


Note: The power source needs to be set to Battery to see the effect of this code change to see the impact.


For NetSim v11.1 v12, v13:


Inside fn_NetSim_Zigbee_ChangeRadioState() function present in ChangeRadioState.c file add following codes(highlighted in red):


bool fn_NetSim_Zigbee_ChangeRadioState(NETSIM_ID nDeviceId, PHY_TX_STATUS nOldState, PHY_TX_STATUS nNewState)
{
ptrIEEE802_15_4_PHY_VAR phy = WSN_PHY(nDeviceId);
ptrBATTERY battery = phy->battery;
bool isChange = true;
if (battery)
{
isChange = battery_set_mode(battery, nNewState, pstruEventDetails->dEventTime);

fprintf(stderr, "\nNode Name\tRemaining_Power \t Time\n");
fprintf(stderr, "\n%s\t%lf mW\t%lf ms\n\n",
NETWORK->ppstruDeviceList[nDeviceId - 1]->szDeviceName,
battery_get_remaining_energy(battery),
pstruEventDetails->dEventTime);
_getch();

}


  • Rebuild the Zigbee project, libZigbee.dll inside the binary folder of the current workspace will get modified.
  • Create a scenario in WSN and simulate.
  • During the simulation,user can see the remaining power of each node along printed to the simulation console along with the timestamp.


For NetSim v11:


Inside fn_NetSim_Zigbee_ChangeRadioState() function present in ChangeRadioState.c file add following codes(highlighted in red):


bool fn_NetSim_Zigbee_ChangeRadioState(NETSIM_ID nDeviceId, PHY_TX_STATUS nOldState, PHY_TX_STATUS nNewState)
{
ptrIEEE802_15_4_PHY_VAR phy = WSN_PHY(nDeviceId);
ptrBATTERY battery = phy->battery;
bool isChange = true;
if (battery)
{
isChange = battery_set_mode(battery, nNewState, pstruEventDetails->dEventTime);

fprintf(stderr, "\nNode Name\tRemaining_Power \t Time\n");
fprintf(stderr, "\n%s\t%lf mW\t%lf ms\n\n",
NETWORK->ppstruDeviceList[nDeviceId - 1]->szDeviceName,
battery_get_remaining_energy(battery),
pstruEventDetails->dEventTime);
_getch();

}


For NetSim v10:


Inside fn_NetSim_Zigbee_ChangeRadioState() function present in ChangeRadioState.c file add following codes


fprintf (stderr,"\nNode Name\tRemaining_Power \t Time\n");

fprintf(stderr,"\n%s\t%lf mW\t%lf ms\n\n", NETWORK->ppstruDeviceList[nDeviceId-1]->szDeviceName,pstruDevicePower[nDeviceId-1]->dRemainingPower,pstruEventDetails->dEventTime);

_getch();


For NetSim v9:


Inside fn_NetSim_Zigbee_ChangeRadioState() function present in ChangeRadioState.c file add following codes


fprintf (stderr,"\nNode Name\tRemaining_Power \t Time\n");

fprintf(stderr,"\n%s\t%lf mW\t%lf ms\n\n", NETWORK->ppstruDeviceList[nDeviceId-1]->szDeviceName,pstruDevicePower[nDeviceId-1]->dRemainingPower,pstruEventDetails->dEventTime);

getch();



  • Rebuild the Zigbee project and replace the default libZigbee.dll with the modified one.
  • Create a scenario in WSN and simulate it.
  • During the simulation, user can see the remaining power of each node printed to the simulation console along with the timestamp.


Example 2: Print node positions in MANET


Open Mobility Project, and in Mobility.c and go to fn_NetSim_Mobility_Run() function. Inside the default, case add the following codes


For NetSim v11.1, v12, v13.:


fprintf(stderr,"\n The position of %s at time %.2lfms is X=%.2lf and Y = %.2lf \n",DEVICE_NAME(pstruEventDetails->nDeviceId),

pstruEventDetails->dEventTime,

DEVICE_POSITION(pstruEventDetails->nDeviceId)->X,

DEVICE_POSITION(pstruEventDetails->nDeviceId)->Y);

_getch();


  • Build Mobility project,libMobility.dll inside the binary folder of the current workspace will get modified.
  • Create a scenario in MANET and configure the mobility model of the nodes.
  • During the simulation, the user can notice that the positions of the nodes are displayed in the console w.r.t. the simulation time.


For NetSim v10 and v11:


fprintf(stderr,"\n The position of %s at time %.2lfms is X=%.2lf and Y = %.2lf \n",DEVICE_NAME(pstruEventDetails->nDeviceId),

pstruEventDetails->dEventTime,

DEVICE_POSITION(pstruEventDetails->nDeviceId)->X,

DEVICE_POSITION(pstruEventDetails->nDeviceId)->Y);

_getch();


For NetSim v9:


fprintf(stderr,"\n The position of %s at time %.2lfms is X=%.2lf and Y = %.2lf \n",DEVICE_NAME(pstruEventDetails->nDeviceId),

pstruEventDetails->dEventTime,

DEVICE_POSITION(pstruEventDetails->nDeviceId)->X,

DEVICE_POSITION(pstruEventDetails->nDeviceId)->Y);

getch();

  • Build Mobility project and replace libMobility.dll inside the binary folder of NetSim installation directory.
  • Create a scenario in MANET and configure the mobility model of the nodes.
  • During simulation, the user can notice that the positions of the nodes are displayed in the console w.r.t. the simulation time.