Lost data on the socket programming

2 Ansichten (letzte 30 Tage)
Sangkyun Kim
Sangkyun Kim am 10 Okt. 2011
Hello
Now, I work for a simple socket programming.
With many help, I made a simple level2 s-function with C. It works normally at first, but after several seconds(about 10 seconds), the data is start to collapsed.
Strange thing is that no data is missing when I put some constant values for the data of transmission. Data loss appears only when I use variable data for the transmission.
I thought that this is a kind of memory management problem. However, I can not find what makes this problem until now.
If anyone can give me answer or hint, it will be greatly appreciated.
I attached my code. This is a client program on the Matlab, The server program is a extremely simple echo function on my micro controller.(It just send back the data with bytes directly)
---------------------------------------------------------------------------
/*
* sfuntmpl_basic.c: Basic 'C' template for a level 2 S-function.
*
* -------------------------------------------------------------------------
* | See matlabroot/simulink/src/sfuntmpl_doc.c for a more detailed template |
* -------------------------------------------------------------------------
*
* Copyright 1990-2002 The MathWorks, Inc.
* $Revision: 1.27.4.2 $
*/
/*
* You must specify the S_FUNCTION_NAME as the name of your S-function
* (i.e. replace sfuntmpl_basic with the name of your S-function).
*/
#define S_FUNCTION_NAME sFclient
#define S_FUNCTION_LEVEL 2
// defines for the TCP/IP
#define PORT 50001 //Changeable
// define the packet size( x 4Bytes)
#define TCPdataCnt 20
/*
* Need to include simstruc.h for the definition of the SimStruct and
* its associated macro definitions.
*/
#include "simstruc.h"
// Include files for the Visual studio(TCP/IP)
#include <stdio.h>
#include <WinSock2.h>
#include <WS2tcpip.h>
// Global variables for TCP/IP
char destination[] = "192.168.22.2";
SOCKET dstSocket;
struct sockaddr_in dstAddr;
unsigned char buffer[128];
unsigned char buffer_temp[128];
WSADATA data;
/* Error handling
* --------------
*
* You should use the following technique to report errors encountered within
* an S-function:
*
* ssSetErrorStatus(S,"Error encountered due to ...");
* return;
*
* Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
* It cannot be a local variable. For example the following will cause
* unpredictable errors:
*
* mdlOutputs()
* {
* char msg[256]; {ILLEGAL: to fix use "static char msg[256];"}
* sprintf(msg,"Error due to %s", string);
* ssSetErrorStatus(S,msg);
* return;
* }
*
* See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
*/
/*====================*
* 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, 0); /* 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, 1)) return;
ssSetInputPortWidth(S, 0, TCPdataCnt);
ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*/
/*
* Set direct feedthrough flag (1=yes, 0=no).
* A port has direct feedthrough if the input is used in either
* the mdlOutputs or mdlGetTimeOfNextVarHit functions.
* See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
*/
ssSetInputPortDirectFeedThrough(S, 0, 1);
if (!ssSetNumOutputPorts(S, 1)) return;
ssSetOutputPortWidth(S, 0, TCPdataCnt);
ssSetNumSampleTimes(S, 1);
ssSetNumRWork(S, 0);
ssSetNumIWork(S, 0);
ssSetNumPWork(S, 0);
ssSetNumModes(S, 0);
ssSetNumNonsampledZCs(S, 0);
/* Specify the sim state compliance to be same as a built-in block */
ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
ssSetOptions(S, 0);
}
/* 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, CONTINUOUS_SAMPLE_TIME);
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
}
#define 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,0), &data);
// Set the structure of sockaddr_in
memset( &dstAddr, 0, sizeof(dstAddr));
dstAddr.sin_port = htons(PORT);
dstAddr.sin_family = AF_INET;
dstAddr.sin_addr.s_addr = inet_addr(destination);
// Create a socket
dstSocket = socket( AF_INET, SOCK_STREAM, 0);
connect(dstSocket, (struct sockaddr *) &dstAddr, sizeof(dstAddr));
}
#endif /* MDL_START */
/* Function: mdlOutputs =======================================================
* Abstract:
* In this function, you compute the outputs of your S-function
* block.
*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
const real_T *u = (const real_T*) ssGetInputPortSignal(S,0);
real_T *y = ssGetOutputPortSignal(S,0);
int temp[TCPdataCnt];
int rcvData[TCPdataCnt];
double rcvDouble[TCPdataCnt];
int c1;
int c2;
int c3;
int c4;
unsigned int i;
int validFrame;
int validData;
int recvFlag;
unsigned int rcvSize;
unsigned int sendSize;
sendSize = TCPdataCnt*4 + 2;
rcvSize = TCPdataCnt*4 + 2;
buffer[0] = 0x7E;
for( i = 0; i < TCPdataCnt; ++i)
{
temp[i] = (int)(u[i]*1000000);
buffer[i*4 + 1] = (unsigned char)((temp[i] >> 24) & 0x00FF);
buffer[i*4 + 2] = (unsigned char)((temp[i] >> 16) & 0x00FF);
buffer[i*4 + 3] = (unsigned char)((temp[i] >> 8) & 0x00FF);
buffer[i*4 + 4] = (unsigned char)(temp[i] & 0x00FF);
}
buffer[sendSize-1] = 0x7E;
// send a packet
send( dstSocket, buffer, sendSize, 0);
// receive a packet
//recvFlag = recv( dstSocket, buffer, rcvSize, 0);
for( i = 0; i < rcvSize; ++i)
{
recvFlag = 0;
while( recvFlag != 1 )
{
recvFlag = recv( dstSocket, buffer_temp, 1, 0);
}
buffer[i] = buffer_temp[0];
}
// Convert the packet data to the integer variable
for( i = 0; i < TCPdataCnt; ++i)
{
rcvData[i] = 0;
c1 = (int)buffer[4*i + 1];
c2 = (int)buffer[4*i + 2];
c3 = (int)buffer[4*i + 3];
c4 = (int)buffer[4*i + 4];
rcvData[i] = rcvData[i] | ((c1 << 24) & 0xFF000000);
rcvData[i] = rcvData[i] | ((c2 << 16) & 0x00FF0000);
rcvData[i] = rcvData[i] | ((c3 << 8) & 0x0000FF00);
rcvData[i] = rcvData[i] | ((c4) & 0x000000FF);
}
for( i = 0; i < TCPdataCnt; ++i)
{
y[i] = (double)rcvData[i]/1000000;
}
}
#define MDL_UPDATE
#if defined(MDL_UPDATE)
static void mdlUpdate(SimStruct *S, int_T tid)
{
}
#endif
/* 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)
{
// Terminate the socket
closesocket(dstSocket);
WSACleanup();
}
/*======================================================*
* 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

Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by