help in udp and s-function?

Status
Not open for further replies.

asic1984

Full Member level 5
Joined
Nov 15, 2003
Messages
257
Helped
4
Reputation
8
Reaction score
1
Trophy points
1,298
Activity points
2,340
hi

i tried to make this code to receive data with the udp protocol but it doesnot receive any thing

i wish that any one can help in this s-function as this is very urgent to me

Code:
#define S_FUNCTION_NAME  sfuntaha_r //receiver
#define S_FUNCTION_LEVEL 2

#include <afx.h>
#include <afxsock.h>
#include <time.h>
#include <string>
#include <stdlib.h>
//#include <winsock2.h>
//

WSADATA              wsaData;
   SOCKET               ReceivingSocket;
   SOCKADDR_IN          ReceiverAddr;
   int                  Port = 6500;
   char                 ReceiveBuf[1024];
   int                  BufLength =1024;
   SOCKADDR_IN          SenderAddr;
   int                  SenderAddrSize = sizeof(SenderAddr);
   bool connected=false;

//


/*
 * Need to include simstruc.h for the definition of the SimStruct and
 * its associated macro definitions.
 */
#include "simstruc.h"





// S-function parameters
#define NUM_PARAMS (1)
#define TS_PARAM (ssGetSFcnParam(S,0))


// Macros to access the S-function parameter values
#define SAMPLE_TIME (mxGetPr(TS_PARAM)[0])








/*====================*
 * S-function methods *
 *====================*/

/* Function: mdlInitializeSizes ===============================================
 * Abstract:
 *    The sizes information is used by Simulink to determine the S-function
 *    block's characteristics (number of inputs, outputs, states, etc.).
 */
static void mdlInitializeSizes(SimStruct *S)
{
    /* See sfuntmpl_doc.c for more details on the macros below */

    ssSetNumSFcnParams(S, NUM_PARAMS);  /* Number of expected parameters */
    if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
        /* Return if number of expected != number of actual parameters */
        return;
    }

    ssSetNumContStates(S, 0);
    ssSetNumDiscStates(S, 0);

    if (!ssSetNumInputPorts(S, 0)) return;

	
	

	// There is one numerical output from this function
    if (!ssSetNumOutputPorts(S, 1)) return;
	// output
    ssSetOutputPortWidth(S, 0, 4);
    


	ssSetNumSampleTimes(S, 1);
    ssSetNumRWork(S, 0);
    ssSetNumIWork(S, 0);
    ssSetNumPWork(S, 0);
    ssSetNumModes(S, 0);
    ssSetNumNonsampledZCs(S, 0);

    ssSetOptions(S, 0);

	// Initialize sockets
    //AfxSocketInit(NULL);
}



/* Function: mdlInitializeSampleTimes =========================================
 * Abstract:
 *    This function is used to specify the sample time(s) for your
 *    S-function. You must register the same number of sample times as
 *    specified in ssSetNumSampleTimes.
 */
static void mdlInitializeSampleTimes(SimStruct *S)
{
    ssSetSampleTime(S, 0, SAMPLE_TIME);
    ssSetOffsetTime(S, 0, 0.0);

}



#undef MDL_INITIALIZE_CONDITIONS   /* Change to #undef to remove function */
#if defined(MDL_INITIALIZE_CONDITIONS)
  /* Function: mdlInitializeConditions ========================================
   * Abstract:
   *    In this function, you should initialize the continuous and discrete
   *    states for your S-function block.  The initial states are placed
   *    in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
   *    You can also perform any other initialization activities that your
   *    S-function may require. Note, this routine will be called at the
   *    start of simulation and if it is present in an enabled subsystem
   *    configured to reset states, it will be call when the enabled subsystem
   *    restarts execution to reset the states.
   */
  static void mdlInitializeConditions(SimStruct *S)
  {
  }
#endif /* MDL_INITIALIZE_CONDITIONS */



#define MDL_START  /* Change to #undef to remove function */
#if defined(MDL_START) 
  /* Function: mdlStart =======================================================
   * Abstract:
   *    This function is called once at start of model execution. If you
   *    have states that should be initialized once, this is the place
   *    to do it.
   */
  static void mdlStart(SimStruct *S)
 {
   
   WSAStartup(MAKEWORD(2,2), &wsaData);
   
   ReceivingSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
   ReceiverAddr.sin_family = AF_INET;
   ReceiverAddr.sin_port = htons(Port);    
   ReceiverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  
   bind(ReceivingSocket, (SOCKADDR *)&SenderAddr, sizeof(SenderAddr));

   

  }
#endif /*  MDL_START */



/* Function: mdlOutputs =======================================================
 * Abstract:
 *    In this function, you compute the outputs of your S-function
 *    block. Generally outputs are placed in the output vector, ssGetY(S).
 */
static void mdlOutputs(SimStruct *S, int_T tid)
{             
	   double d[4];
	   char buf[8];

     

	   recvfrom(ReceivingSocket, buf, BufLength, 0,
            (SOCKADDR *)&SenderAddr, &SenderAddrSize);
     
		    d[0]=atof(buf);  
	 recvfrom(ReceivingSocket, buf, BufLength, 0,
            (SOCKADDR *)&SenderAddr, &SenderAddrSize);
        d[1]=atof(buf);  
		   recvfrom(ReceivingSocket, buf, BufLength, 0,
            (SOCKADDR *)&SenderAddr, &SenderAddrSize);
        d[2]=atof(buf);  
		    recvfrom(ReceivingSocket, buf, BufLength, 0,
            (SOCKADDR *)&SenderAddr, &SenderAddrSize);
        d[3]=atof(buf);  		
		
	  real_T  *y = ssGetOutputPortRealSignal(S,0);
	  
	  y[0]=d[0];
	  y[1]=d[1];
	  y[2]=d[2];
	  y[3]=d[3];
	 

 
    
	
}



#undef MDL_UPDATE  /* Change to #undef to remove function */
#if defined(MDL_UPDATE)
  /* Function: mdlUpdate ======================================================
   * Abstract:
   *    This function is called once for every major integration time step.
   *    Discrete states are typically updated here, but this function is useful
   *    for performing any tasks that should only take place once per
   *    integration step.
   */
  static void mdlUpdate(SimStruct *S, int_T tid)
  {
  }
#endif /* MDL_UPDATE */



#undef MDL_DERIVATIVES  /* Change to #undef to remove function */
#if defined(MDL_DERIVATIVES)
  /* Function: mdlDerivatives =================================================
   * Abstract:
   *    In this function, you compute the S-function block's derivatives.
   *    The derivatives are placed in the derivative vector, ssGetdX(S).
   */
  static void mdlDerivatives(SimStruct *S)
  {
  }
#endif /* MDL_DERIVATIVES */



/* Function: mdlTerminate =====================================================
 * Abstract:
 *    In this function, you should perform any actions that are necessary
 *    at the termination of a simulation.  For example, if memory was
 *    allocated in mdlStart, this is the place to free it.
 */
static void mdlTerminate(SimStruct *S)
{

}


/*======================================================*
 * See sfuntmpl_doc.c for the optional S-function methods *
 *======================================================*/

/*=============================*
 * Required S-function trailer *
 *=============================*/

#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

thanks too much for help
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…