⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 phd_getrawdata_to_matlab.c

📁 本例利用C MEX及API文件实现matlab与PHD只见的通讯.能实现数据的存取。
💻 C
字号:
/*
Program to take data out of PHD and store them into matlab.
PHD = Process History Database, a Honeywell-database to store data of a chemical plant

Author : Hans Vereecken
003235613411
hans.vereecken@notes.basant.be

BASF Antwerp Belgium
May be distributed freely.


To compile in matlab via "mex phd_getrawdata_to_matlab.c netapishr.lib"
*/

#include "PHDUSER.H" /* PHD-header */
#include "mex.h" /* Matlab header */
#include <stdio.h>
#include <string.h>
#include "matrix.h"

void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
/*	Calling convention of Matlab
	nlhs : number of left hand side arguments (5 in this case)
	plhs : pointer to the left hand side arguments
	nrhs : number of right hand side arguments (4 in this case)
	plhs : pointer to the right hand side arguments


to call the mexfunction in Matlab, use :
[time_vector,value_vector,conf_vector,ITIME of startingdate,ITIME of ref_time]= phd_getrawdata_to_matlab(tagname,begintime,endtime,reference time);

ITIME = seconds after 01/01/1970

e.g. [t,v,conf,start,ref]=phd_getrawdata_to_matlab(tag,t1,t2,t_ref);	 

Time format like this : 16-NOV-1999 00:00:00 ; characters in MATLAB and in C++
ITIME is the time in seconds since 1/1/1970 00:00:00

First login using PHD_connect(...)

*/

{
int   buflen;
int k; /* counter for for-loops */
char *input_tagname; 
char *input_start_time; 
char *input_end_time; 
char *input_ref_time; 
double *pind_v,*pind_t,*pind_itime,*pind_itime_ref; 
double *pind_c;

TAGNO tagno;
PHDATA *pr_data; /* pointer to the data structure of PHD */
PHSPEC spec; /* PHD-structure for the specifications */
ISTAT status;
INT4 tformat; /* integer for the time format ; 1 = time format like 16-NOV-1999 00:00:00 */
ITIME itime_start; /* starting time , converted to seconds since 1/1/1970 00:00:00 */
ITIME itime_ref; /* reference time , converted to seconds since 1/1/1970 00:00:00 */

/*------------------------------------------------------------------------------------------*/

/* Error checking*/
/* check for proper number of arguments */
    if(nrhs!=4) 
      mexErrMsgTxt("4 inputs required.");
    else if(nlhs != 5) 
      mexErrMsgTxt("5 output arguments required.");



/* First input must be a string (the tagname) */
    if ( mxIsChar(prhs[0]) != 1)
      mexErrMsgTxt("The tagname (first input) must be a string.");

/* It must be a row vector */
    if (mxGetM(prhs[0])!=1)
      mexErrMsgTxt("The tagname must be a row vector.");
    
/* Get the length of the input string */
    buflen = (mxGetM(prhs[0]) * mxGetN(prhs[0])) + 1;

/* allocate memory for input string */
    input_tagname=mxCalloc(buflen, sizeof(char));

/* copy the string data from prhs[0] into a C string.*/
    status = mxGetString(prhs[0], input_tagname, buflen);
    if(status != 0) 
      mexWarnMsgTxt("Not enough space. first string (the tagname) is truncated.");

tagno=phd_tagno(input_tagname); // gives the number of a specific tag


if(tagno==0)
	{
	mexPrintf("Tag does not exist!\n");
	return;
	}
//mexPrintf("the tagname ofthe tag that matlab get data from is:\n read_tagname=%s\n",input_tagname);
mxFree(input_tagname); /* Free the memory*/

/*-------------------------------------------------------------------------*/

/* Memory allocation of the SPEC-structure */
memset(&spec,0,sizeof(spec));

spec.smpsecs=0; /* Return raw data */
spec.rawdata='X';
spec.resamp_method='S'; /* Direct sample */

/* Memory allocation of the PHDATA-structure */
status = phd_newphdata(0,0,&pr_data);

if (status != 0)
	{
	mexPrintf("Memory allocation error when creating pr_data!\n");
	free(&spec); /* deallocate the memory of the spec-structure */
	phd_delphdata(&pr_data); /* deallocate the memory of the pr_data structure */
	/* It is possible that this routine doesn't work after a non succesfull memory allocation */
	return;
	}

/*-----------------------------------------------------------------------------------------*/

tformat=1; /* like this : "16-NOV-1999 00:00:00" */

/* Second input must be a string (the begindate) */
    if ( mxIsChar(prhs[1]) != 1)
      mexErrMsgTxt("The begindate (second input) must be a string.");

/* It must be a row vector */
    if (mxGetM(prhs[1])!=1)
      mexErrMsgTxt("The begindate must be a row vector.");
    
/* Get the length of the input string */
    buflen = (mxGetM(prhs[1]) * mxGetN(prhs[1])) + 1;

/* allocate memory for input string */
    input_start_time=mxCalloc(buflen, sizeof(char));

/* copy the string data from prhs[1] into a C string.*/
    status = mxGetString(prhs[1], input_start_time, buflen);
    if(status != 0) 
      mexWarnMsgTxt("Not enough space. second string (the starting time) is truncated.");

/* third input must be a string (the enddate) */
    if ( mxIsChar(prhs[2]) != 1)
      mexErrMsgTxt("The enddate (third input) must be a string.");

/* It must be a row vector */
    if (mxGetM(prhs[2])!=1)
      mexErrMsgTxt("The enddate must be a row vector.");
    
/* Get the length of the input string */
    buflen = (mxGetM(prhs[2]) * mxGetN(prhs[2])) + 1;

/* allocate memory for input string */
    input_end_time=mxCalloc(buflen, sizeof(char));

/* copy the string data from prhs[2] into a C string.*/
    status = mxGetString(prhs[2], input_end_time, buflen);
    if(status != 0) 
      mexWarnMsgTxt("Not enough space. Third string (the end time) is truncated.");

/* Converting the starting time to seconds since midnight 1/1/1970
	to make some calculations in Matlab, not used in PHD */
phd_itime(input_start_time, tformat, &itime_start);


/* Input 4 must be a string (the reference time) */
    if ( mxIsChar(prhs[3]) != 1)
      mexErrMsgTxt("The reference time (input 4) must be a string.");

/* It must be a row vector */
    if (mxGetM(prhs[3])!=1)
      mexErrMsgTxt("The reference time must be a row vector.");
    
/* Get the length of the input string */
    buflen = (mxGetM(prhs[3]) * mxGetN(prhs[3])) + 1;

/* allocate memory for input string */
    input_ref_time=mxCalloc(buflen, sizeof(char));

/* copy the string data from prhs[3] into a C string.*/
    status = mxGetString(prhs[3], input_ref_time, buflen);
    if(status != 0) 
      mexWarnMsgTxt("Not enough space. string 4 (the reference time) is truncated.");

/* Converting the reference time to seconds since midnight 1/1/1970 */
phd_itime(input_ref_time, tformat, &itime_ref);

/* mexPrintf("Starting fetch.\n"); */



//mexPrintf("input_tagname=%s\n",input_tagname);
//mexPrintf("input_ref_time=%s\n",input_ref_time);





status=phd_getdata(tagno,input_start_time,input_end_time,tformat,&spec,&pr_data,NULL);
if(status!=0)
	{
	mexPrintf("Get data failed!\n");
	free(&spec); /* deallocate the memory of the spec-structure */
	phd_delphdata(&pr_data); /* deallocate the memory of the pr_data structure */
	/* It is possible that this routine doesn't work properly after a non succesfull phd_getdata */
	return;
	}

/* mexPrintf("Fetch finished. Start creating matlabvariables.\n"); */

mxFree(input_start_time); /* Free the memory*/
mxFree(input_end_time); /* Free the memory*/

/* Since variable does not yet exist in MATLAB workspace,
   create it and place it in the global workspace. */
	plhs[0]=mxCreateDoubleMatrix(pr_data->nval,1,mxREAL);
	/* No possibility to test the pointer, function doesn't return anything if unsuccesfull
	the mex-file is ended automaticly by matlab if the creation was unsuccesfull 
	Problem = memory allocation of pr_data is not set free in this case
	memory can only be deallocated by closing matlab */
	pind_t=mxGetPr(plhs[0]); /* time vector */
	plhs[1]=mxCreateDoubleMatrix(pr_data->nval,1,mxREAL);
	pind_v=mxGetPr(plhs[1]); /* value vector */
	plhs[2]=mxCreateDoubleMatrix(pr_data->nval,1,mxREAL);
	pind_c=mxGetPr(plhs[2]); /* vector with confidence numbers */



if (pr_data->datatype == 'F') // if the datatype is real (float)
{
for (k=0;k<pr_data->nval;k++)
	{
	pind_v[k]=pr_data->fval[k]; /* floating point values */
	}
}

if (pr_data->datatype == 'I') // if the datatype is integer
{
for (k=0;k<pr_data->nval;k++)
	{
	pind_v[k]=(double) pr_data->ival[k]; /* integer values --> converted to double*/
	}
}

for (k=0;k<pr_data->nval;k++)
	{
	pind_t[k]=pr_data->istamp[k]; /* time stamp in seconds */
	}

for (k=0;k<pr_data->nval;k++)
	{
	pind_c[k]=pr_data->conf[k]; /* confidence values */
	}

plhs[3]=mxCreateDoubleMatrix(1,1,mxREAL);
	pind_itime=mxGetPr(plhs[3]); /* return the ITIME value of the starting time */
	pind_itime[0]=itime_start;   /* in the 4th left hand side parameter when calling the mex routine*/

plhs[4]=mxCreateDoubleMatrix(1,1,mxREAL);
	pind_itime_ref=mxGetPr(plhs[4]); /* return the ITIME value of the reference time */
	pind_itime_ref[0]=itime_ref;   /* in the left hand side parameter no 5 when calling the mex routine*/

free(&spec); /* deallocate the memory of the spec-structure */
phd_delphdata(&pr_data); /* deallocate the memory of the pr_data structure */

/* mexPrintf("Finished. Returning to matlab.\n"); */

/* The End */
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -