Applicable Versions
NetSim StandardNetSim Pro


NOTE: Scroll down to follow steps as per different versions of NetSim


The parameters such as Vehicle Position(X,Y) and current time can be obtained from NetSim Variables:

DEVICE_POSITION(pstruEventDetails->nDeviceId)->X

DEVICE_POSITION(pstruEventDetails->nDeviceId)->Y

pstruEventDetails->dEventTime

The parameters such as vehicle speed, vehicle direction, and vehicle position are associated to SUMO and can be obtained by using TraCI API's 

Follow the steps provided below to obtain SUMO related parameters during run time:


1. Modifying the SUMO interface python script get vehicle parameters using TraCI APIs and write it to pipe file:

The steps to be followed are different for different versions of NetSim. Refer to the appropriate section below:


Applicable Releases
v13.1v13.2v13.3v14.0


1. NetSim SUMO interfacing file SumoRun.py is present in <NetSim_Install_Directory>/bin folder. Make a copy of this file as a backup open the SumoRun.py and perform the following changes(highlighted in red):


if k==1:

        win32file.WriteFile(p1, 'c'.encode())

        #get coordinates

        position_x = str(abs(traci.vehicle.getPosition(i)[0]))

        position_y= str(abs(traci.vehicle.getPosition(i)[1]))

        #send to Netsim

        win32file.WriteFile(p1, position_x.encode())

        win32file.WriteFile(p1, position_y.encode())

        #time.sleep(0.5)

        speed = str(abs(traci.vehicle.getSpeed(i)))

        angle = str(abs(traci.vehicle.getAngle(i)))

        win32file.WriteFile(p1, speed.encode())

        win32file.WriteFile(p1, angle.encode())

        if i == k5[0]:                            

            traci.simulationStep()

    else :

        win32file.WriteFile(p1, 'f'.encode())                

traci.close()

p1.close()


2. Save SumoRun.py with the changes done.


Applicable Releases
v11.1v12v13


1. NetSim SUMO interfacing file SumoRun.py is present in <NetSim_Install_Directory>/bin folder. Make a copy of this file as a backup and open the SumoRun.py and perform the following changes(highlighted in red):


if k == 1:

if i == k5[0] : #For every 1st vehicle present in list of vehicles, simulate

traci.simulationStep()

win32file.WriteFile(p1, 'c')#The vehicle was found, being sent to Netsim for Connection('c' for confirmation)

position_x = str(abs(traci.vehicle.getPosition(i)[0])) #get coordinates

position_y = str(abs(traci.vehicle.getPosition(i)[1]))

#print '\n', position_x, position_y, '\n'

win32file.WriteFile(p1, position_x)#send to Netsim

win32file.WriteFile(p1, position_y)

#time.sleep(0.5)

speed = str(abs(traci.vehicle.getSpeed(i)))

angle = str(abs(traci.vehicle.getAngle(i)))

win32file.WriteFile(p1, speed.encode())

win32file.WriteFile(p1, angle.encode())

else :

win32file.WriteFile(p1, 'f')#Send not found to Netsim('f' for denial of connection)

step += 10

traci.close()

p1.close()


2. Save SumoRun.py with the changes done.


2. Modifying NetSim Source codes to read and display the received vehicle parameters from the pipe file:

The steps involved may vary between different versions of NetSim. Choose the appropriate section of this article, as per the version of NetSim. 


Applicable Releases
v11.1v12v13
v14


Note: From v13.1 Open NetSim source code in Visual Studio by clicking on the Source code option in NetSim Home Screen via Your Work->Source Code->Open Code.


1. Open NetSim source code in Visual Studio 2019 by clicking on the Open Code button present in NetSim Home Screen via Open Simulation->Workspace Options->Open Code.

2. Go to Mobility project through the solution explorer and open the Sumo_interface.c file. In the function *corr(char* id)(), and modify the function definition (highlighted in red) as shown below:


double *corr(char* id){

 BOOL fSuccess;//If reading/writing successfil or not

 DWORD cbRead, cbToWrite, cbWritten;// For Pipes

 CHAR chBuf[BUFSIZ]; //For reading messages from pipes

 double xcor1,ycor1;// x and y coordinates to be received from sumo

 double* coordinates;

 double speed; //speed of vehicle to be received from sumo

 double direction; //direction of vehicle to be received from sumo

.

.

.

.

.

.

.

.

do

  {

   // Read Y coordinate

   fSuccess = ReadFile

    (

    hPipe, // pipe handle

    chBuf, // buffer to receive reply

    BUFSIZ*sizeof(CHAR), // size of buffer

    &cbRead, // number of bytes read

    NULL// not overlapped

    );

   if ( ! fSuccess && GetLastError() != ERROR_MORE_DATA )

    break;

   chBuf[cbRead]=0;

  } while (!fSuccess); // repeat loop if ERROR_MORE_DATA

  ycor1 = atof(chBuf);

  if(ycor1<0)

   ycor1=0;

  do

  {

   // Read vehicle speed

   fSuccess = ReadFile

   (

    hPipe, // pipe handle

    chBuf, // buffer to receive reply

    BUFSIZ * sizeof(CHAR), // size of buffer

    &cbRead, // number of bytes read

    NULL// not overlapped

   );

   if (!fSuccess && GetLastError() != ERROR_MORE_DATA)

    break;

   chBuf[cbRead] = 0;

  } while (!fSuccess); // repeat loop if ERROR_MORE_DATA

  speed = atof(chBuf);

  

  do

  {

   // Read vehicle direction

   fSuccess = ReadFile

   (

    hPipe, // pipe handle

    chBuf, // buffer to receive reply

    BUFSIZ * sizeof(CHAR), // size of buffer

    &cbRead, // number of bytes read

    NULL// not overlapped

   );

   if (!fSuccess && GetLastError() != ERROR_MORE_DATA)

    break;

   chBuf[cbRead] = 0;

  } while (!fSuccess); // repeat loop if ERROR_MORE_DATA

  direction = atof(chBuf);

  fprintf(stderr, "\n%s Direction: %f(Degrees)\tSpeed: %f(m/s)\n", id, direction,speed);

  coordinates = (double*)malloc(2*sizeof* coordinates);

  coordinates[0]=xcor1;

  coordinates[1]=ycor1;

  return (coordinates);//Return X Y Coordinates

 }

 else

 {

  return(NULL);// If vehicle not found, return NULL

 }

}


3. Save the changes and right-click on the Mobility module in the solution explorer and select Rebuild.

4. After a successful build, NetSim will automatically take care of linking the modified source code with simulations performed.


Applicable Releases
v10v11.0

1. Open NetSim source code in Visual Studio 2015/2017 by double-clicking on the NetSim.sln file present in “<NetSim_Install_Directory>/src/Simulation” folder.

2. Go to Mobility project through the solution explorer and open the Sumo_interface.c file. In the function *corr(char* id)(), and modify the function definition (highlighted in red) as shown below:


double *corr(char* id){

 BOOL fSuccess;//If reading/writing successfil or not

 DWORD cbRead, cbToWrite, cbWritten;// For Pipes

 CHAR chBuf[BUFSIZ]; //For reading messages from pipes

 double xcor1,ycor1;// x and y coordinates to be received from sumo

 double* coordinates;

 double speed; //speed of vehicle to be received from sumo

 double direction; //direction of vehicle to be received from sumo

.

.

.

.

.

.

.

.

do

  {

   // Read Y coordinate

   fSuccess = ReadFile

    (

    hPipe, // pipe handle

    chBuf, // buffer to receive reply

    BUFSIZ*sizeof(CHAR), // size of buffer

    &cbRead, // number of bytes read

    NULL// not overlapped

    );

   if ( ! fSuccess && GetLastError() != ERROR_MORE_DATA )

    break;

   chBuf[cbRead]=0;

  } while (!fSuccess); // repeat loop if ERROR_MORE_DATA

  ycor1 = atof(chBuf);

  if(ycor1<0)

   ycor1=0;

  do

  {

   // Read vehicle speed

   fSuccess = ReadFile

   (

    hPipe, // pipe handle

    chBuf, // buffer to receive reply

    BUFSIZ * sizeof(CHAR), // size of buffer

    &cbRead, // number of bytes read

    NULL// not overlapped

   );

   if (!fSuccess && GetLastError() != ERROR_MORE_DATA)

    break;

   chBuf[cbRead] = 0;

  } while (!fSuccess); // repeat loop if ERROR_MORE_DATA

  speed = atof(chBuf);

  

  do

  {

   // Read vehicle direction

   fSuccess = ReadFile

   (

    hPipe, // pipe handle

    chBuf, // buffer to receive reply

    BUFSIZ * sizeof(CHAR), // size of buffer

    &cbRead, // number of bytes read

    NULL// not overlapped

   );

   if (!fSuccess && GetLastError() != ERROR_MORE_DATA)

    break;

   chBuf[cbRead] = 0;

  } while (!fSuccess); // repeat loop if ERROR_MORE_DATA

  direction = atof(chBuf);

  fprintf(stderr, "\n%s Direction: %f(Degrees)\tSpeed: %f(m/s)\n", id, direction,speed);

  coordinates = (double*)malloc(2*sizeof* coordinates);

  coordinates[0]=xcor1;

  coordinates[1]=ycor1;

  return (coordinates);//Return X Y Coordinates

 }

 else

 {

  return(NULL);// If vehicle not found, return NULL

 }

}


3. Save the changes and right-click on the Mobility module in the solution explorer and select Rebuild.

4. Upon successful build, you will get a new libMobility.dll file in the “<NetSim_Install_Directory>/src/Simulation/DLL” folder.

5. Copy this newly-built DLL file and replace it in the bin folder of NetSim after you rename the original libMobility.dll file which is already existing there(as a backup).



Result and Analysis


After this upon running simulations with these code changes, NetSim console will display the vehicle speed and direction obtained from SUMO in the simulation console as shown below:


Visit: https://tetcos.com/vanets.html