252806294 发表于 2009-6-26 16:47

关于s函数与其他进程之间的通信问题

目前我在做一个课题,要将simulink仿真数据实时传到一个windows进程然后再进行后面的驱动,通过共享内存和事件机制进行二进程之间的同步交互。
下面是s函数

#define S_FUNCTION_NAMEmyport
#define S_FUNCTION_LEVEL 2

#include "simstruc.h"
#include "windows.h"
extern HANDLE hGlobalWriteEvent;
extern HANDLE hGlobalReadEvent;
real_T * pshareblock;
HANDLE hMapFile;
DWORD dwWaitResult;
//HANDLE OpenEvent0;
//HANDLE OpenEvent1;

static void mdlInitializeSizes(SimStruct *S)
{
    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, 6);
    ssSetInputPortDirectFeedThrough(S, 0, 0);
    if (!ssSetNumOutputPorts(S, 1)) return;
    ssSetOutputPortWidth(S, 0, 6);
    ssSetNumSampleTimes(S, 1);
    ssSetNumRWork(S, 0);
    ssSetNumIWork(S, 0);
    ssSetNumPWork(S, 0);
    ssSetNumModes(S, 0);
    ssSetNumNonsampledZCs(S, 0);
    ssSetOptions(S, 0);
}
static void mdlInitializeSampleTimes(SimStruct *S)
{
    ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
    ssSetOffsetTime(S, 0, 0.0);
}
#define MDL_INITIALIZE_CONDITIONS   /* Change to #undef to remove function */
#if defined(MDL_INITIALIZE_CONDITIONS)
static void mdlInitializeConditions(SimStruct *S)
{
      hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, // Read/write permission.
            FALSE,                           // Do not inherit the name
            "MySharemem");            // of the mapping object.
      if (hMapFile == NULL)
      {
            printf("Could not open file-mapping object.");
      }
      pshareblock=( double* )MapViewOfFile(hMapFile,
                  FILE_MAP_ALL_ACCESS,
                  0,
                  0,
                  6* sizeof(double));
      
      if (pshareblock == NULL)
      {
         printf("Could not map view of file.");
      }
   
          OpenEvent(
            EVENT_ALL_ACCESS,// access
            FALSE,    // inheritance option
            "WriteEvent"      // object name
      );
            OpenEvent(
            EVENT_ALL_ACCESS,// access
             FALSE,    // inheritance option
             " ReadEvent"      // object name
      );
      
}
   
#endif /* MDL_INITIALIZE_CONDITIONS */

#define MDL_START/* Change to #undef to remove function */
#if defined(MDL_START)
static void mdlStart(SimStruct *S)
{
}
#endif /*MDL_START */

static void mdlOutputs(SimStruct *S, int_T tid)
{
    real_T *u =(real_T *)ssGetInputPortRealSignal(S,0);
    real_T*y = (real_T *)ssGetOutputPortSignal(S,0);
    y = u;
    y = u;
    y = u;
    y = u;
    y = u;
    y = u;
}

#define MDL_UPDATE/* Change to #undef to remove function */
#if defined(MDL_UPDATE)
static void mdlUpdate(SimStruct *S, int_T tid)
{
   
    dwWaitResult = WaitForSingleObject( hGlobalReadEvent,
         INFINITE);
          if (dwWaitResult=WAIT_OBJECT_0)
      {   
             real_T *u = (real_T *) ssGetInputPortRealSignal(S,0);
            
            pshareblock=u;
            pshareblock=u;
            pshareblock=u;
            pshareblock=u;
            pshareblock=u;
            pshareblock=u;
         if ((! ResetEvent(hGlobalReadEvent))||(! SetEvent(hGlobalWriteEvent)))
                {
                     printf("Could not ResetReadEvent or SetWriteEvent.");
                }
            
      }
}
#endif /* MDL_UPDATE */

#define MDL_DERIVATIVES/* Change to #undef to remove function */
#if defined(MDL_DERIVATIVES)

static void mdlDerivatives(SimStruct *S)
{
}
#endif /* MDL_DERIVATIVES */

static void mdlTerminate(SimStruct *S)
{
UnmapViewOfFile(pshareblock);
    CloseHandle(hMapFile);
}


#ifdefMATLAB_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


在将c文件编译成mexw32文件时,出现一个错误:
>> mex myport.c

Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

C:\DOCUME~1\vlp\LOCALS~1\Temp\myport.obj
   Creating library _lib6486.x and object _lib6486.exp
myport.obj : error LNK2001: unresolved external symbol _hMutex
myport.mexw32 : fatal error LNK1120: 1 unresolved externals

C:\PROGRAM FILES\MATLAB71\BIN\MEX.PL: Error: Link of 'myport.mexw32' failed.

??? Error using ==> mex
Unable to complete successfully

如果将函数static void mdlUpdate中的
    dwWaitResult = WaitForSingleObject( hGlobalReadEvent,
         INFINITE);
去掉,则可以通过编译,请高手帮忙,是不是s函数不认识WaitForSingleObject还是什么原因
:@( :@(
页: [1]
查看完整版本: 关于s函数与其他进程之间的通信问题