This document summarizes the development of a distributed simulation toolbox for MATLAB/Simulink. The toolbox allows for real-time communication between systems using UDP. It was developed in two phases: first, test applications in C++, then S-functions for MATLAB. The C++ applications demonstrated singlecast, multicast, and broadcast transmissions of data arrays. The S-functions translate this functionality into Simulink blocks for UDP send and receive with parameters for port, IP, and data type.
IEEE 802.11-power save mode (PSM) independent basic service set (IBSS) Save, the time is divided into intervals of the signals. At the beginning of each interval signal and power saving alarm periodically all open windows (vocals). The station will be in competition with the rest of the frame window frame sent voice data leakage range. Element depends frame transmission IEEE CSMA / CA as defined in 802.11 DCF. A chance of transmit voice frames type of collision energy IBSS success. This article gives an analysis model with a chance of success output transmission window fixed size element. The results of the simulation analysis of the accuracy of the analysis.
Hybrid Model Based Testing Tool Architecture for Exascale Computing SystemCSCJournals
Exascale computing refers to a computing system which is capable to at least one exaflop in next couple of years. Many new programming models, architectures and algorithms have been introduced to attain the objective for exascale computing system. The primary objective is to enhance the system performance. In modern/super computers, GPU is being used to attain the high computing performance. However, it’s the objective of proposed technologies and programming models is almost same to make the GPU more powerful. But these technologies are still facing the number of challenges including parallelism, scale and complexity and also many more that must be fixed to achieve make computing system more powerful and efficient. In this paper, we have present a testing tool architecture for a parallel programming approach using two programming models as CUDA and OpenMP. Both CUDA and OpenMP could be used to program shared memory and GPU cores. The object of this architecture is to identify the static errors in the program that occurred during writing the code and cause absence of parallelism. Our architecture enforces the developers to write the feasible code through we can avoid from the essential errors in the program and run successfully.
IEEE 802.11-power save mode (PSM) independent basic service set (IBSS) Save, the time is divided into intervals of the signals. At the beginning of each interval signal and power saving alarm periodically all open windows (vocals). The station will be in competition with the rest of the frame window frame sent voice data leakage range. Element depends frame transmission IEEE CSMA / CA as defined in 802.11 DCF. A chance of transmit voice frames type of collision energy IBSS success. This article gives an analysis model with a chance of success output transmission window fixed size element. The results of the simulation analysis of the accuracy of the analysis.
Hybrid Model Based Testing Tool Architecture for Exascale Computing SystemCSCJournals
Exascale computing refers to a computing system which is capable to at least one exaflop in next couple of years. Many new programming models, architectures and algorithms have been introduced to attain the objective for exascale computing system. The primary objective is to enhance the system performance. In modern/super computers, GPU is being used to attain the high computing performance. However, it’s the objective of proposed technologies and programming models is almost same to make the GPU more powerful. But these technologies are still facing the number of challenges including parallelism, scale and complexity and also many more that must be fixed to achieve make computing system more powerful and efficient. In this paper, we have present a testing tool architecture for a parallel programming approach using two programming models as CUDA and OpenMP. Both CUDA and OpenMP could be used to program shared memory and GPU cores. The object of this architecture is to identify the static errors in the program that occurred during writing the code and cause absence of parallelism. Our architecture enforces the developers to write the feasible code through we can avoid from the essential errors in the program and run successfully.
Actividad para nivel preescolar que propicia la formación ciudadana, elaborada por alumnas de séptimo semestre de la Licenciatura en Educación Preescolar
Докладчик: Антон Журков
Агентства, рекламодатели, рекламные площадки и сервисы – это основная аудитория MoscowDigital. Мы собираемся вместе и прекрасно проводим время, постигая новые технологии и общаясь в кулуарах, но почему же столько шишек набивается в процессе совместной работы? Антон успел поработать с каждой стороны баррикад и на основе своего жизненного опыта расскажет о том, какие недопонимания возникают на всех этапах работы: от постановки цели и задачи, определения бюджета и сроков до презентации оценки результатов. Каждый сможет не только узнать себя, но и понять мотивацию партнера.
I was asked to give an orientation to supporting historically underrepresented students in business and accounting schools. We showed an overview of the topic with basic definitions and compared two college program examples.
Stratégie Responsabilité Sociétale des Entreprises (RSE) et marchés publics Frédéric Grevey
Faites de votre politique RSE un atout !
Vous avez peut-être déjà une politique RSE sans le savoir…
Apprenez comment mettre en avant vos actions quotidiennes d’économies d’énergies, recyclage, sociales pour remporter plus de marchés.
Thèmes :
- Comprendre la notion de développement durable dans les marchés publics
- Appréhender la notion de Responsabilité Sociétale des Entreprises
- Faire l’état des lieux de sa politique RSE et valoriser l’existant
Identifier les marges de progrès
Pour en savoir plus sur la non-violence et sur la résolution non-violente de conflits : www.irnc.org , rubrique « Diaporamas »
Pour en savoir plus sur les « chercheurs d’humanité » (non-violence, alternatives économiques, écologie, changement sociétal, spiritualité) : www.irnc.org, rubrique « Diaporamas »
Les étapes de l'histoire de la responsabilité sociale et environnementale des entreprises
Pourquoi et comment entreprises font le choix de devenir responsables
How to conduct an anti-money laundering (AML) system assessmentKeith Furst
This presentation was given on October 4, 2016 at the Toronto Marriott Downtown Eaton Centre Hotel at the 14th installment of Canada’s premier event in the field of money laundering compliance and control. The theme of Money Laundering in Canada 2016 is Financial Crime, Compliance, and Regulation: Keeping Pace with the Times.
Actividad para nivel preescolar que propicia la formación ciudadana, elaborada por alumnas de séptimo semestre de la Licenciatura en Educación Preescolar
Докладчик: Антон Журков
Агентства, рекламодатели, рекламные площадки и сервисы – это основная аудитория MoscowDigital. Мы собираемся вместе и прекрасно проводим время, постигая новые технологии и общаясь в кулуарах, но почему же столько шишек набивается в процессе совместной работы? Антон успел поработать с каждой стороны баррикад и на основе своего жизненного опыта расскажет о том, какие недопонимания возникают на всех этапах работы: от постановки цели и задачи, определения бюджета и сроков до презентации оценки результатов. Каждый сможет не только узнать себя, но и понять мотивацию партнера.
I was asked to give an orientation to supporting historically underrepresented students in business and accounting schools. We showed an overview of the topic with basic definitions and compared two college program examples.
Stratégie Responsabilité Sociétale des Entreprises (RSE) et marchés publics Frédéric Grevey
Faites de votre politique RSE un atout !
Vous avez peut-être déjà une politique RSE sans le savoir…
Apprenez comment mettre en avant vos actions quotidiennes d’économies d’énergies, recyclage, sociales pour remporter plus de marchés.
Thèmes :
- Comprendre la notion de développement durable dans les marchés publics
- Appréhender la notion de Responsabilité Sociétale des Entreprises
- Faire l’état des lieux de sa politique RSE et valoriser l’existant
Identifier les marges de progrès
Pour en savoir plus sur la non-violence et sur la résolution non-violente de conflits : www.irnc.org , rubrique « Diaporamas »
Pour en savoir plus sur les « chercheurs d’humanité » (non-violence, alternatives économiques, écologie, changement sociétal, spiritualité) : www.irnc.org, rubrique « Diaporamas »
Les étapes de l'histoire de la responsabilité sociale et environnementale des entreprises
Pourquoi et comment entreprises font le choix de devenir responsables
How to conduct an anti-money laundering (AML) system assessmentKeith Furst
This presentation was given on October 4, 2016 at the Toronto Marriott Downtown Eaton Centre Hotel at the 14th installment of Canada’s premier event in the field of money laundering compliance and control. The theme of Money Laundering in Canada 2016 is Financial Crime, Compliance, and Regulation: Keeping Pace with the Times.
The following resources come from the 2009/10 BEng in Digital Systems and Computer Engineering (course number 2ELE0065) from the University of Hertfordshire. All the mini projects are designed as level two modules of the undergraduate programmes.
The objectives of this module are to demonstrate, within an embedded development environment:
Processor – to – processor communication
Multiple processors to perform one computation task using parallel processing
This project requires the establishment of a communication protocol between two 68000-based microcomputer systems. Using ‘C’, students will write software to control all aspects of complex data transfer system, demonstrating knowledge of handshaking, transmission protocols, transmission overhead, bandwidth, memory addressing. Students will then demonstrate and analyse parallel processing of a mathematical problem using two processors. This project requires two students working as a team.
1. Summer Internship 2010
Development of a
MATLAB/Simulink - Toolbox for
Distributed Simulation
Author:
James Dianics
Supervisor:
Prof. Dr.-Ing. Reinhard
Finsterwalder
July 30, 2010
2. Abstract
This report gives an overview of the development of a distributed simulation toolbox for
Matlab/Simulink. The project was developed as a sub-project to the MASTER program
which is designed to be a student built flight simulator for education and research. The
model dynamics for the flight simulator has been built and implemented in the Mat-
lab/Simulink environment. A toolbox to allow for the real time communication between
systems is necessary. This project specifically is to build the UDP solution of the dis-
tributed simulation toolbox which will contain the ability to transmit via Singlecast,
Multicast and Broadcast. The creation of the toolbox was accomplished in two phases;
the development of test applications in C++, and then the development of the UDPSend
and UDPReceive S-functions for Matlab.
4. 1 Introduction
1.1 Project MASTER
The MASTER project was started in 2001 at the University of the Federal Armed Forces
Munich, Germany. The purpose of the MASTER project is the development of a modular
vehicle simulator with exchangeable cockpit modules for education and research appli-
cation. Thus ”MASTER” is an acronym which stands for Modular Advanced Simulator
Technology for Education and Research. MASTER has been structured so that the ma-
jority of the work will be accomplished by student projects.
1.2 Distributed Simulation
In the development of a large simulation or application a single computer’s resources are
often insufficient. Distributed computing allows for either parallel computing on a single
machine with multiple cores or among many nodes in a network; either solution provides
an increased amount of hardware resources resulting in better application performance.
1.3 Universal Datagram Protocol (UDP)
There are two commonly used communication protocols, Transmission Control Protocol
(TCP) and Universal Datagram Protocol (UDP). TCP is commonly used for streaming
data between two clients such as a web browser and web server. The main points of TCP
that were of a concern are that TCP utilizes error checking and creates a buffer waiting
for delayed data before releasing the packet, both of these functions delay the speed of
the data but give a more reliable signal.
UDP does not utilize error checking and by definition is unreliable. This attribute can
be beneficial if used correctly because it allows for less signal delay because of the lack of
processing on the network that needs to be done before receiving a signal. One must be
careful about its application and understand that packets could be missing, duplicated or
arrive out of order. Another benefit of UDP is that it is capable of Singlecast, Multicast,
and Broadcast transmissions where as TCP is only capable of Singlecast. Singlecast is
the typical single client single server transmission. Multicast is located on a different
IP range and theoretically a single client can send a message to an unlimited number of
listening servers.
UDP is best used during real-time applications where speed is required. This could be
during VoIP, IP-TV or applications such as Skype where lost packets could mean a smear
in the picture but the overall quality of the picture shall remain good if the connection
is decent. UDP was chosen for the MASTER project because of the need for real-time
communication and varying transmission capabilities.
2
5. Figure 1: Route Sequences Potential Receivers(Yellow)
Figure 2: Singlecast
Figure 3: Multicast
Figure 4: Broadcast
2 Programming UDP
Windows sockets 2 (Winsock2) is an API that allows for programmers to easily create
networking functions for applications. In order to create networking ability only a few
windows socket functions are necessary.
struct WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
WSADATA is a structure that is required to define the functionality of the windows socket
and the function WSAStartup takes a pointer to the structure and initializes the socket
in the windows operating system. This is necessary for every socket ran on a windows
system. When the socket is closed conversely WSACleanup(); needs to be used to delete
and free the socket on the windows system.
3
6. To create a socket a call
s = socket(AF_INET,SOCK_DGRAM,0);
Where AF INET is the address family associated with TCP and UDP type protocols.
SOCK DGRAM is a connectionless type protocol for UDP transmission. Thus the socket
being created here is defined for UDP connectionless use.
To set where the socket is listening(server side) or where the socket is sending datagrams
(client side) the SOCKADDR IN structure needs to be defined.
memset (&addr,0,sizeof(SOCKADDR_IN));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr=inet_addr(IP);
Where the memory at the structures location is cleared and set with the family, port, and
IP address in that respective order. To accomplish communication there are four basic
functions for TCP and UDP.
TCP utilizes connections and thus uses different functions for sending and receiving data
send() and recv()
UDP is connectionless and uses
sendto() and recvfrom()
because the client and server is not strictly defined before this function.
sendto ( s , buffer , sizeof ( buffer ) , 0 ,(SOCKADDR∗)&addr , sizeof (SOCKADDR IN) ) ;
recvfrom ( s , buffer , sizeof ( buffer ) ,0 ,NULL,NULL) ;
When finished with the socket use
closesocket(s);
Combined together this would be the most basic of UDP programming required to get a
simple application running using Winsock2.
4
7. 3 C++ Test Application
Before the development of the Matlab/Simulink distributed simulation toolbox two test
applications were built using Microsoft’s Visual C++ application development studio.
The applications were designed to meet the requirements that needed to be met in the
Matlab environment. Requirements for the Client
• The client shall utilize UDP protocol
• The client shall send arrays of data
• The client shall be able to send all data in byte format
• The client shall be able to send Singlecast, Multicast and Broadcast transmissions
• The client shall be able to send in real-time
Requirements for the Server
• The server shall utilize UDP protocol.
• The server shall receive datagram packets and be able to unpack them into a spec-
ified data type.
• The server shall be able to join Multicast groups.
• The server shall be able to receive Singlecast and Broadcast Transmissions.
• The server shall be able to receive transmission in real-time
Requirements for packet transmission protocol
• The packet shall contain a 3 integer header in byte format.
• The header shall contain information about the size of the packet, the data type of
the data contained and the number of elements contained in the array being sent
or received.
• Preceding the header the data array shall be contained in a byte format.
Figure 5: Packet Protocol
5
8. Data types that the classes currently support were included due to their definition by
Matlab/Simulink
Figure 6: Matlab/Simulink Data Types
Two classes were developed to handle the socket and UDP functions to meet the require-
ments of the project. Two classes were developed so that a TCPsocket class could later
be developed extending the socket class.
6
9. 3.1 Developed Classes
Figure 7: Class Diagram
Use of the two classes to develop a UDP server or client is fairly straightforward.
The socket class defines two socket structures and stores them as protected variables.
• addr
• s
The socket class also contains two functions.
• bind(): Connect the socket to a port
• close(): Closes a socket
The udpsocket class utilizes the windows socket functions and the developed protocol to
send and receive data
• create(): Creates a udpsocket object
• setdest(): Sets the destination port and IP
• joinmulticast(): Joins a multi-cast group at a specific IP
• pack(): packs an array of data into the previously discussed protocol
• send(): Sends a package on the udpsocket
• startthread(): Sets a parameter to prepare for receivethread
• receivethread(): starts another thread to fill data buffers from the incoming signal
7
10. • getdata(): returns the data from the receivethread() function, size, type, elements,
and package
• endthread(): prepares the thread for closing and deleting
• closethread(): closes the thread
• getaddr(): gets the SOCKADDR IN structure from the socket class
The template function compile accepts an unknown data type to convert the UDP pro-
tocol byte form back to a designated data type (float, int, etc...)
template <class Unk>
void compile (char*message,int size,Unk* &data)
{
byte *temp,*temp2,*temp3;
temp3 = new byte[size];
temp = (byte*)message;
for (int i = 0; i<(size/sizeof(Unk)); i++)
{
temp2 = temp+12+i*sizeof(Unk);
memcpy(temp3+sizeof(Unk)*i,temp2,sizeof(Unk));
}
data = (Unk*)temp3;
3.2 Sample Server Application in C++
All of this is complied into the main thread to make an example server in
c++...
int main (int argc, char *argv[])
{
printf("UDP servern");
UDPsocket s;
char message[256];
int size,type;
if(s.create())
{
s.close();
printf("Error on createn");
return 1;
}
if(s.bind())
{
8
11. s.close();
printf("error on bindn");
system ("PAUSE");
return 1;
}
if(s.joinmulticast())
{
printf("Error on joinmulticastn");
s.close();
return 1;
}
Here the UDPsocket object is created and the required variables are declared
to obtain a message and compile it. The socket is created, bound, and joined
to a multi-cast IP all with error checking.
Next the receivethread function is intialized with the startthread() function
call and a while loop is ran to get data from the buffers.
s.startthread();
s.getdata(message,size,type,message_len)
if(message_len>0&&size>0)
{
switch(type)
{
case INT:
{
int * data = new int[size/sizeof(int)];
compile(message,size,data);
for(int i = 0;i<size/sizeof(int);i++)
{printf("%in",data[i]);
cout << data[i] << endl;
}
delete data;
break;
}
If the message length is greater than 0 (meaning that a message has been
received) a switch structure is used to determine the data type based on the
type received and the data is compiled and read out. The data type has been
enumerated in the socket class.
s.endthread();
s.closethread();
s.close();
9
12. When the server is finished the thread listening on the socket is ended and
closed and the socket is then closed.
4 Matlab/Simulink Toolbox
MATLAB is a well known and widely used technical computing software package. To-
gether with Simulink one can simulate and do model-based design for multi domain sys-
tems. To allow for the communication of multiple computers in the Matlab and Simulink
environment the C++ test applications were translated into S-functions.
S-functions in general are a computer language description of a single Simulink block.
S-functions can be written in many languages ( C++, C, Fortran etc) and then compiled
into a MEX (Matlab Executable) file which is implemented in Simulink.
Understanding how Simulink and S-functions work is important when beginning to build
a Simulink block. In a very simple explanation the block accepts at every time step,
calculates a result, and sends an output.
Figure 8: Simulink Engine
10
13. Every S - Function follows a strict structure the most basic template includes these 4
main functions.
static void m d l I n i t i a l i z e S i z e s ( SimStruct ∗S)}
static void mdlInitializeSampleTimes ( SimStruct ∗S)
static void mdlStart ( SimStruct ∗S)
static void mdlOutputs ( SimStruct ∗S)
static void mdlTerminate ( SimStruct ∗S)
4.1 Outline of UDPSend S-Function
mdlInitializeSizes
1. Set the number of parameters(3 for the send block)
2. Set the input port width and data type (1 input with inherited width and data
type)
3. Set number of output ports (0 outputs)
mdlInitializeSampleTimes
1. Set sample times
mdlStart
1. Get parameters from the block
2. Create a UDPsocket object
3. Based upon parameters set the destination to send the data
4. Save the socket in the PWork Vector for later use
mdlOutputs
1. Get UDPsocket object from PWork Vector
2. Based upon data type pack and send the data
mdlTerminate
1. Get UDPsocket object from PWork vector
2. Close and delete the socket
11
14. Every S-function has inputs and outputs which are defined during the initialization.
With the UDPsend block it is initialized to have a single input with data type and width
inherited from the driving block. MDLstart is only ran at the beginning of the simulation
here is where the socket is opened and set using the parameters provided by the user.
The output function will be ran at every time step and is being used to send the data
from. Terminate is only ran at the end of the simulation and this is where we simply
close and delete the UDP socket object from memory.
4.2 Outline of UDPReceive S-Function
mdlInitializeSizes
1. Set the number of parameters (5 for the receive block)
2. Set the input port width and data type (0 inputs)
3. Set number of output ports (1 output)
4. Set OutputPortWidth with the Elements parameter
5. Set OutputPortDataType withe the Data Type parameter
mdlInitializeSampleTimes
1. Set sample times
mdlStart
1. Get parameters from the block and check if the IP parameter is a string
2. Create a UDPsocket object
3. Bind the socket to the port based on the parameter
4. Based upon a parameter join a Multicast group or listen on broadcast
5. start the receivethread with s.startthread() function
6. Save the socket in the PWork Vector for later use
mdlOutputs
1. Get UDPsocket object from PWork Vector
2. s.getdata(message,size,type,message length) to gather data
3. If a message is received use a switch structure on type to compile
4. obtain pointer to the output port real T *y = (real T*)ssGetOutputPortSignal(S,0)
5. copy the compiled data to the output port
12
15. mdlTerminate
1. Get UDPsocket object from PWork vector
2. s.endthread()
3. s.closethread()
4. Close and delete the socket
4.3 Creating a Library of S-Function Blocks
Before a S-function can be used it needs to be compiled into a MEX file. This requires
linking all of the necessary files so that Matlab can create an executable for its environ-
ment. In the Matlab command prompt compile the send and receive functions developed
during this project using the following two commands.
• mex UDPSend.cpp udpsocket.cpp WS2 32.lib socket.cpp
• mex UDPReceive.cpp udpsocket.cpp WS2 32.lib socket.cpp
Once successfully compiled in the Matlab environment a library can easily be built.
1. Open a new simulink model
2. Add two new S-functions to the model from the User-Defined Functions library
3. Open the S-function parameters by double clicking
4. Under S-Function name: insert UDPSend in one and UDPReceive in the
other
5. Select File ... New ... Library
6. Drag both created S-Functions to the new library
7. Save the Library file (.mdl) in its on folder in the Matlab path
8. In this new folder create a new .m file ”slblocks”
The slblocks.m file gives Simulink a reference of how label and use the library. In a short
example it tells it what folder the files are in and how to name the library.
function blkStruct = slblocks
% Specify that the product should appear in the library browser
% and be cached in its repository
Browser.Library = ’UDPCommBlocks’;
Browser.Name = ’UDP Communication Blocks’;
blkStruct.Browser = Browser;
Is all that is necessary to be included in the .m file.
13
16. 4.4 Masking the Blocks
To add a mask to the block open the .mdl library file created holding the blocks
1. Unlock the Library by selecting Edit ... Unlock Library in the library window
2. right click on the block and select Mask S-Function...
3. Proceed to the Parameters tab
4. Add a new Parameter
• Prompt is the title that will show up in the mask
• Variable is the variable that will be passed to the S-Function
• Type is the type of input from the user
5. Once all parameters are defined in the mask they need to be passed to the S-
Function
6. Right click again on the block and select Look Under Mask
7. Add the parameter variables to the parameters list
8. block description and other documentation can be handled within the other tabs
14
18. 4.5 Example Simulink Models using Distributed Simulation Tool-
box
Figure 11: S-Function UDPSend - Transmitting position and orientation data from the
flight model to the visualization engine
16
19. Figure 12: S-Function UDPReceive - Receiving position and orientation data in the
visualization engine
5 Results
A Matlab/Simulink Distributed Simulation Toolbox was successfully developed using
the UDP transmission protocol. The send and receive blocks are able to send arrays
of all Matlab data types using Singlecast, Multicast or Broadcast transmissions. Some
problems arose while writing the receive S-Function from the developed C++ test ap-
plication. Originally a blocking socket was used to receive data in the test application
but would crash Simulink when implemented as a S-Function. As a temporary solution
a non-blocking socket was used to successfully complete the S-Function. While testing
the S- Function, strange behavior was observed where data was still is received after
the send model had stopped running; this was attributed to the non-blocking socket
building a buffer on the network. The final solution was found by creating a separate
thread to operate a blocking socket that would listen for incoming messages, this thread
could run continuously without delaying Simulink and is the final successful solution. The
distributed simulation toolbox could be further improved by also implementing other net-
work protocols into separate blocks, notably TCP could be useful in other applications
where error checking is necessary. The UDP blocks could also be improved by adding the
ability to send structures or user created data types.
17
20. 6 References
Anonymous (2010). Writing S-Functions. The
Mathworks Inc., Natick, USA.
Anonymous (2010). MathWorks - MATLAB and Simulink for Technical Computing.
<http://www.mathworks.com/>.
Anonymous (2010). Windows Sockets 2 [online].
<http://msdn.microsoft.com/enus/
library/ms740673(VS.85).aspx>.
7 Appendix
7.1 socket.h and socket.cpp
socket.h
#ifndef SOCKET H
#define SOCKET H
#include <winsock2 . h>
#include <ws2tcpip . h>
c l a s s Socket
{
public :
enum datatype
{
DOUBLE = 1 ,
SINGLE = 2 ,
BOOLEAN = 9 ,
INT8 =3,
UINT8=4,
INT16 = 5 ,
UINT16 = 6 ,
INT32 = 7 ,
UINT32 = 8 ,
};
Socket ( ) ;
˜ Socket ( ) ;
int c l o s e ( ) ;
int bind ( int port =12345);
protected :
18
21. SOCKET s ;
SOCKADDR IN addr ;
};
#endif
socket.cpp
#include ” socket . h”
Socket : : Socket ()
{
WSADATA wsaData ;
WSAStartup(MAKEWORD(2 ,2) ,& wsaData ) ;
}
Socket : : ˜ Socket ()
{
WSACleanup ( ) ;
}
int Socket : : c l o s e ()
{
i f ( clos esocket ( s)==SOCKET ERROR)
{
return 1;
}
return 0;
}
int Socket : : bind ( int port )
{
memset (&addr ,0 , sizeof (SOCKADDR IN) ) ;
addr . s in fa mi ly = AF INET;
addr . sin port = htons ( port ) ;
addr . sin addr . s addr=INADDR ANY;
i f ( : : bind ( s , (SOCKADDR∗)&addr ,
sizeof (SOCKADDR IN)) == SOCKET ERROR)
{
return 1;
}
return 0;
}
19
22. 7.2 udpsocket.h and udpsocket.cpp
udpsocket.h
#ifndef UDPSOCKET H
#define UDPSOCKET H
#include ” socket . h”
#include <windows . h>
#include <process . h>
c l a s s UDPsocket : public Socket
{
public :
UDPsocket ( ) ;
˜UDPsocket ( ) ;
int create ( ) ;
SOCKADDR IN getaddr (){ return addr ;}
SOCKET getsocket (){ return s ; }
int joinmulticast (char∗ IP=” 2 2 6 . 0 . 0 . 1 ” ) ;
void setdest (char∗ IP=” 2 2 6 . 0 . 0 . 1 ” , int port = 12345);
int send ( void∗ buffer , int b u f f e r l e n ) ;
unsigned char∗ pack ( void∗ number , int type , int s i z e ) ;
static long WINAPI receivethread (LPVOID Param ) ;
void getdata (char ∗msg , int &size , int &type , int &msg len ) ;
void startthread ( ) ;
void endthread ( ) ;
void closethread ( ) ;
private :
};
#endif
udpsocket.cpp
#include "UDPsocket.h"
#include "simstruc.h"
SOCKET s = INVALID_SOCKET;
int g_message_len=0,g_size=0,g_type=0;
char g_buffer[2000];
HANDLE rthread=0;
int param;
UDPsocket::UDPsocket ()
{
}
20
24. case Socket::DOUBLE:
{x = sizeof(real_T);
break;
}
case Socket::SINGLE:
{x = sizeof(real32_T);
break;
}
case Socket::BOOLEAN:
{ x = sizeof(boolean_T);
break;
}
case Socket::INT8:
{
x = sizeof(int8_T);
break;
}
case Socket::UINT8:
{
x = sizeof(uint8_T);
break;
}
case Socket::INT16:
{
x = sizeof(int16_T);
break;
}
case Socket::UINT16:
{
x = sizeof(uint16_T);
break;
}
case Socket::INT32:
{
x = sizeof(int32_T);
break;
}
case Socket::UINT32:
{
x = sizeof(uint32_T);
break;
}
}
size = size + 12;
byte* packet = new byte[size];
byte* total,*elements,*typ;
int e = (size-12)/x;
22
26. param = 0;
}
void UDPsocket::closethread()
{
CloseHandle(rthread);
}
7.3 S-Functions
UDPSend
#define S_FUNCTION_NAME UDPSend_gen
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
#include <WinSock2.h>
#include <stdio.h>
#include "udpsocket.h"
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S, 3);
//Parameter 1 :: IP to broadcast to
//Parameter 2 :: Port
//Parameter 3:: Broadcast Type
// Parameter mismatch will be reported by Simulink
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S))
{
return;
}
// 1 input that is dynamically_sized with direct feedthrough
if (!ssSetNumInputPorts(S, 1)) return;
ssSetInputPortWidth(S, 0, DYNAMICALLY_SIZED);
ssSetInputPortDataType(S,0,DYNAMICALLY_TYPED);
ssSetInputPortDirectFeedThrough(S, 0, 1);
if (!ssSetNumOutputPorts(S,0)) return;
//one sample time initialized below
ssSetNumSampleTimes(S, 1);
// Reserve place for C++ object Socket
ssSetNumPWork(S, 1);
24
27. ssSetOptions(S,
SS_OPTION_WORKS_WITH_CODE_REUSE |
SS_OPTION_EXCEPTION_FREE_CODE);
}
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
}
#define MDL_START
static void mdlStart(SimStruct *S)
{
const real_T * brdc = mxGetPr(ssGetSFcnParam(S,2));
//check if ip is a string else report error
if(brdc[0]==2||brdc[0]==1)
{
if (!mxIsChar(ssGetSFcnParam(S,0)) ||
(mxGetM(ssGetSFcnParam(S,0)) != 1 ) ) {
ssSetErrorStatus(S,"First Parameter must be a string");
return;
}
}
//Find Size of String
mwSize buflen= mxGetN(ssGetSFcnParam(S,0))*sizeof(mxChar)+1;
char* IP;
IP = (char*)mxMalloc(buflen);
//write the parameter to the string IP
mxGetString(ssGetSFcnParam(S,0),IP,buflen);
//get port from the second parameter
const real_T * port = mxGetPr(ssGetSFcnParam(S,1));
//Create and setdest of socket
UDPsocket * s= new UDPsocket();
s->create();
if(brdc[0]==3)
{
setsockopt(s->getsocket(),SOL_SOCKET,
SO_BROADCAST,"1",sizeof(char));
s->setdest("255.255.255.255",port[0]);
}
else
{
s->setdest(IP,port[0]);
}
25
28. //Deallocate the string formed with mxgetstring()
mxFree(IP);
//save socket to be used later in PWork vector
ssGetPWork(S)[0]=s;
}
static void mdlOutputs(SimStruct *S, int_T tid)
{
int elements,size;
const char * tname;
//Get Socket from PWork vector
UDPsocket *s = static_cast<UDPsocket*>(ssGetPWork(S)[0]);
DTypeId inpt = ssGetInputPortDataType(S,0);
size = ssGetDataTypeSize(S,inpt);
elements = ssGetCurrentInputPortWidth(S,0);
size = elements*size;
tname = ssGetDataTypeName(S,inpt);
int_T id = ssGetDataTypeId(S,tname);
//Get Pointer to data from input port
boolean_T c = ssGetInputPortRequiredContiguous(S,0);
if(c)
{
real_T * ptr = (real_T*)ssGetInputPortSignal(S,0);
s->pack((void*)ptr,id,size);
}
else
{
InputPtrsType ptr = ssGetInputPortSignalPtrs(S,0);
switch(id+1)
{
case Socket::DOUBLE:
{
InputRealPtrsType u = (InputRealPtrsType)ptr;
s->pack((void*)*u,id,size);
break;
}
case Socket::SINGLE:
{
InputReal32PtrsType u = (InputReal32PtrsType)ptr;
26
29. s->pack((void*)*u,id,size);
break;
}
case Socket::INT8:
{
InputInt8PtrsType u = (InputInt8PtrsType)ptr;
s->pack((void*)*u,id,size);
break;
}
case Socket::UINT8:
{
InputUInt8PtrsType u = (InputUInt8PtrsType)ptr;
s->pack((void*)*u,id,size);
break;
}
case Socket::INT16:
{
InputInt16PtrsType u = (InputInt16PtrsType)ptr;
s->pack((void*)*u,id,size);
break;
}
case Socket::UINT16:
{
InputUInt16PtrsType u = (InputUInt16PtrsType)ptr;
s->pack((void*)*u,id,size);
break;
}
case Socket::INT32:
{
InputInt32PtrsType u = (InputInt32PtrsType)ptr;
s->pack((void*)*u,id,size);
break;
}
case Socket::UINT32:
{
InputUInt32PtrsType u = (InputUInt32PtrsType)ptr;
s->pack((void*)*u,id,size);
break;
}
case Socket::BOOLEAN:
{
InputBooleanPtrsType u = (InputBooleanPtrsType)ptr;
s->pack((void*)*u,id,size);
break;
}
}
27
30. }
}
static void mdlTerminate(SimStruct *S)
{
UDPsocket *s = static_cast<UDPsocket*>(ssGetPWork(S)[0]);
s->close();
delete s;
}
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif
UDPReceive
#define S_FUNCTION_NAME UDPReceive_gen
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
#include <WinSock2.h>
#include <stdio.h>
#include "udpsocket.h"
#include <process.h>
template <class Unk>
void compile (char*messages,int sizes,Unk* &data)
{
byte *temp,*temp2,*temp3;
temp3 = new byte[sizes];
temp = (byte*)messages;
for (int i = 0; i<(sizes/sizeof(Unk)); i++)
{
temp2 = temp+12+i*sizeof(Unk);
memcpy(temp3+sizeof(Unk)*i,temp2,sizeof(Unk));
}
data = (Unk*)temp3;
//delete temp3;
}
28
31. static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S, 5);
//Parameter 1:: IP address as string
//Parameter 2:: Port
//Parameter 3:: DataType 0-8
//Parameter 4:: Number of elements
//Parameter 5:: Broadcast type
// Parameter mismatch will be reported by Simulink
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
return;
}
const real_T * dtype = mxGetPr(ssGetSFcnParam(S,2));
const real_T * elem = mxGetPr(ssGetSFcnParam(S,3));
if (!ssSetNumInputPorts(S, 0)) return;
if (!ssSetNumOutputPorts(S,1)) return;
ssSetOutputPortWidth(S, 0, elem[0]);
ssSetOutputPortDataType(S,0,(dtype[0]-1));
ssSetNumSampleTimes(S, 1);
// Reserve place for C++ object
ssSetNumPWork(S, 1);
ssSetOptions(S,0);
}
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
}
#define MDL_START
static void mdlStart(SimStruct *S)
{
//check if ip is a string else report error
if (!mxIsChar(ssGetSFcnParam(S,0)) ||
(mxGetM(ssGetSFcnParam(S,0)) != 1 ) ) {
ssSetErrorStatus(S,"First Parameter must be a string");
29
32. return;
}
//Find Size of String
mwSize buflen= mxGetN(ssGetSFcnParam(S,0))*sizeof(mxChar)+1;
char* IP;
IP = (char*)mxMalloc(buflen);
//write the parameter to the string IP
mxGetString(ssGetSFcnParam(S,0),IP,buflen);
//get port from the second parameter
const real_T * port = mxGetPr(ssGetSFcnParam(S,1));
//get broadcast type
const real_T * brdc = mxGetPr(ssGetSFcnParam(S,4));
UDPsocket *s = new UDPsocket();
if(s->create())
{
s->close();
ssPrintf("Error on createn");
system ("PAUSE");
}
if(s->bind(port[0]))
{
s->close();
ssPrintf("error on bindn");
system ("PAUSE");
}
int btype = (int)brdc[0];
switch(btype)
{
case 1:
{
if(s->joinmulticast(IP))
{
ssPrintf("Error on joinmulticastn");
system ("PAUSE");
s->close();
}
break;
}
case 3:
{
setsockopt(s->getsocket(),SOL_SOCKET,SO_BROADCAST,"1",sizeof(char));
break;
}
30
33. }
mxFree(IP);
s->startthread();
ssGetPWork(S)[0]=s;
}
static void mdlOutputs(SimStruct *S, int_T tid)
{
UDPsocket *s = static_cast<UDPsocket*>(ssGetPWork(S)[0]);
char message[2000];
int message_len=0;
int size=0, type=0;
s->getdata(message,size,type,message_len);
if(message_len >0&&size>0)
{
switch(type+1)
{
case Socket::DOUBLE:
{
real_T * data = new real_T[size/sizeof(real_T)];
compile(message,size,data);
real_T *y =(real_T*)ssGetOutputPortSignal(S,0);
memcpy(y, data, size);
delete data;
break;
}
case Socket::SINGLE:
{
real32_T * data = new real32_T[size/sizeof(real32_T)];
compile(message,size,data);
real32_T *y = (real32_T*)ssGetOutputPortSignal(S,0);
memcpy(y, data, size);
delete data;
break;
}
31
34. case Socket::INT8:
{
int8_T *data = new int8_T[size/sizeof(int8_T)];
compile(message,size,data);
int8_T *y = (int8_T*)ssGetOutputPortSignal(S,0);
memcpy(y, data, size);
delete data;
break;
}
case Socket::UINT8:
{
uint8_T *data = new uint8_T[size/sizeof(uint8_T)];
compile(message,size,data);
uint8_T *y = (uint8_T*)ssGetOutputPortSignal(S,0);
memcpy(y, data, size);
delete data;
break;
}
case Socket::INT16:
{
int16_T *data = new int16_T[size/sizeof(int16_T)];
compile(message,size,data);
int16_T *y = (int16_T*)ssGetOutputPortSignal(S,0);
memcpy(y, data, size);
delete data;
break;
}
case Socket::UINT16:
{
uint16_T *data = new uint16_T[size/sizeof(uint16_T)];
compile(message,size,data);
uint16_T *y = (uint16_T*)ssGetOutputPortSignal(S,0);
memcpy(y, data, size);;
delete data;
break;
}
case Socket::INT32:
{
int32_T *data = new int32_T[size/sizeof(int32_T)];
compile(message,size,data);
int32_T *y = (int32_T*)ssGetOutputPortSignal(S,0);
memcpy(y, data, size);
delete data;
break;
}
case Socket::UINT32:
{
uint32_T *data = new uint32_T[size/sizeof(uint32_T)];
32