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

📄 matlab_to_phd_hans.c

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



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

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

void matlab_to_PHD(char tagname_write,char timestamp,float rawdata[])
//void matlab_to_PHD(char tagname_write,char timestamp,double rawdata[])
{
}
/*void main()
{
	char tagname_write;
    char timestamp;
    double rawdata;
    matlab_to_PHD(tagname_write,timestamp,rawdata);
	return;
}
*/
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
/*	Calling convention of Matlab
	nlhs : number of left hand side arguments (0 in this case)
	plhs : pointer to the left hand side arguments
	nrhs : number of right hand side arguments (3 in this case)
	plhs : pointer to the right hand side arguments


to call the mexfunction in Matlab, use :

 matlab_to_PHD(tagname_write,timestamp,rawdata); 

ITIME = seconds after 01/01/1970	 

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

First login using PHD_connect(...)

*/

{
int   buflen,mrows,ncols;
char *tagname; 
char *timestamp; 
int datalen;
float *databuf;
//float *databuf;
//void *databuf;

TAGNO tagno;
ISTAT status;
INT4 tformat; /* integer for the time format ; 1 = time format like 16-NOV-1999 00:00:00 */
ITIME itime_stamp; /* timestamp, converted to seconds since 1/1/1970 00:00:00 */
CONF conf=100;       /*the confidence of data*/

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

/* Error checking*/
/* check for proper number of arguments */
    if(nrhs!=3) 
      mexErrMsgTxt("3 inputs required.");
    else if(nlhs !=0) 
      mexErrMsgTxt("There are too many output argument .");



/* 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 */
    tagname=mxCalloc(buflen, sizeof(char));

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

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


if(tagno==0)
	{
	mexPrintf("Tag does not exist!\n");
	return;
	}

//mexPrintf("the tagno of the tag that the matlab data is put into is:\n tagno=%d\n",tagno);
//mexPrintf(" the tagname of the tag that the matlab data is put into is:\n tagname=%s\n",tagname);

mxFree(tagname); /* Free the memory*/



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

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



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

/* It must be a row vector */
    if (mxGetM(prhs[1])!=1)
      mexErrMsgTxt("The timestamp 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 */
   timestamp=mxCalloc(buflen, sizeof(char));

/* copy the string data from prhs[1] into a C string.*/
    status = mxGetString(prhs[1], timestamp, buflen);
   if(status != 0) 
     mexWarnMsgTxt("Not enough space. second string (timestamp) is truncated.");
//mexPrintf("show the value of timestamp \n timestamp=%s\n",timestamp);
/* Converting the starting time to seconds since midnight 1/1/1970
	to make some calculations in Matlab, not used in PHD */
phd_itime(timestamp, tformat, &itime_stamp);

//mexPrintf("show the value of timestamp \n timestamp=%s\n",timestamp);




/* Input 3 must be a row of data (rawdata) */

mrows=mxGetM(prhs[2]);
ncols=mxGetN(prhs[2]);
/*if ( !mxIsSingle(prhs[2]) ||mxIsComplex(prhs[2])||(mrows==1&&ncols==1))
{
   mexErrMsgTxt("The rawdata (input 3) must be a scalar single.");

}
*/
//plhs[0]=mxCreateDoubleMatrix(mrows,ncols,mxREAL);

databuf=(float *)mxGetPr(prhs[2]);
//databuf=mxGetPr(plhs[2]);
    //if ( mxIsSingle(prhs[2]) != 1)
      //  mexErrMsgTxt("The rawdata (input 3) must be a float number.");


    //if ( mxIsDouble(prhs[2]) != 1)
      //mexErrMsgTxt("The rawdata (input 3) must be a double number.");
/*
/* It must be a row vector */
    //if (mxGetM(prhs[2])!=1)
     // mexErrMsgTxt("The rawdata must be a row vector.");



    
/* Get the length of the input data*/
//	buflen=mxGetNumberOfDimensions(prhs[2]);
    //buflen =(mxGetM(prhs[2]))*(mxGetN(prhs[2]));
	//datalen=buflen*(sizeof(float));
     datalen=1*(sizeof(float));

/* allocate memory for input data */
   //data_buf=mxCalloc(buflen, sizeof(int));
      // databuf=(float*)(mxGetPr(prhs[2]));
       // databuf=mxGetPr(prhs[2]);

 //status = mxGetString(prhs[2], databuf, buflen);
   // if(status != 0) 
     // mexWarnMsgTxt("Not enough space. 3 iuput is truncated.");



//mexPrintf("buflen=%d \n datalen=%d\n",buflen,datalen);
//mexPrintf("\n show the value of conf \n conf=%d\n",conf);
mexPrintf("\n  the value of data which is put into PHD is:\n rawdata=%f\n",*databuf);



//status=phd_loaddata(tagno,pr_phdata,min_dproc,arc_loaad);
//status=phd_putdata(tagno,timestamp,tformat,conf,databuf,datalen,units);
status=phd_moddata(tagno,timestamp,tformat,conf,databuf,datalen,NULL);
if(status!=0)
	{
	mexPrintf("put data failed!\n");
	//free(&spec); /* deallocate the memory of the spec-structure */
	//phd_delphdata(&pr_data); /* deallocate the memory of the pr_data structure */
	return;
	}

//matlab_to_PHD(tagname,timestamp,rawdata);

mxFree(timestamp); /* Free the memory*/
mxFree(databuf); /* Free the memory*/




}

⌨️ 快捷键说明

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