回 帖 发 新 帖 刷新版面

主题:求助解决XPC中数据采集板卡的A/D模块编写

本人在SIMULINK的 XPC中用C MEX S-函数编写宏拓PC7484数据采集板卡的A/D模块..
但是模块在下载到目标PC机后 运行正常缺没有采集到任何数据...
请高手指点下哪里错了 不胜感激 本人QQ23608441...
如要PC7484说明书。请QQ联系~
程序如下 :
#define  S_FUNCTION_LEVEL  2
#undef   S_FUNCTION_NAME
#define  S_FUNCTION_NAME  adpc7484
#include  <stddef.h>
#include  <stdlib.h>
#include  "simstruc.h" 
#ifdef   MATLAB_MEX_FILE
#include  "mex.h"
#else
#include  <windows.h>
#include  "io_xpcimport.h"
#endif

/* Input Arguments */
#define NUM_PARAMS             (5)
#define BASE_ADDRESS_ARG       (ssGetSFcnParam(S,0))
#define DEV_ARG                (ssGetSFcnParam(S,1))
#define CHANNEL_ARG            (ssGetSFcnParam(S,2))
#define RANGE_ARG              (ssGetSFcnParam(S,3))
#define SAMPLE_TIME_PARAM      (ssGetSFcnParam(S,4))
/* Convert S Function Parameters to Varibles */
#define BASE                   ((uint_T) mxGetPr(BASE_ADDRESS_ARG)[0])
#define SAMPLE_TIME            ((real_T) mxGetPr(SAMPLE_TIME_PARAM)[0])
#define SAMPLE_OFFSET          ((real_T) mxGetPr(SAMPLE_TIME_PARAM)[1])

static char_T msg[256];
      /* char_T ms[256];*/
/*====================*
* S-function methods *
*====================*/
static void mdlCheckParameters(SimStruct *S)
{
}
static void mdlInitializeSizes(SimStruct *S)
{
    uint_T i;
#ifndef MATLAB_MEX_FILE
#include "io_xpcimport.c"
#endif
    

    ssSetNumSFcnParams(S, NUM_PARAMS);
    if (ssGetNumSFcnParams(S) == ssGetSFcnParamsCount(S)) {
        mdlCheckParameters(S);
        if (ssGetErrorStatus(S) != NULL) {
            return;
        }
    } else {
        return; /* Parameter mismatch will be reported by Simulink */
    }
    ssSetNumContStates(S, 0);
    ssSetNumDiscStates(S, 0);

    if (!ssSetNumInputPorts(S, 0)) return;
    if (!ssSetNumOutputPorts(S, mxGetNumberOfElements(CHANNEL_ARG))) return;
    for (i=0;i<mxGetNumberOfElements(CHANNEL_ARG);i++) {
        ssSetOutputPortWidth(S, i, 1);
    }
    ssSetNumSampleTimes(S, 1);
    ssSetNumModes(S, 0);
    ssSetNumNonsampledZCs(S, 0);
    ssSetSFcnParamNotTunable(S,0);
    ssSetSFcnParamNotTunable(S,1);
    ssSetSFcnParamNotTunable(S,2);
    ssSetSFcnParamNotTunable(S,3);
    ssSetSFcnParamNotTunable(S,4);
    ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE | SS_OPTION_PLACE_ASAP);

}


static void mdlInitializeSampleTimes(SimStruct *S)
{
    ssSetSampleTime(S, 0, SAMPLE_TIME);
    ssSetOffsetTime(S, 0, SAMPLE_OFFSET);
}

#define MDL_START
static void mdlStart(SimStruct *S)
{
#ifndef MATLAB_MEX_FILE

    uint_T i, channel;
    char devName[20];
    
switch ((int_T)mxGetPr(DEV_ARG)[0]) {
        case 1:
            strcpy(devName,"Hotec PC-7484");
            break;
}
    /* Check That The Hardware is functioning */
    /* Setup ADCTRL Parameters */

    rl32eInpW(BASE+0x2);           /* dummy read from A/D */
   
    if (!(rl32eInpB(BASE+0x3) & 0x80)) { /* if data ready, error */
        sprintf(msg,"%s (0x%x): test A/D conversion failed", devName, BASE);
      ssSetErrorStatus(S,msg);
        return;
    }
#endif
                 
}
static void mdlOutputs(SimStruct *S, int_T tid)
{
#ifndef MATLAB_MEX_FILE
    uint_T i,channel,dh,dl;
    real_T *y,range,res;
    for (i=0;i<mxGetNumberOfElements(CHANNEL_ARG);i++) 
{
       channel=(uint_T)mxGetPr(CHANNEL_ARG)-1;
        y=ssGetOutputPortSignal(S,i);
        range=mxGetPr(RANGE_ARG);
        rl32eOutpB(BASE,channel); /* select channel */
        rl32eOutpB(BASE+1,0x00); /* start conversion */
  do{
   dl=rl32eInpB(BASE+3);
  }while(dl&0x80!=0x80);
  dh=rl32eInpB(BASE+2);
  dl=rl32eInpB(BASE+3);
  res=dh*16+dl&0x0F;
        y[0]=(dl&0x7F)*range/128; 
    }
#endif
                 
}
static void mdlTerminate(SimStruct *S)
{   
}

#ifdef MATLAB_MEX_FILE  /* Is this file being compiled as a MEX-file? */
#include "simulink.c"   /* Mex glue */
#else
#include "cg_sfun.h"    /* Code generation glue */
#endif

回复列表 (共1个回复)

沙发

我比你还惨,我连mex32文件还没生成呢,给出的错误提示是无法链接。能不能给一点修改意见。不过我怀疑是我的Matlab破解不完全,可能正版的可以吧。

我来回复

您尚未登录,请登录后再回复。点此登录或注册